题目链接
http://www.lydsy.com/JudgeOnline/problem.php?id=3884
思路
要写这道题,需要用到一个神奇的定理:
我们设,则
这样,我么就可以递归求解。
容易证明:中每次会除以,因此时间复杂度为。
代码
#include <cstdio>
int t,p;
inline int phi(int x)
{
int res=x;
for(register int i=2; i*i<=x; ++i)
{
if(x%i==0)
{
res=(res/i)*(i-1);
while(x%i==0)
{
x/=i;
}
}
}
if(x!=1)
{
res=(res/x)*(x-1);
}
return res;
}
inline int quickpow(int a,int b,int mo)
{
int res=1;
while(b)
{
if(b&1)
{
res=(1ll*res*a)%mo;
}
a=(1ll*a*a)%mo;
b>>=1;
}
return res;
}
int f(int x)
{
if(x==1)
{
return 0;
}
int ph=phi(x);
return quickpow(2,f(ph)+ph,x);
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&p);
printf("%d\n",f(p));
}
return 0;
}