#P13953. [ICPC 2023 Nanjing R] 原根
[ICPC 2023 Nanjing R] 原根
Description
BaoBao has just learnt about primitive roots in number theory and is showing off his knowledge to Little Cyan Fish through an instant messaging software.
:::align{center}

This image is only for amusement and has nothing to do with the problem itself. You can safely skip this image if you can't read Chinese. :::
Based on the fact that if a non-negative integer is a primitive root modulo (where is a prime), then , BaoBao decided to use the expression to check if is a primitive root modulo . Unfortunately, in most programming languages (for example C and C++), is the bitwise exclusive-or (XOR) operator, not the power operator. Little Cyan Fish spotted this issue at once and now he is interested in the following problem:
Given a prime number and a non-negative integer , how many non-negative integers satisfies and ? Here is bitwise exclusive-or (XOR) operator.
Please help Little Cyan Fish solve this problem.
Input Format
There are multiple test cases. The first line of the input contains an integer () indicating the number of test cases. For each test case:
The first and only line contains two integers and (, , is a prime).
Output Format
For each test case, output one line containing one integer indicating the number of satisfying the constraints.
3
2 0
7 11
1145141 998244353
1
2
872
京公网安备 11011102002149号