- Decide the key that represents the information needed later.
- Update its count or stored state while scanning the input.
- Use constant-time expected lookups to detect matches or assemble the result.
Code notes
- 78 lines of Python from the credited upstream file abc418_e.py.
- The implementation visibly relies on sequence storage, hash lookup, ordered lookup.
- No explicit loop blocks detected.
Complexity
Expected hash operations are constant time, but the surrounding scan and the number of stored keys determine total work and memory.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
12 3from math import gcd4 5 6def calc_line_passing_through(point1, point2):7 """8 9 Args:10 point1, point2: Points that constitutes a line.11 12 Returns:13 parameters of the line.14 slope : a, b15 intercept: c16 17 Landau notation: O(log(min(a, b, c))).18 19 See:20 https:atcoder.jp/contests/abc248/submissions/3101860821 """22 23 a = point2[1] - point1[1]24 b = point1[0] - point2[0]25 c = -(point1[0] * a + point1[1] * b)26 27 if a < 0:28 a, b, c = -a, -b, -c29 30 if a == 0 and b < 0:31 b, c = -b, -c32 33 g = gcd(gcd(a, b), c)34 a = g35 b = g36 c = g37 38 return (a, b, g)39 40 41def main():42 import sys43 from collections import defaultdict44 45 input = sys.stdin.readline46 47 n = int(input())48 xy = [tuple(map(int, input().split())) for _ in range(n)]49 d = defaultdict(list)50 51 for i in range(n):52 for j in range(i + 1, n):53 a, b, g = calc_line_passing_through(xy[i], xy[j])54 d[(a, b)].append(g)55 56 ans = 057 58 59 60 for _, values in d.items():61 size = len(values)62 ans += size * (size - 1) 63 64 65 duplicates = defaultdict(int)66 67 for value in values:68 duplicates[value] += 169 70 for duplicate in duplicates.values():71 ans -= duplicate * (duplicate - 1) 272 73 print(ans 2)74 75 76if __name__ == "__main__":77 main()78