-
Notifications
You must be signed in to change notification settings - Fork 0
/
HanSoloLazerGun.java
130 lines (116 loc) · 2.47 KB
/
HanSoloLazerGun.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* https://codeforces.com/problemset/problem/514/B
#brute-force #geometry #math #binary-search
* Binary Search Tree with the formular factor
*
other way: characteristic of 3 point in a line.
(average line)
A (xA, yA);
B (xB, yB);
C (xC, yC);
(xB - xA)*(yC - yA) = (xC - xA)*(yB - yA)
(xB - x0)/(yB - y0) = (xC - x0) / (yC - y0)
A (0, 0)
B (2, 1)
C (10, 5)
*/
import java.util.Scanner;
import java.util.TreeSet;
public class HanSoloLazerGun {
public static int getGDC(int val0, int val1, int val2) {
int v1, v2, v0 = Math.min(val0, val1);
if (val2 != 0) {
v0 = Math.min(v0, val2);
}
if (val0 == v0) {
v1 = val1;
v2 = val2;
} else if (val1 == v0) {
v1 = val0;
v2 = val2;
} else {
v1 = val0;
v2 = val1;
}
int tmp, gdc = 1;
for (int i = 1; i <= Math.sqrt(v0); i++) {
if (v0 % i == 0) {
tmp = v0 / i;
if (v1 % tmp == 0 && v2 % tmp == 0) {
gdc = tmp;
break;
}
if (v1 % i == 0 && v2 % i == 0) {
gdc = i;
}
}
}
return gdc;
}
public static Formular getFormular(int x1, int y1, int x2, int y2) {
int a, b, c;
int gdc = 1;
// x = k
if (x1 == x2) {
b = 0;
a = 1;
c = -x1;
}
// y = k
else if (y1 == y2) {
a = 0;
b = 1;
c = -y1;
}
// ax + by + c = 0
else {
a = y2 - y1;
b = x1 - x2;
c = -(a * x1 + b * y1);
if (a < 0) {
a = -a;
b = -b;
c = -c;
}
gdc = getGDC(a, Math.abs(b), Math.abs(c));
}
return new Formular(a / gdc, b / gdc, c / gdc);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int x0 = sc.nextInt();
int y0 = sc.nextInt();
int x, y;
TreeSet<Formular> ts = new TreeSet<>();
for (int i = 0; i < n; i++) {
x = sc.nextInt();
y = sc.nextInt();
if (x == x0 && y == y0) {
continue;
}
Formular f = getFormular(x0, y0, x, y);
ts.add(f);
}
System.out.println(ts.size());
}
}
// f(x) : ax + by + c = 0;
class Formular implements Comparable<Formular> {
int a, b, c;
Formular(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
public int compareTo(Formular other) {
if (a == other.a) {
if (b == other.b) {
return c - other.c;
} else {
return b - other.b;
}
} else {
return a - other.a;
}
}
}