设f(n)为模n时的答案,由2k mod n=2k mod φ(n)+φ(n) mod n(并不会证),且k mod φ(n)=f(φ(n)),直接就可以得到一个递推式子。记搜一发即可。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int read()
{
int x=,f=;char c=getchar();
while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
return x*f;
}
#define N 10000010
int T,n,f[N],phi[N],prime[N>>],cnt=;
bool flag[N];
int ksm(int a,int k,int p)
{
int s=;
for (;k;k>>=,a=1ll*a*a%p) if (k&) s=1ll*s*a%p;
return s;
}
int calc(int n)
{
if (~f[n]) return f[n];
return f[n]=ksm(,calc(phi[n])+phi[n],n);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("bzoj3883.in","r",stdin);
freopen("bzoj3883.out","w",stdout);
const char LL[]="%I64d\n";
#else
const char LL[]="%lld\n";
#endif
flag[]=;phi[]=;
for (int i=;i<=N-;i++)
{
if (!flag[i]) prime[++cnt]=i,phi[i]=i-;
for (int j=;j<=cnt&&prime[j]*i<=N-;j++)
{
flag[prime[j]*i]=;
if (i%prime[j]==) {phi[prime[j]*i]=phi[i]*prime[j];break;}
else phi[prime[j]*i]=phi[i]*(prime[j]-);
}
}
memset(f,,sizeof(f));f[]=f[]=;
T=read();
while (T--)
{
n=read();
printf("%d\n",calc(n));
}
return ;
}