Hilbert's Hash Browns
Time limit1sMemory limit512 MB
Count how many residues mod n are reachable as x^p + q mod n over all nonnegative integers x.
- Level
Hard8 of 10
- Topics
- Number theory, Math, Combinatorics, Implementation
- Solved
- No attempts yet
Problem
Hilbert's Hotel has infinitely many rooms, numbered 0, 1, 2, and so on, so it takes in one more guest even when it looks full: every guest in room moves to room , and room 0 becomes free. The restaurant attached to the hotel, Hilbert's Hash Browns, is not infinite. It has a very large number of tables, but that number is fixed.
The waiter there is very lazy. Instead of keeping a record of which tables are free, he seats every customer with one simple formula. He asks the customer for the hotel room number , raises that number to the power , and adds . The result is huge and there are only tables, so he takes the remainder modulo and points the customer to that table. Tables are numbered 0 to , and the customer goes to table . If someone already sits there, the customer leaves hungry.
The waiter picks new values of and every day, and he has noticed that on some days a table is never used however many customers arrive. With , and , table 0 is never used, because no integer satisfies .
Room numbers run over every nonnegative integer. Given , and , find the largest number of tables that can be used.
Input
The first line contains three integers , and separated by spaces (, , ).
Output
Print the largest number of tables that can be used.