Editorial for CTU Open Contest 2017 - Go Northwest!


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
  • We need to count the number of ways to select two (not necessarily distinct!) points such that when we connect them by a line, the angle between the line and horizontal axis is 45^\circ
  • How?
  • The coordinates of points can help us to index diagonals to find out how many points lie on the same diagonal
  • The northwest diagonals can be indexed by x+y
  • The northeast diagonals can be indexed by x-y
  • For each diagonal that contains x points, the number of ways to select two points to form a line is x \times (x-1)
  • That leaves us to sum the result over all diagonals and divide it by number of all options N^2
  • Total running time: \mathcal O(N) or \mathcal O(N \log N) depending on the indexing of diagonals

Comments

There are no comments at the moment.