super_log
In Complexity theory, some functions are nearly O(1)O(1), but it is greater then O(1)O(1). For example, the complexity of a typical disjoint set is O(nα(n))O(nα(n)). Here α(n)α(n) is Inverse Ackermann Function, which growth speed is very slow. So in practical application, we often assume α(n) \le 4α(n)≤4.
super_log 链接
##
不用考虑是否互质,直接运用公式,逐渐将其迭代
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+5;
const int maxn=1000020;
const int Maxn=1000020;
ll prime[Maxn],phi[Maxn],pn=0;
bool jud[Maxn];
ll quickjia(ll a,ll b,ll p)
{
ll sum=0;
while(b)
{
if(b&1)
{
sum=(sum+a)%p;
}
a=(a+a)%p;
b>>=1;
}
return sum;
}
ll quick(ll a,ll b,ll p)
{
ll sum=1;
while(b)
{
if(b&1)
{
sum=quickjia(sum,a,p)%p;
}
a=quickjia(a,a,p)%p;
b>>=1;
}
return sum;
}
void init()//欧拉函数打表
{
phi[1]=1;
for(int i=2; i<=maxn; i++)
{
if(!jud[i])
{
prime[++pn]=i;
phi[i]=i-1;
}
for(int j=1; j<=pn; j++)
{
ll cnt=i*prime[j];
if(cnt>maxn)
break;
jud[cnt]=true;
if(i%prime[j]==0)
{
phi[cnt]=phi[i]*prime[j];
break;
}
else
{
phi[cnt]=phi[i]*(prime[j]-1);
}
}
}
}
ll slove(ll a,ll b,ll p)
{
if(p==1)
return 0;
if(b==0)
return 1;
ll s=slove(a,b-1,phi[p]);
if(s<phi[p]&&s)
return quick(a,s,p);
else//注意零的特判,零直接当做指数。
return quick(a,s+phi[p],p);
}
int main()
{
int t;
init();
scanf("%d",&t);
ll a,b,m;
ll ans=0;
while(t--)
{
scanf("%lld %lld %lld",&a,&b,&m);
ans=slove(a,b,m);
printf("%lld\n",ans%m);
}
}