题意
思路
递归地利用欧拉降幂公式,直到模数<= 3。
这里有一个结论是,对 p 求欧拉数直到 1 最多需要求 log(p) 次。
链接
https://cn.vjudge.net/contest/176263#status/nwpu201630314/C/0/
代码
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long LL;
const int maxn = 1e7 + 10;
int ans[maxn];
int n, t;
LL quick_pow(int a, int b, int c)
{
LL res = 1, t = a;
while(b > 0)
{
if(b & 1) res = (res * t) % c;
t = (t * t) % c;
b >>= 1;
}
return res;
}
LL get_phi(LL C)
{
LL res = C;
for(int i= 2; i * i <= C; i++)
{
if(C % i == 0)
{
res = res / i * (i - 1);
while(C % i == 0) C /= i;
}
}
if(C > 1) res = res / C * (C - 1);
return res;
}
int solve(int t)
{
if(ans[t] >= 0) return ans[t];
int phit = get_phi(t);
int res = quick_pow(2, solve(phit) + phit, t);
return res;
}
int main()
{
//freopen("in.txt", "r", stdin);
memset(ans, -1, sizeof ans);
ans[1] = ans[2] = 0, ans[3] = 1;
scanf("%d", &n);
while(n --)
{
scanf("%d", &t);
cout << solve(t) << endl;
}
return 0;
}