#P7437. [2017年杭电多校]Division Game
[2017年杭电多校]Division Game
Division Game
Problem Description
There are piles of stones in a circle, numbered from to , where the number of the stones in each pile is initially. You can do some round operations, where the initial round is numbered as the -st round. The operation of the -th round is to modify the pile of stones numbered . In each round, you should remove from this pile some stones (at least one stone), satisfying that the number of stones in this pile before this operation is a multiple of the number of stones in this pile after operation, which means that you ought to remain at least one stone in this pile. The game is ended if there exists at least one pile containing only one stone. Given two positive integers and , your task is to calculate for each pile the number of the possible operation plans that it is the last operated pile before the game is ended. The integer may be very large, so the prime-factor decomposition of will be given, in other words, if , then the integers and will be given, but the integer will not. The answer may be very large, so you only need to give the value of the answer modulo .
Input
The input contains multiple test cases. For each test case: The first line contains two positive integers and , satisfying that . In next lines, the -th line contains two positive integers and , satisfying that . It is guaranteed that are distinct. About test cases in total, where no more than cases satisfy .
Output
For each test case, output " Case #: " in one line (without quotes), where indicates the case number starting from and denotes the number of the possible operation plans modulo for the pile numbered of corresponding case.
Sample Input
1 1
2 2
2 1
3 1
5 1
1 2
2 3
2 2
2 4
5 4
Sample Output
Case #1: 2
Case #2: 3
Case #3: 6 4
Case #4: 1499980 1281085
Source
2017 Multi-University Training Contest - Team 1