We are given up to n=50 points, distinct. For each pair of points, there is a line (infinite line) connecting them, but if multiple points are collinear, they share the same line. So essentially we have a set of lines determined by the points: each line that contains at least two points is considered a wire. And each such wire is unique; if two points lie on same line, they define the same wire. We need to count the number of pairs of wires that intersect. Two wires intersect if they are not parallel? Actually, two lines on a plane intersect unless they are parallel (including coincident? But coincident lines are the same line; but we consider distinct wires. Since each wire is defined by a set of points that are collinear, two wires are distinct if they are not the same line. So distinct lines may be parallel and not intersect (if parallel distinct lines) or may intersect at a single point. We need to count pairs of distinct wires (lines) that intersect, i.e., are not parallel. Wait: parallel distinct lines do not intersect. However, there is also the possibility that two lines are the same, but they are not considered distinct wires, so we don't count them. So essentially we have a set L of lines (each line determined by at least 2 points). Count number of unordered pairs (l1, l2) with l1 != l2 such that l1 and l2 intersect (i.e., not parallel). Equivalent to total pairs minus pairs that are parallel but distinct.
But careful: Two lines that are coincident are the same line, so they are not considered distinct wires. Since each wire is defined by the set of points, if multiple points are collinear, they are all on the same line, so there is only one wire for that line. So we won't have duplicate lines. So we have a set of unique lines. So we just need to count intersecting pairs.
However, there is nuance: Two lines could be parallel but not coincident, they will not intersect. Also lines could be the same? Not allowed. So answer = total pairs - number of pairs of parallel lines.
Total pairs: if we have m distinct lines (each with at least 2 points), then answer = C(m,2) - (number of unordered pairs of distinct lines that are parallel).
But is there any other condition? What about lines that are the same? Not counted. So indeed.
So we need to compute:
-
Determine all lines that contain at least 2 points from the given set.
-
Among those lines, we need to count how many are distinct (by their direction and offset). Then count parallel pairs.
But careful: lines might have same direction but be distinct (parallel). So we need to group lines by direction (slope) and also by intercept to distinguish lines.
Given n <= 50, we can compute all lines defined by every pair of points. However, many pairs will define same line. So we need to identify unique lines. For each line, we need its representation (e.g., normalized equation). Then count total number of lines m. Then among these lines, count pairs that are parallel but not the same.
Alternatively, we can compute number of intersecting pairs directly: For each pair of distinct lines, check if they intersect (i.e., not parallel). That would be O(m^2) and m can be up to number of lines. The maximum number of lines determined by n points (no three collinear) is C(n,2) lines because each pair determines a line, but if no three collinear, each line contains exactly 2 points, so number of lines m = C(n,2). For n=50, C(50,2)=1225. So m <= 1225. O(m^2) ~ 1.5 million, which is fine. But we need to identify lines uniquely. The challenge is to avoid double counting: many pairs of points define same line if points collinear. So we need to compute the set of lines.
So approach: enumerate all pairs of points (i,j) with i<j. For each pair, compute a canonical representation of the line that goes through these two points. But careful: different pairs may yield same line; we need to merge them into one line.
We can use a dictionary mapping from a canonical representation to a set of points that lie on that line (or just count points). For each line, we need to know at least its direction (for parallel test) and its offset (to determine if two lines are the same). For counting intersecting pairs, we just need to know which lines are parallel (or same). But we must ensure we don't count same line as parallel. So we can store for each line a normalized representation: (a,b,c) such that ax+by+c=0 with gcd and sign normalization, and also ensure that lines are unique by that tuple.
Then we have a list of these normalized triples. Then total pairs = C(m,2). Then count parallel pairs: for each distinct direction, if there are k lines with that direction, then number of parallel pairs is C(k,2). However, note: lines with same direction but are the same line would be duplicates; but since we only have unique lines, that won't happen. So indeed we can group by direction.
But direction grouping should be careful: lines with same slope but possibly both normalized representations may have different (a,b,c) but same direction if (a,b) are proportional. However, after normalization, (a,b) are reduced to relatively prime integers, with a certain sign convention (e.g., make a >0 or a==0 and b>0). So direction is essentially the normalized (a,b) vector. So we can group by (a,b) after normalization.
Thus algorithm:
-
Read n, points list.
-
For each unordered pair (i,j) with i<j, compute line through points i and j. But to build set of lines, we need to assign all points that are collinear to the same line. A straightforward way: For each line, we can collect all points that lie on it. However, if we process each pair, we could use union-find? But simpler: Use a map from canonical line to the set of points (or just store indices). For each pair (i,j), compute canonical line representation and add both point indices to the set for that line. At the end, each line will have a set of points (size >=2). That will give us unique lines. But careful: If we add points from different pairs, eventually the set contains all points on that line. That's fine.
But caution: The canonical representation must be unique for each line. For line defined by two points (x1,y1) and (x2,y2), we can compute parameters:
If dx = x2 - x1, dy = y2 - y1.
Line equation can be represented as:
dy * (x - x1) - dx * (y - y1) = 0 => dyx - dxy + (dxy1 - dyx1) = 0.
So (a,b,c) = (dy, -dx, dxy1 - dyx1). But this representation is not normalized: (a,b,c) could have common factor and sign. Also for vertical lines, dx=0, then a=dy, b=0, c = 0y1 - dyx1? Actually if dx=0, then line x = x1. Equation: 1x + 0y - x1 = 0. Using above: dy * x - 0y + (0y1 - dyx1) = dyx - dyx1 = dy(x - x1) = 0, so a=dy, b=0, c = -dy*x1. Divide by gcd(dy,0)=|dy|, and adjust sign. But better to have a normalized representation.
We can compute the line in the form: ax + by + c = 0, where a,b,c are integers, and we enforce normalization: either:
- Let a and b be reduced so that gcd(a,b) = 1 (and we also need to consider c? Actually we need uniqueness for the line; same line can have multiple c values if we scale a,b,c. So we need to scale so that a,b,c are integers, with a,b relatively prime, and also perhaps impose a sign condition (e.g., a > 0, or a==0 and b > 0). However, note that c must also be consistent: if we scale a,b,c by -1, it's the same line. So we can choose sign such that the first non-zero among a and b is positive. Additionally, we should reduce gcd of (a,b,c) as well? Actually if we scale the equation by a factor, the line remains the same. So we need to normalize the triple up to a scalar factor. So we can compute (a,b,c) from two points, then reduce by gcd(|a|,|b|,|c|) to simplest integer coefficients, and then enforce sign convention. However, note that if a and b are both zero, that can't happen because line defined by two distinct points cannot have a=b=0. So it's fine.
But careful: The triple (a,b,c) and (ka,kb,kc) represent the same line for any non-zero k. So we need to pick a canonical scaling. Typically: compute a, b, c as integers (like a = y1 - y2, b = x2 - x1, c = x1y2 - x2y1). That's another common representation: line through (x1,y1) and (x2,y2): (y2 - y1)x + (x1 - x2)y + (x2y1 - x1y2) = 0. Check: Using determinant: |x y 1; x1 y1 1; x2 y2 1| = 0 => (y1 - y2)x + (x2 - x1)y + (x1y2 - x2y1)=0. So a = y1 - y2, b = x2 - x1, c = x1y2 - x2y1. This also works.
We can compute a = y1 - y2, b = x2 - x1, c = x1y2 - x2y1.
Then normalize: let g = gcd(|a|,|b|,|c|). If g > 0, divide a,b,c by g. Then enforce sign convention: if a < 0 or (a == 0 and b < 0) then multiply a,b,c by -1.
This yields a unique representation for each line.
But note: There is a subtlety: When we reduce by gcd of all three, we might lose information about whether c is multiple of something? Actually it's fine because the line equation is homogeneous? Wait, the line equation is ax+by+c=0, it's not homogeneous because c is constant term. Scaling by non-zero factor doesn't change the line. So indeed we can reduce by gcd of (a,b,c) to get a primitive triple. However, consider the case where a and b are not both zero, but c could be zero. Reducing by gcd(|a|,|b|,|c|) works because if c=0, gcd may be gcd(|a|,|b|). That's fine.
Thus we can use this representation as key for a line.
But we need to be careful: When computing the line from two points, there might be integer overflow? Coordinates up to 1e4, differences up to 2e4, multiplications up to 1e8, which fits within 32-bit signed int? 1e4*1e4=1e8, plus sign, fits in 32-bit (2e9). gcd computation with up to 1e8 numbers is fine.
n=50, total pairs 1225, so building map is fine.
After building map: For each line representation key, we store maybe the set of points or just that it exists. We need later to list all distinct lines. So we can just collect keys (the normalized triple) and also we need to know the direction (a,b) for grouping parallel lines. However, note: After normalization, (a,b) is also normalized and can be used for direction. Two lines are parallel if their (a,b) are proportional (i.e., (a1,b1) = k*(a2,b2)). Since we have normalized (a,b) by making them relatively prime (via dividing by gcd of all three), but note that gcd(a,b) may not be 1 necessarily? Actually after dividing by gcd(a,b,c), we guarantee that gcd(|a|,|b|,|c|)=1, but this does not guarantee that gcd(a,b)=1, because c could share a factor with a and b. Example: a=2,b=2,c=0 -> gcd(2,2,0)=2, dividing gives a=1,b=1,c=0. So gcd(a,b)=1. But consider a=2,b=4,c=2 -> gcd(2,4,2)=2 -> a=1,b=2,c=1. Then gcd(1,2)=1. So indeed after dividing by overall gcd, the triple is primitive overall, but could a and b still have common factor? If a and b had common factor d >1, then that factor would also divide (d divides a,b), and because c could be anything, but we divided by gcd(a,b,c). If d divides both a and b, then d divides gcd(a,b,c) as well (since gcd includes a and b). So after division, a and b become a/d and b/d, which are coprime? They could still have common factor if d was not the full gcd? Actually if a and b share a factor d, then that d is a divisor of both a and b, thus also a divisor of gcd(a,b). Since gcd(a,b,c) includes gcd(a,b) as a factor? Not necessarily: Suppose a=2,b=2,c=1. gcd(2,2,1)=1. So a and b share factor 2 but overall gcd is 1 because c is 1. So after not dividing by 2 (since gcd=1), we have a=2,b=2,c=1. Then a and b are not coprime. So indeed after normalizing by overall gcd, we may have lines where a and b have a common factor > 1, because c breaks the common factor. That would affect direction grouping: direction is given by the ratio a:b (or the normalized direction vector). Two lines are parallel if (a1,b1) is a scalar multiple of (a2,b2). But if (a,b) are not reduced to coprime pair, we might have two lines with same direction but with different (a,b) representations that are not scalar multiples? Actually if they are parallel, their (a,b) should be proportional. So to group by direction, we should reduce the direction vector (a,b) to a normalized form independent of c. So we should compute a direction tuple (A,B) normalized such that gcd(|A|,|B|)=1, and sign convention (e.g., A >0 or A==0 and B >0). Then we can group lines by that normalized direction.
Thus for each line, we can compute its normalized direction (a_dir, b_dir) = (a,b) reduced by gcd(|a|,|b|) and sign normalization. But note that from our line representation, (a,b) might not be primitive. However, we can compute direction separately.
Alternative: Represent lines by slope-intercept as rational numbers, but dealing with vertical lines is easier with direction vectors.
Simplify: For each line we compute two normalized things:
-
For uniqueness of line (including offset): use normalized (a,b,c) as described (reducing by gcd of all three and sign normalization). This ensures each line gets a unique key.
-
For direction grouping: compute (dx, dy) from the two points: (x2-x1, y2-y1) and normalize it to a canonical direction vector (dx0, dy0) where gcd(|dx|,|dy|)=1, and sign normalized (dx>0 or dx==0 and dy>0). This direction is independent of which pair we used because all points on the line have direction proportional. So we can compute direction for each line from its points set: we can take any two points on the line and compute direction.
But we already have the line representation; we can also compute direction from (a,b) because the line's normal vector is (a,b). Actually ax+by+c=0, the line's normal vector is (a,b). The direction vector is perpendicular to normal: (b, -a) or (-b, a). So the direction's slope is -a/b if b!=0. So the direction vector can be (b, -a) (since dot((a,b),(b,-a)) = ab + b(-a) =0). So if we take (b, -a) as direction, its ratio is the same as any direction vector along the line. However, note that if we normalized (a,b,c) by overall gcd, the direction vector (b,-a) may not be primitive. But we can compute normalized direction by reducing (b, -a) by its own gcd and sign. That would be equivalent to reducing (a,b) by its gcd and then taking direction. But maybe simpler: compute direction from points.
Given we already have a set of points on the line (maybe all points on that line), we can pick two representative points from that set to compute direction. But we might not need to store points; we could compute direction from (a,b) as follows: compute g = gcd(|a|,|b|). Then (a/g, b/g) is the reduced normal vector. But direction is perpendicular: (b/g, -a/g). But we can just use the reduced normal (a/g, b/g) to identify direction because lines with same direction have normals that are also proportional (since normal direction also indicates slope). Actually two lines are parallel if their normals are proportional. So we can group by reduced normal (a0,b0) where (a0,b0) = (a,b) divided by gcd(a,b), and then sign normalized. But careful: The normal (a,b) might be (0,0) but not possible. So yes, parallel lines have proportional normals. So we can compute a reduced normal vector for each line by taking (a,b) from the line equation (after overall normalization?) but as we noted, (a,b) might not be reduced to coprime; but we can compute (a',b') = (a,b) divided by gcd(|a|,|b|) and apply sign normalization. This yields a unique direction (normal direction) for each line. Two lines are parallel if their reduced normals are equal (since we normalized sign). However, careful: If two lines have normals (a1,b1) and (a2,b2) that are proportional, after reduction they become the same tuple. So indeed we can group by this reduced normal.
But what about the case where (a,b) from overall normalized triple might have a common factor? e.g., line representation (a,b,c) = (2,2,1). gcd(a,b)=2, so reduced normal would be (1,1). That's fine. So we can compute reduced normal separately.
Thus algorithm steps:
- Initialize a dictionary lines_map = {} that maps from a canonical line key (norm_a, norm_b, norm_c) to something (maybe a list of points or just a flag). But we need to later get list of lines to compute parallel groups. So we can just store that line's direction info. So we can, when we encounter a new line (i.e., when we add a point pair that leads to creating a line if not already present), we compute its normalized representation and also compute its direction key (dir_a, dir_b). And later we will collect all lines.
But careful: Since we are processing pairs, many pairs will map to same line. We can just, for each pair, compute the normalized line representation and then insert into a set of lines (like set of triples). After processing all pairs, we have the set of unique lines. However, there is a potential issue: If three points are collinear, many pairs will produce the same line representation. That's fine; set will deduplicate. So we can just compute normalized representation for each pair and insert into a set. But we need to ensure that the representation is consistent across all pairs on the same line. If our normalization (using a,b,c from two points and then reducing by overall gcd) yields the same triple for any two points on the same line? Let's test.
Suppose we have line with points (0,0) and (1,2). Compute a = y1-y2 = 0-2 = -2, b = x2-x1 = 1-0 = 1, c = x1y2 - x2y1 = 02 - 10 = 0. So triple = (-2,1,0). Normalize: gcd(2,1,0)=1? gcd(2,1,0) = gcd(gcd(2,1),0)=gcd(1,0)=1. So triple = (-2,1,0). Sign normalization: if a < 0 then multiply by -1: (2,-1,0). So key = (2,-1,0).
Now take points (2,4) on same line. Compute with (0,0) and (2,4): a = 0-4 = -4, b = 2-0 = 2, c = 04 - 20 = 0. Triple = (-4,2,0). gcd(4,2,0)=2 -> (-2,1,0) after division? Actually divide by 2: (-2,1,0). Sign: a<0 -> multiply -1 -> (2,-1,0). Same key. Good.
Now take points (1,2) and (2,4): a = 2-4? Actually y1-y2 = 2-4 = -2, b = 2-1 = 1, c = 14 - 22 = 4-4=0. Triple = (-2,1,0) -> (2,-1,0). So consistent.
Now consider a line not through origin: points (0,1) and (1,3). a = 1-3 = -2, b = 1-0=1, c = 03 - 11 = -1. Triple = (-2,1,-1). gcd(2,1,1)=1 -> (-2,1,-1). Sign: a<0 -> (2,-1,1). So key = (2,-1,1).
Now points (2,5) also on line? Check slope: (1,2) from (0,1) to (2,5): slope 4/2=2, yes. Using (0,1) and (2,5): a = 1-5 = -4, b = 2-0=2, c = 05 - 21 = -2. gcd(4,2,2)=2 -> divide: (-2,1,-1). Sign: a<0 -> (2,-1,1). Same.
So indeed the normalization yields a unique representation irrespective of which two points on the line we use.
But there is a corner: If the line's points yield a triple where after reduction, (a,b,c) are primitive, but the sign normalization based on a (or first non-zero) might still be unique. However, we must ensure that the representation does not depend on the order of points. The sign of a,b,c from formula depends on point order. But after we multiply by -1 if a<0 (or if a==0 and b<0), we get a canonical sign. But we must be careful: Suppose the line yields triple where after reduction a = 0, b positive? Actually sign normalization: if a < 0, multiply by -1; if a == 0 and b < 0, multiply by -1; (and if a == 0 and b == 0, impossible). That ensures a >=0, and if a==0 then b>0. That should be consistent because if we flip points, we might get a different sign before normalization, but after normalization we should get the same. Let's test with vertical line: points (2,0) and (2,3). a = y1-y2 = 0-3 = -3, b = x2-x1 = 2-2 = 0, c = x1y2 - x2y1 = 23 - 20 = 6. Triple = (-3,0,6). gcd(3,0,6) = 3 -> (-1,0,2). Sign: a<0 -> multiply by -1: (1,0,-2). So key = (1,0,-2). Another pair: (2,1) and (2,4): a = 1-4=-3, b=0, c=24-21=8-2=6 => (-3,0,6) -> same. Good.
What about line where a=0 after reduction? e.g., horizontal line: points (0,1), (3,1). a = 1-1=0, b = 3-0=3, c = 01 - 31 = -3. Triple = (0,3,-3). gcd(0,3,3)=3 -> (0,1,-1). Sign: a==0 and b>0? b=1>0 so no sign change: (0,1,-1). Another pair: (1,1),(4,1): a=0, b=3, c=11 -41=1-4=-3 -> (0,3,-3)-> (0,1,-1). Good.
So the method works.
Thus we can compute for each pair the normalized triple and insert into a set. After processing all pairs, we have a set of triples representing all distinct lines. Let m = size of set.
Now we need to count pairs of lines that intersect. We can compute total pairs = m*(m-1)//2.
Then subtract number of pairs that are parallel but distinct.
To count parallel pairs, we need to group lines by direction. For each line, compute its normalized direction. One way: from the normalized triple (a,b,c), compute the direction reduced normal: Let d = gcd(|a|,|b|). If d>0, then (a/d, b/d) after sign normalization (the same sign normalization: ensure first non-zero positive). But note: Our triple already has sign normalization based on a (if a>0 or a==0 and b>0). However, that sign normalization ensures a>=0 and if a==0 then b>0. So then (a,b) itself is already normalized for direction? Not exactly: For parallel lines, they share the same normal direction up to scaling. Since we have already normalized sign for the line triple, but the scaling factor for (a,b) might still be not reduced relative to each other if we only reduced by overall gcd including c. As we saw earlier, (a,b) could be (2,2) for one line and (1,1) for another line? Could that happen? Let's see: Suppose we have two distinct parallel lines. For line1: points maybe (0,0) and (1,1): a=0-1=-1, b=1-0=1, c=01 -10=0 -> after normalization: a<0 -> multiply: (1,-1,0). So (a,b)=(1,-1) after sign normalization? But sign normalization gave a>0, so (1,-1). But for line2: points (0,1) and (1,2): a=1-2=-1, b=1-0=1, c=02 -11 = -1 -> after gcd? a,b,c = (-1,1,-1) -> gcd=1? Actually gcd(1,1,1)=1? Wait absolute: 1,1,1 gcd=1 -> (-1,1,-1) -> sign: a<0 -> (1,-1,1). So (a,b)=(1,-1). So they have same (a,b) after line normalization because both have (a,b) = (1,-1). That's fine.
But consider line1: points (0,0) and (2,2): a=0-2=-2, b=2-0=2, c=02 -20 =0 -> triple (-2,2,0) -> gcd(2,2,0)=2 -> (-1,1,0) -> sign: a<0 -> (1,-1,0). So (a,b)=(1,-1). So again same.
Now consider line2: points (0,1) and (2,3): a=1-3=-2, b=2-0=2, c=03 -21 = -2 -> (-2,2,-2) -> gcd(2,2,2)=2 -> (-1,1,-1) -> sign: a<0 -> (1,-1,1). So (a,b)=(1,-1). So all lines with slope 1 have (a,b) = (1,-1) after our line normalization? That seems consistent because we reduced by overall gcd, and after sign normalization, we got the smallest integer representation where a >0, but note that (a,b) may not be coprime (they are (1,-1) coprime). So indeed they coincide.
But can there be a scenario where two parallel lines yield different (a,b) after this line normalization? Suppose we have two distinct parallel lines that are not coincident. The direction (normal) should be proportional. If we compute (a,b) from each line after reducing by overall gcd, we may get (2,2) for one line and (1,1) for another, if the overall gcd for the first line includes c that shares a factor with a,b? For example, consider line1: points (0,0) and (2,2) we already saw yields (1,-1). That's (1,-1). To get (2,2), we need a line where after normalization, (a,b) = (2,2) but that would imply a and b have a common factor 2, but then the triple (a,b,c) would have overall gcd=1 because c is odd maybe? Let's try to construct a line where a=2,b=2,c something such that gcd(2,2,c)=1. For gcd(2,2,c) to be 1, c must be odd. So if we can have a line with equation 2x+2y+1=0 (i.e., x+y+0.5=0). But does such a line contain two integer points? Points with integer coordinates on line 2x+2y+1=0 => 2(x+y) = -1 -> x+y = -0.5, not integer, so cannot have two integer points because if two integer points lie on a line with integer coefficients, then the constant term must be such that the line equation can be scaled to have integer points? Actually if there are two integer points (x1,y1) and (x2,y2), then plugging into a line equation with integer a,b,c (not necessarily primitive) should satisfy. Given a,b,c integers, the condition for integer points existence is not restrictive? But we have points with integer coordinates. The line equation derived from two integer points will produce a,b,c integers with gcd(a,b,c) maybe >0? Actually from two integer points, a = y1-y2, b = x2-x1, c = x1y2 - x2y1. These are all integers. So the triple (a,b,c) is integer. The overall gcd of (a,b,c) could be any number. After reducing by gcd, we get primitive triple. So after normalization, the triple is primitive (gcd=1). In that triple, (a,b) may still have common factor if the gcd of (a,b) is >1 but the overall gcd including c is 1, meaning c is not divisible by that common factor. So it's possible to have a primitive triple where a and b share a common factor >1. For example, consider points (0,0) and (2,0). Then a = 0-0=0, b = 2-0=2, c = 00 - 20 = 0. Triple (0,2,0). gcd(0,2,0)=2, after dividing we get (0,1,0). So not that.
We need an example with a,b having common factor but c not sharing that factor. Let's search: Points: (0,0) and (2,4). Then a = 0-4 = -4, b = 2-0 = 2, c = 04 - 20 = 0. Triple (-4,2,0). gcd(4,2,0)=2 -> (-2,1,0) -> sign: (2,-1,0). So (a,b) becomes (2,-1) which is coprime. So not.
To get a and b with gcd>1, we need the line such that the differences have a common factor, but also the constant term c is such that overall gcd is 1 (so c is not divisible by that factor). This would require that the two points are such that (dx, dy) have a common factor d>1, but the line does not pass through the origin? Actually if we have two points, we can compute a,b,c. a = -dy, b = dx, c = - (a x1 + b y1)? Actually from formula c = x1y2 - x2y1. Suppose dx and dy have common factor d. Write dx = d * dx', dy = d * dy' with gcd(dx', dy')=1. Then a = -ddy', b = ddx', c = x1y2 - x2y1. Does c necessarily have factor d? Let's check: x1y2 - x2y1 = x1*(y1+dy) - (x1+dx)y1 = x1y1 + x1dy - x1y1 - dxy1 = x1dy - dxy1 = d(x1dy' - dx'y1). So c = d(x1dy' - dx'y1). So c is also divisible by d. Indeed from algebra, x1y2 - x2y1 = x1(y1+dy) - (x1+dx)y1 = x1y1 + x1dy - x1y1 - dxy1 = x1dy - dxy1. Since dx and dy have factor d, both terms have factor d, so c is divisible by d. So for any two integer points, if dx and dy share a common factor d, then a,b,c all share that factor? a = -dy, b = dx, c = x1dy - dxy1, all divisible by d. So the triple (a,b,c) has gcd at least d. So after dividing by overall gcd, which will be at least d, the resulting a and b will be reduced: a' = -dy/d, b' = dx/d, which are coprime because dx/d and dy/d are coprime (since we divided by the common factor). So indeed, for any line determined by two integer points, the normalized triple (after dividing by gcd(a,b,c)) will have a and b coprime. Because gcd(a,b,c) includes the common factor of dx and dy. So after reduction, a and b become coprime. Let's verify: Suppose points (0,1) and (2,5). dx=2, dy=4, d=2. a = -4, b = 2, c = 05 - 2*1 = -2. So triple (-4,2,-2). gcd(4,2,2)=2 -> (-2,1,-1) -> sign: (2,-1,1). a,b = (2,-1) are coprime? gcd(2,1)=1. Yes.
What about if the points are such that dx=0? Then dy maybe nonzero; then a = -dy, b=0, c = -dxy1? Actually if dx=0, then b=0, a = -dy, c = x1dy - 0y1 = x1dy. gcd of (|dy|,0,|x1dy|) = |dy|? Because gcd(dy,0,|x1dy|) = |dy|gcd(1,0,|x1|) maybe? Actually gcd(dy,0) = |dy|. And x1dy also divisible by dy, so overall gcd = |dy|. After dividing, a = -dy/|dy| with sign? Actually we get a = -dy/|dy| = -sign(dy), b=0, c = x1dy/|dy| = sign(dy)x1. After sign normalization: if a<0? That yields maybe a positive after sign? For vertical line, after normalization, a = 1, b=0, c = -x1 (depending on sign). So (a,b) = (1,0) which are coprime. Similarly for horizontal lines: dy=0, then a=0, b=dx, gcd = |dx|, after normalization a=0, b=1, c = -y1? Actually need to compute: dy=0, dx non-zero. a = 0, b = dx, c = -dxy1. gcd = |dx|, after division a=0, b=dx/|dx| = ±1, c = - (dx/|dx|) y1. Sign normalization: a==0 and b<0? We enforce b>0. So b=1, c = -sign(dx)*y1? Actually if dx positive, b=1, c = -y1; if dx negative, after division b = -1, then we multiply by -1 to make b positive? Wait sign normalization: if a == 0 and b < 0, multiply entire triple by -1. So if after division b = -1, we multiply to get a=0,b=1,c = y1? Something like that. So (a,b) = (0,1) coprime.
Thus, for any line defined by two integer points, after normalizing the triple (a,b,c) by gcd(a,b,c) and applying sign rule, the resulting (a,b) will be coprime. Because the gcd(a,b,c) includes any common factor of a and b. So we can use (a,b) from this triple directly as the normalized direction (normal vector). Since they are coprime, they represent a unique direction. Indeed, two lines are parallel if and only if their normalized (a,b) from this representation are equal (since both are primitive and sign-normalized). So we can just use the (a,b) part of the line's normalized triple as the direction key. That will be consistent across all lines: each line's (a,b) are primitive and sign-normalized. So we can group by (a,b).
Thus we can compute:
For each pair of points (i,j):
a = y_i - y_j
b = x_j - x_i
c = x_iy_j - x_jy_i
Compute g = gcd(|a|, |b|, |c|)
a //= g; b //= g; c //= g
if a < 0 or (a == 0 and b < 0):
a = -a; b = -b; c = -c
Now triple (a,b,c) is canonical.
Insert into set lines_set (maybe store as tuple). At the same time, we can also track the direction (a,b) which is already primitive.
But careful: What about lines that are defined by more than 2 points? The above method will produce same triple for any pair, so set will deduplicate. So after processing all pairs, we have unique lines.
However, there is a subtlety: If we process each pair and compute normalized triple and insert into set, we will end up with each line represented exactly once. Good.
Now, we have m = len(lines_set). Then we need to count pairs that are parallel. To do that, we can create a dictionary direction_count: for each line's (a,b) (since (a,b) is primitive and sign-normalized), increment count. Then number of parallel pairs = sum over each direction: C(cnt, 2). Then answer = C(m,2) - sum(C(cnt,2)).
But wait: Are there any pairs of lines that are the same? No, because we have unique lines. So that seems correct.
Check with examples:
Example 1:
4 points:
(0,0), (1,1), (0,3), (1,2)
Let's compute lines manually? We'll trust output: 14.
Compute m = number of distinct lines? Could be many. Let's quickly enumerate: Points: A(0,0), B(1,1), C(0,3), D(1,2). Points are not all collinear. We have lines defined by each pair:
Pairs:
AB: (0,0)-(1,1): line slope 1. Equation? We'll compute later.
AC: (0,0)-(0,3): vertical x=0.
AD: (0,0)-(1,2): slope 2.
BC: (1,1)-(0,3): slope? (3-1)/(0-1) = 2/(-1) = -2.
BD: (1,1)-(1,2): vertical x=1.
CD: (0,3)-(1,2): slope -1.
So we have lines:
1: AB (through (0,0),(1,1)): also maybe other points? No others.
2: AC: x=0
3: AD: slope 2 through (0,0)
4: BC: slope -2 through (1,1) and (0,3)
5: BD: x=1
6: CD: slope -1 through (0,3),(1,2)
That's 6 lines. So m=6. Total pairs = C(6,2)=15.
Now parallel pairs: Check which lines are parallel.
- AB slope 1, CD slope -1 (not parallel).
- AD slope 2, BC slope -2 (not parallel).
- vertical lines: x=0 and x=1 are parallel. So one parallel pair: (AC, BD).
Any other parallels? AB is slope 1, no other slope 1. AD slope 2, no other slope 2. BC slope -2, no other. CD slope -1, none. So only one parallel pair. So answer = 15 - 1 = 14. Matches.
Example 2:
4 points: (0,0), (0,2), (0,4), (2,0)
Points: A(0,0), B(0,2), C(0,4), D(2,0)
Pairs:
AB: x=0 (vertical)
AC: x=0 (same)
AD: (0,0)-(2,0) horizontal y=0
BC: x=0 (same)
BD: (0,2)-(2,0) slope? (0-2)/(2-0) = -2/2 = -1.
CD: (0,4)-(2,0) slope (0-4)/(2-0) = -4/2 = -2.
Distinct lines:
- Line L1: x=0 (through A,B,C) (vertical)
- L2: y=0 (horizontal) through A and D? Actually A and D: (0,0) and (2,0) => y=0
- L3: line through B and D: slope -1
- L4: line through C and D: slope -2
Also what about A and B? Already L1. A and C? L1. B and C? L1. So total lines m=4.
Total pairs = C(4,2)=6.
Parallel pairs: Which are parallel? L1 is vertical; L2 is horizontal; not parallel. L3 and L4 have slopes -1 and -2, not parallel. So no parallel lines? But answer is 6. So 6 - 0 = 6. Matches.
Example 3:
3 points: (-1,-1), (1,0), (3,1)
Check collinearity: slopes: (0 - (-1))/(1 - (-1)) = 1/2 = 0.5; (1-0)/(3-1)=1/2=0.5, so all collinear. So only one line. m=1. Total pairs = 0. Answer 0. Matches.
Thus method works.
But we need to consider potential pitfalls: When computing gcd of three numbers where one might be 0, Python's math.gcd can handle two numbers, but we need gcd of three: gcd(gcd(a,b),c). But math.gcd can take two arguments. We'll compute g = math.gcd(abs(a), abs(b)); then g = math.gcd(g, abs(c)). However, note that math.gcd(0,0) is 0. But a,b cannot both be 0 because points distinct? Actually if points are distinct, (dx,dy) not both zero, so a and b cannot both be zero? a = y1-y2, b = x2-x1; if both zero, then y1=y2 and x1=x2, which can't happen. So at least one of a,b non-zero. So g after gcd(abs(a),abs(b)) will be at least 1. Then gcd(g, abs(c)) could be >0; but if c=0, then gcd(g,0)=g, fine.
But we need to be careful with sign normalization: after dividing by g, we have a,b,c integers. Then apply sign condition: if a < 0 or (a == 0 and b < 0), multiply all by -1.
One nuance: What about lines where after dividing, a = 0 and b > 0 but c may be negative? That's fine. For horizontal lines, we get (0,1, c) with c negative if y is positive? For points (0,0) and (1,0): a=0-0=0, b=1-0=1, c=00-10=0 -> (0,1,0). For points (0,1) and (2,1): a=1-1=0, b=2-0=2, c=01-21=-2 -> after dividing by gcd(0,2,2)=2: (0,1,-1). sign: a=0, b=1>0, so keep -> (0,1,-1). So line y=1 has representation (0,1,-1). That's fine.
Now, compute direction key as (a,b) because they are primitive after normalization. So grouping by (a,b) will count parallel lines.
But is it possible that two lines have same (a,b) but are actually the same line? They would then have the same (a,b,c). In our set, we store lines by (a,b,c). So distinct lines with same (a,b) are distinct because c differs. So that's fine.
Now we can implement.
Edge Cases:
-
n=2: two points define one line. m=1, answer=0. Works.
-
All points collinear: m=1, answer 0.
-
Many points, many lines. Up to 1225 lines.
Complexity: O(n^2 * (log coordinate)) for gcd. Plus O(m^2) but we are using grouping, so O(m) after building set. So fine.
Potential issues: Python's set of tuples of integers works. We must ensure that we use Python's built-in math.gcd.
Let's test on random small cases by brute force to verify.
But note: The problem statement says "If there are more than two poles lying on the same line, they are connected by a single common wire." So we have correctly handled that: each line is a wire.
Now, is there any subtlety about intersecting? Two wires intersect if their lines intersect at a point. This includes the case where intersection point may be at a pole? That's fine; any intersection counts. Also, what about lines that are the same? Not counted. So the simple count stands.
Thus solution: count distinct lines, then subtract number of parallel pairs.
But wait: In the first example, we got 14. That matches. So fine.
But we need to double-check that we didn't miss any line: In the first example, we considered lines from each pair. That covers all lines because each line is defined by at least two points. However, if three points collinear, there is only one line, but we might generate it multiple times, but set ensures unique.
Now think about scenario where there are many collinear points, but also other points. The number of lines is less than C(n,2). Our method will still produce set.
Now is there any case where a line can be defined by points but not by a pair? Actually any line containing at least two points is defined by any pair from its point set, so our enumeration of all pairs will include it.
Thus algorithm is correct.
Now I will produce the Python code.
Implementation steps:
-
import sys, math.
-
Read n.
-
points = [tuple(map(int, sys.stdin.readline().split())) for _ in range(n)]
-
lines_set = set() # store (a,b,c)
For i in range(n):
for j in range(i+1, n):
x1,y1 = points[i]
x2,y2 = points[j]
a = y1 - y2
b = x2 - x1
c = x1y2 - x2y1
g = math.gcd(abs(a), abs(b))
g = math.gcd(g, abs(c))
if g != 0: # g > 0
a //= g; b //= g; c //= g
# sign normalization
if a < 0 or (a == 0 and b < 0):
a = -a; b = -b; c = -c
lines_set.add((a,b,c))
-
m = len(lines_set)
-
direction_count = {}
For each (a,b,c) in lines_set:
dir_key = (a,b) # since a,b are primitive
direction_count[dir_key] = direction_count.get(dir_key, 0) + 1
-
total_pairs = m*(m-1)//2
-
parallel_pairs = sum( cnt*(cnt-1)//2 for cnt in direction_count.values() )
-
answer = total_pairs - parallel_pairs
-
Print answer.
Double-check sign normalization: For a=0 and b<0 after division, we flip sign. That yields a=0,b>0. So all lines with a=0 have b positive. So direction key for horizontal lines: (0,1). For vertical lines: we have a>0 because a can be positive? Let's test vertical: points (2,0),(2,3): a = 0-3 = -3, b = 2-2=0, c = 23 -20 =6. After dividing by gcd=3: (-1,0,2). Then sign: a<0 -> multiply -1: (1,0,-2). So (a,b)=(1,0). Good.
What about line with a negative and b positive after division? If after division a is negative, we flip sign. So a becomes positive, b becomes negative if b was positive? Example: line slope positive? Let's test line through (0,0) and (1,2): a = 0-2 = -2, b = 1-0 =1, c=0. gcd=1: (-2,1,0). a<0 -> flip: (2,-1,0). So (a,b) = (2,-1). So direction key is (2,-1). That is a primitive vector. But is this consistent across all lines with slope 2? For another line with slope 2 but offset, say through (0,1) and (1,3): a = 1-3=-2, b=1-0=1, c= -1? Actually c = 03 -11 = -1. gcd=1: (-2,1,-1) -> flip: (2,-1,1). So same (a,b)=(2,-1). So direction key is (2,-1). So all lines with slope 2 have (a,b) = (2,-1). Are there lines with slope 2 that could have (a,b) = (1,-0.5)? No, integers only. So fine.
What about lines with slope -2? Let's compute (0,0) and (1,-2) but that's not integer? Actually slope -2: points (0,0) and (1,-2): a = 0 - (-2)=2, b = 1-0=1, c=0*(-2) - 10=0. After normalization: (2,1,0). a>0 so no flip: (2,1,0). So (a,b) = (2,1). For another line slope -2: (0,1) and (1,-1): a = 1-(-1)=2, b=1-0=1, c=0(-1) - 1*1 = -1. So (2,1,-1). So (a,b)=(2,1). So indeed lines with slope -2 have (2,1). While lines with slope 2 have (2,-1). So direction keys differ, good.
Now, are there any lines with direction keys that are essentially the same direction but with opposite sign? Our sign normalization ensures a>0, so direction (a,b) will always have a>0, or a==0 and b>0. So directions that are opposite (like ( -2,1) would be normalized to (2,-1) after sign flip). But note that the direction vector along the line could be (b,-a) which for (a,b)=(2,-1) gives direction (-1,-2) or something? Actually (b,-a) = (-1, -2) which is opposite to (1,2) maybe? But that's okay because we care about normal vectors being parallel. For parallel lines, their normals are either exactly same (after sign normalization) or opposite? But sign normalization ensures normals are oriented to have a>0 (or a==0,b>0). So opposite normals (like ( -2,1) and (2,-1)) both get normalized to (2,-1)? Wait, consider a line with normal ( -2,1). After normalization we would have (2,-1) because we flip sign if a<0. So both normals become the same. So lines with normals that are opposite are considered same direction (since lines are undirected, parallel lines can have normals pointing in opposite directions but they are still parallel). Our sign normalization ensures they are merged. So grouping by (a,b) after sign normalization correctly groups parallel lines.
Thus approach is correct.
But wait, there is one more nuance: What about lines that are coincident? They would have same (a,b,c), so they are not distinct. So not counted.
Now confirm for a case where there are many parallel lines: e.g., 5 vertical lines. They all have (a,b) = (1,0). So direction_count[1,0] = 5, parallel_pairs = 10. Then total_pairs minus that.
Now, test with random small n and brute force enumeration of all pairs of lines and check intersection by checking if not parallel. But given n small, we could also compute lines by enumerating all subsets of points of size >=2 that are collinear? But our method seems robust.
One more nuance: When we compute line from a pair, we use the triple. But there is a potential issue: If the three numbers have a common divisor after sign normalization? Actually after dividing by gcd, they are primitive as triple. However, if we later group by (a,b) only, note that (a,b) might still have a common factor if the overall gcd was 1 but a and b have a common factor? As argued earlier, this cannot happen because if a and b share a factor d>1, then d divides a and b, and then from c = x1y2 - x2y1, we argued that d also divides c? Is that always true? Let's verify: a = y1-y2, b = x2-x1. If d divides both a and b, then d divides any integer combination. But c = x1y2 - x2y1 = x1*(y1 + a?) Actually we can derive: Since a = y1 - y2, b = x2 - x1. Then c = x1y2 - x2y1. Write y2 = y1 - a, x2 = x1 + b. Then c = x1*(y1 - a) - (x1 + b)y1 = x1y1 - x1a - x1y1 - by1 = - x1a - by1. So c = - (x1a + y1b). Since d divides a and b, d divides x1a + y1*b, so d divides c. So indeed if d divides a and b, then d divides c. Therefore the triple (a,b,c) has d as a common factor. Thus gcd(a,b,c) is at least d. So after dividing by gcd(a,b,c), a and b become coprime. Therefore our assertion holds.
Thus (a,b) from normalized triple are coprime. Good.
Now there is an edge case: When a=0 and b=0? impossible.
Now, one more nuance: We used the triple (a,b,c) = (y1-y2, x2-x1, x1y2 - x2y1). But note that this representation gives a normal vector (a,b). There's also the alternative (y2-y1, x1-x2, x2y1 - x1y2) which is just the negative. Normalization will handle.
Now check if we might get different representations for the same line if we use different formulas? No.
Now we need to consider the possibility of floating point or rational slopes but we are using integer arithmetic, so it's precise.
Thus the solution is ready.
But wait: The problem says "If there are more than two poles lying on the same line, they are connected by a single common wire." This means that each line is a wire only if it contains at least two poles. Our enumeration of all pairs will generate each line exactly once (as a set of points). But is there a scenario where a line contains exactly one point? No, we only consider lines with at least two points. So all good.
Now we can implement.
However, we should double-check if there could be lines that contain multiple points but are not captured by any pair? Actually any line with at least two points is captured by any pair among those points.
Thus code is done.
Now test on examples.
We'll quickly simulate:
Example 1:
points = [(0,0),(1,1),(0,3),(1,2)]
Compute pairs:
(0,0)-(1,1): a=-1,b=1,c=0 -> after gcd: a=-1,b=1,c=0 -> sign: a<0 -> (1,-1,0)
(0,0)-(0,3): a=-3,b=0,c=0 -> gcd=3 -> (-1,0,0) -> sign -> (1,0,0)
(0,0)-(1,2): a=-2,b=1,c=0 -> gcd=1 -> (-2,1,0) -> sign -> (2,-1,0)
(1,1)-(0,3): a=1-3=-2? Actually y1-y2 = 1-3=-2, b=0-1=-1? Wait x2-x1: 0-1 = -1, c = x1y2 - x2y1 = 13 - 01 = 3. So a=-2,b=-1,c=3. gcd(2,1,3)=1? Actually abs:2,1,3 gcd=1 -> (-2,-1,3). Sign: a<0 -> multiply -1 -> (2,1,-3). So line (2,1,-3). Check if that matches our earlier manual? For line BC slope? (1,1)-(0,3): slope (3-1)/(0-1) = 2/ -1 = -2. So normal (2,1) fits. Good.
(1,1)-(1,2): a=1-2=-1, b=1-1=0, c=12 - 11 = 2-1=1 -> (-1,0,1) gcd=1 -> sign -> (1,0,-1)
(0,3)-(1,2): a=3-2=1, b=1-0=1, c=02 - 13 = -3 -> (1,1,-3) gcd=1, a>0 -> (1,1,-3)
Set of lines: (1,-1,0), (1,0,0), (2,-1,0), (2,1,-3), (1,0,-1), (1,1,-3). That's 6.
Now directions: (1,-1): count1? Actually (1,-1) appears for lines (1,-1,0) and? (2,-1,0) has (2,-1) not (1,-1). (1,0) appears for (1,0,0) and (1,0,-1): count 2. (2,-1): only (2,-1,0) count1. (2,1): (2,1,-3) count1. (1,1): (1,1,-3) count1. So parallel pairs = C(2,2)=1. total pairs = 15, answer=14. OK.
Example 2: points: (0,0),(0,2),(0,4),(2,0)
Pairs:
(0,0)-(0,2): a=-2,b=0,c=0 -> gcd=2 -> (-1,0,0) -> sign -> (1,0,0)
(0,0)-(0,4): a=-4,b=0,c=0 -> gcd=4 -> (-1,0,0) -> (1,0,0) same.
(0,0)-(2,0): a=0,b=2,c=0 -> gcd=2 -> (0,1,0)? Wait compute: a=0-0=0, b=2-0=2, c=00 - 20=0 -> (0,2,0) gcd=2 -> (0,1,0) sign: a=0,b=1>0 -> (0,1,0)
(0,2)-(0,4): a=-2,b=0,c=0? Actually a=2-4=-2,b=0-0=0,c=04 -02=0 -> (1,0,0) after normalization.
(0,2)-(2,0): a=2-0=2? y1-y2 = 2-0=2, b = x2-x1 = 2-0=2, c = x1y2 - x2y1 = 00 - 22 = -4 -> (2,2,-4) gcd=2 -> (1,1,-2) sign: a>0 -> (1,1,-2)
(0,4)-(2,0): a=4-0=4, b=2-0=2, c=00 -24 = -8 -> (4,2,-8) gcd=2 -> (2,1,-4) sign: a>0 -> (2,1,-4)
Now set: (1,0,0), (0,1,0), (1,1,-2), (2,1,-4). That's 4 lines. Directions: (1,0): count1? Actually (1,0,0) has (1,0); (0,1,0) has (0,1); (1,1,-2) has (1,1); (2,1,-4) has (2,1) which is not same as (1,1). So no direction with count >1, parallel_pairs=0, total=6, answer=6.
Example 3: all collinear. Points: (-1,-1),(1,0),(3,1). Compute pairs:
(-1,-1)-(1,0): a = -1-0 = -1, b = 1-(-1)=2, c = (-1)0 - 1(-1) = 0+1=1 -> (-1,2,1) gcd=1 -> sign -> (1,-2,-1)? Wait sign: a<0 -> multiply -1: (1,-2,-1) Actually we must do: after dividing: (-1,2,1), a<0 -> multiply -1: (1,-2,-1). So line1: (1,-2,-1)
(-1,-1)-(3,1): a = -1-1 = -2, b = 3-(-1)=4, c = (-1)1 - 3(-1)= -1 +3=2 -> (-2,4,2) gcd=2 -> (-1,2,1) -> sign -> (1,-2,-1) same.
(1,0)-(3,1): a = 0-1=-1, b = 3-1=2, c = 11 -30 =1 -> (-1,2,1) -> (1,-2,-1). So set size = 1. m=1. answer=0.
All good.
Thus solution is validated.
Now we need to output a single integer.
But wait, the problem says: "If there are more than two poles lying on the same line, they are connected by a single common wire." That means that each line is a wire, but what about lines that contain exactly 2 poles? That's also a wire. So our method includes all lines with >=2 points.
Now one more nuance: Could there be a scenario where the same line appears multiple times due to different pairs of points that are not collinear? No.
Now, potential integer overflow? Not in Python.
Now, time complexity: O(n^2 * log(max coordinate)) = 2500*? Actually n=50, pairs 1225, each gcd O(log(1e4)) trivial.
Thus solution passes.
But there's a catch: The problem title says "This problem is same as the next one, but has smaller constraints." So maybe there is a larger constraints version. For n up to 50, this solution is fine.
Now, finalize code.
But before finalizing, check if there is any other intersection condition: Do wires intersect only if the intersection point is not at a pole? The problem statement: "Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage." It doesn't exclude intersection at a pole. Typically, intersection of lines is considered regardless of whether the point is at a pole. So it counts. In our method, we count any pair of distinct non-parallel lines, which includes those intersecting at a pole. That's correct.
But what about lines that are coincident? They are same wire, not counted.
Thus answer is as described.
Now, I'll produce Python code.
Potential improvements: We could avoid storing the whole set of lines but directly count direction frequencies while ensuring uniqueness. However, it's simpler to build set first.
Implementation details:
-
Use math.gcd.
-
Note: math.gcd(0,0) returns 0. But we handle separately: if g==0 (should not happen because at least one of a,b non-zero, but g could be 0 if a=0,b=0? Not possible. However, if a=0,b=0, points are same, not allowed. So safe.
-
After dividing by g, we have ints.
-
Then sign normalization.
One more nuance: For vertical lines, after dividing, a could be positive, b=0, c negative. That's fine.
Now test with random data.
I'll write a small test harness mentally? Not necessary.
But consider a case where points are many collinear and others. Works.
Now, output.
Thus final answer.
But let me think: Is there any scenario where two distinct lines could be considered not intersecting because they are the same line? No.
Now, confirm that counting parallel pairs via direction_count as described counts all pairs of lines that are parallel, even if they are also intersecting? They don't intersect.
Now, is there any scenario where lines could be considered intersecting if they are skew? No, 2D.
Now, we can finalize.
But wait: Check problem note: In second example, they had three poles collinear and got answer 6. Our method gave 6.
Now, finalize.
Now, think about edge case where there are many points on the same line, but also many other lines, and we might miscount parallel lines if we use (a,b) from normalized triple. Suppose we have two lines that are parallel but not same, and they are both defined by points that are collinear. After normalization, their (a,b) will be same if they are parallel. But could there be a case where two parallel lines have same (a,b) but one of them after normalization ends up with a different (a,b) because of sign normalization? Suppose two parallel lines have normals that are opposite but after sign normalization become same, because we always ensure a>0. So they become same. Good.
Now, what if one line's normal after reduction is (2, -4) but gcd(2,4)=2, so normalized direction would be (1, -2). But our triple normalization would have divided by overall gcd including c. Could that lead to (a,b) not being reduced to (1,-2) but maybe (2,-4) if c is odd? But we argued that if a and b share factor, then c also shares that factor, so overall gcd includes that factor, so after division, they become coprime. So (a,b) will be (1,-2). So fine.
Thus direction keys are consistent.
Now, code:
import sys, math
def main():
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
points = [(int(next(it)), int(next(it))) for _ in range(n)]
lines = set()
for i in range(n):
x1, y1 = points[i]
for j in range(i+1, n):
x2, y2 = points[j]
a = y1 - y2
b = x2 - x1
c = x1 * y2 - x2 * y1
g = math.gcd(abs(a), abs(b))
g = math.gcd(g, abs(c))
if g:
a //= g
b //= g
c //= g
# sign normalization
if a < 0 or (a == 0 and b < 0):
a, b, c = -a, -b, -c
lines.add((a, b, c))
m = len(lines)
dir_cnt = {}
for a, b, c in lines:
key = (a, b)
dir_cnt[key] = dir_cnt.get(key, 0) + 1
total_pairs = m * (m - 1) // 2
parallel_pairs = sum(cnt * (cnt - 1) // 2 for cnt in dir_cnt.values())
ans = total_pairs - parallel_pairs
print(ans)
if __name__ == "__main__":
main()
But note: g could be 0? Only if all a,b,c are zero, which is impossible. So g>0. Actually math.gcd(0,0) returns 0. So if a=0,b=0? Not possible. But suppose a=0,b=0,c!=0? But a and b cannot both be zero. So at least one of a,b non-zero, so gcd(abs(a),abs(b)) is at least 1. Then gcd(g, abs(c)) will be at least 1? It could be 0 if c is 0 and g is 0? But g is at least 1, so gcd(1,0)=1. So g will be >=1. So we can safely divide.
Thus we can just do:
g = math.gcd(abs(a), abs(b))
g = math.gcd(g, abs(c))
a //= g
b //= g
c //= g
If g is 0 (shouldn't happen), we'd get division by zero. To be safe, we can check if g == 0: continue? But not needed.
We could also compute using fractions.Fraction for slope and intercept but integers easier.
Now, final answer.
But we must be careful: In the problem, the map has chimneys, electric poles, wires. But not relevant.
Thus final code is ready.
Now, I'll provide the solution explanation and code.
But before finalizing, check with a tricky case: n=50 random points, but all points on two parallel lines. For instance, 25 points on line y=0, 25 points on line y=1. Then distinct lines: two lines (horizontal lines). m=2, total_pairs=1, parallel_pairs? Both lines are parallel? Horizontal lines y=0 and y=1 are parallel. So parallel_pairs=1. Then answer=0. Is that correct? Yes, they don't intersect. So answer=0. Good.
If points on two intersecting lines, say 25 on y=0, 25 on x=0. Distinct lines: 2, not parallel, total_pairs=1, parallel_pairs=0, answer=1. Intersection at (0,0). Good.
Now, what about more than two lines all through a common point? They all intersect each other, so answer = total_pairs (since none parallel). That is fine.
Now, consider scenario where there are multiple lines with same direction (parallel) but also some lines with other directions. Our formula works.
Thus solution is correct.
Now, provide final answer.
But wait: The problem statement says "If there are more than two poles lying on the same line, they are connected by a single common wire." So we treat each line as a wire. So our method yields number of wires m. However, could there be a scenario where a line contains many points, but we still count only one wire. Our set of lines correctly captures that.
Now, output integer.
Now, let's double-check with a more complex example, maybe with 5 points, all but three collinear? Let's do small check manually.
Points: (0,0), (1,0), (2,0) collinear horizontal; (0,1), (1,1) collinear horizontal but different line; also (1,2) alone? But need at least 2 per line. So lines: line1: y=0 (points 3), line2: y=1 (points 2). Also lines between (0,0) and (0,1): vertical x=0; (1,0)-(1,1): vertical x=1; (2,0)-(1,1) etc. Actually we must consider all pairs. So there will be many lines. We can compute with code.
But conceptually, it's fine.
Now, finalize.
One more nuance: The problem might consider wires as segments? No, it says wires are straight lines infinite in both directions. So intersection of lines is considered.
Thus final solution stands.
Now, I will output the code and explanation. The problem reduces to counting the number of intersecting pairs among all distinct lines that contain at least two given points.
Since each pair of distinct lines either intersect (if they are not parallel) or are parallel (and do not intersect), the answer is
[
\text{total pairs of distinct lines} - \text{number of unordered pairs of parallel lines}.
]
For (n\le 50) we can generate all (\binom{n}{2}) point pairs, compute the unique line determined by each pair, and store a canonical representation of the line.
A line through ((x_1,y_1)) and ((x_2,y_2)) is described by the equation
[
(y_1-y_2)x + (x_2-x_1)y + (x_1y_2 - x_2y_1) = 0.
]
We normalise the triple ((a,b,c)) by dividing by the gcd of (|a|,|b|,|c|) and then enforce a sign convention:
- if (a < 0) or ((a = 0) and (b < 0)), multiply the triple by (-1).
This gives a unique key for each line. After processing all pairs, the set of keys yields the number of distinct lines (m).
For parallel lines, we group by the reduced normal vector ((a,b)) (which is already primitive after the normalisation).
Let (cnt_d) be the number of lines with the same direction (d); then the number of unordered parallel pairs is (\sum_d \binom{cnt_d}{2}).
The final answer is (\binom{m}{2} - \sum_d \binom{cnt_d}{2}).
import sys
import math
def main():
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
points = [(int(next(it)), int(next(it))) for _ in range(n)]
lines = set()
for i in range(n):
x1, y1 = points[i]
for j in range(i + 1, n):
x2, y2 = points[j]
a = y1 - y2
b = x2 - x1
c = x1 * y2 - x2 * y1
g = math.gcd(abs(a), abs(b))
g = math.gcd(g, abs(c))
# g > 0 because the points are distinct
a //= g
b //= g
c //= g
# sign normalization: first non‑zero of (a,b) positive
if a < 0 or (a == 0 and b < 0):
a, b, c = -a, -b, -c
lines.add((a, b, c))
m = len(lines)
dir_cnt = {}
for a, b, c in lines:
key = (a, b)
dir_cnt[key] = dir_cnt.get(key, 0) + 1
total_pairs = m * (m - 1) // 2
parallel_pairs = sum(cnt * (cnt - 1) // 2 for cnt in dir_cnt.values())
print(total_pairs - parallel_pairs)
if __name__ == "__main__":
main()