#P9872. I. Colorings Counting
I. Colorings Counting
I. Colorings Counting
Problem Description
Given a ring of nodes. It's required to color each node with one of the colors in , and ensure that adjacent points have different colors. Consider two colorings are different if and only if two colorings sequences cannot be transformed into each other through several of the following three operations:
- Forall , , then ;
- Forall , , then ;
- Forall , , then . Calculate the different colorings, modulo .
Input
The input consists of multiple test cases. The first line contains a single integer () - the number of test cases. Description of the test cases follows. The first line of each test case contains two integer (, ). It's guaranteed that are not multiples of . It's guaranteed that there will be no more than test cases with . It's guaranteed that there will be no more than test cases with . It's guaranteed that there will be no more than test cases with .
Output
For each test case, print a single integer - the different colorings, modulo .
Sample Input
4
4 2
4 3
6 3
2023 808
Sample Output
1
2
5
304535191
Source
2023“钉耙编程”中国大学生算法设计超级联赛(7)