Editorial for COCI '09 Contest 1 #6 Aladin
Submitting an official solution before solving the problem yourself is a bannable offence.
First, we need to find an efficient way of generating the following sequence:
Known formula gives us (where is the integer part of and is the fractional part of ):
Combining (0), (1a) and (1b) we have:
From (2) we see that we can solve (0) if we can solve:
Note that (3) can be interpreted geometrically: How many lattice points are in the triangle if we exclude lattice points on the axis?
Let us examine the following two cases:
There exists a nonnegative integer and integer from such that . We use this and (3) to obtain:
This reduces this case to the following case.
Let the rectangle be labeled . We can easily calculate the number of lattice points in . We are interested in the number of lattice points beneath its diagonal. This can be calculated by subtracting the number of points above the diagonal from the total number of points. The number of points above the diagonal can be found simpler than the number of points beneath it.
Now, by relabeling axes and , we reduce the second case back to the first case. These reductions 1 → 2 → 1 can only be performed a finite number of times because the sum decreases each time we solve the first case. By following this we can compute members of (0) in . Now we just need to store the array in a fast data structure. It turns out using interval/tournament tree gives the total complexity . Enough to solve the task.
Comments