Editorial for COCI '08 Contest 4 #5 Trezor
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 columns and the limits on and are relatively small, so we will solve the problem by counting vaults each of the guards sees in a particular column.
Let:
- be the total number of vaults in column seen by the guard at ;
- be the total number of vaults in column seen by the guard at ;
- be the total number of vaults in column seen by both guards.
Knowing these numbers, we can calculate:
- The number of super-secure vaults in column as ;
- The number of secure vaults in column as ;
- The number of insecure vaults as .
How do we calculate the number of vaults in a column? Let be the number of vaults the guard can see in the column at distance from his position. For some , the guard can see the vault at only if and are relatively prime. If the greatest common divisor between and is greater than , then and can be divided by the divisor to get the coordinates of another point blocking the view.
The number of integers relatively prime with that are at most can be calculated by finding the prime factors of and using the inclusion-exclusion principle. Take for example and . Then the number of vaults the guard can see in a column at distance from his position is:
A special case is , because the guard can only see the vault directly ahead of him in his column.
Now it is obvious that and . It is less obvious that we can calculate by applying the inclusion-exclusion principle to the union of prime factors of and .
Comments