Description
题解
来自出题人的剧毒题解
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int phi(int x)
{
int res = x;
for(int i = 2;i*i <= x;i++)
if(x % i == 0)
{
res /= i; res *= (i-1);
while(x % i == 0) x /= i;
}
if(x ^ 1) res = res / x * (x-1);
return res;
}
int quick_power(long long x,int n,int mod)
{
long long res = 1;
while(n)
{
if(n & 1) res = (res * x) % mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
int solve(int p)
{
if(p == 1) return 0;
int k = 0;
while(p % 2 == 0){k++;p >>= 1;}
int f = phi(p);
int res = solve(f);
(res+=f - k % f) %= f;//这是为了应对递归中的 -k
res = quick_power(2,res,p) % p;
return res << k;
}
int main()
{
int t,p;
scanf("%d",&t);
while(t--)
{
scanf("%d",&p);
printf("%d\n",solve(p));
}
return 0;
}