Editorial for COCI '08 Contest 4 #5 Trezor


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 say that vaults with a common x-coordinate form a column. There are A+B+1 columns and the limits on A and B are relatively small, so we will solve the problem by counting vaults each of the guards sees in a particular column.

Let:

  • a(x) be the total number of vaults in column x seen by the guard at (-A, 0);
  • b(x) be the total number of vaults in column x seen by the guard at (B, 0);
  • ab(x) be the total number of vaults in column x seen by both guards.

Knowing these numbers, we can calculate:

  • The number of super-secure vaults in column x as ab(x);
  • The number of secure vaults in column x as a(x) + b(x) - 2 \cdot ab(x);
  • The number of insecure vaults as L - a(x) - b(x) + ab(x).

How do we calculate the number of vaults in a column? Let f(dx) be the number of vaults the guard can see in the column at distance dx from his position. For some y, the guard can see the vault at (x+dx, y) only if y and dx are relatively prime. If the greatest common divisor between y and dx is greater than 1, then y and dx can be divided by the divisor to get the coordinates of another point blocking the view.

The number of integers relatively prime with dx that are at most L can be calculated by finding the prime factors of dx and using the inclusion-exclusion principle. Take for example L = 15 and dx = 6. Then the number of vaults the guard can see in a column at distance 6 from his position is:

\displaystyle f(6) = \left\lfloor \frac{15}{1} \right\rfloor - \left\lfloor \frac{15}{2} \right\rfloor - \left\lfloor \frac{15}{3} \right\rfloor + \left\lfloor \frac{15}{6} \right\rfloor = 15-7-5+2 = 5

A special case is f(0) = 1, because the guard can only see the vault directly ahead of him in his column.

Now it is obvious that a(x) = f(x-A) and b(x) = f(B-x). It is less obvious that we can calculate ab(x) by applying the inclusion-exclusion principle to the union of prime factors of x-A and B-x.


Comments

There are no comments at the moment.