https://nanti.jisuanke.com/t/41299
Analysis: Title given a, b, mod seek to meet the minimum conditions of a, was the subject of a formula, each as long as recursion, b + 1's will, so we can actually think b is the number, the number of what? Inverse function logarithmic function. . . . That is seeking a title of a power of a power ..... and has been engaged in b times to obtain answers.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int M=1e6+6; ll ph[M]; ll init(){ ph[0]=0,ph[1]=1; for(int i=2;i<M;i++) ph[i]=i; for(int i=2;i<M;i++){ if(ph[i]==i){ for(int j=i;j<M;j+=i) ph[j]=ph[j]/i*(i-1); } } } ll ksm(ll a,ll b,ll mod){ ll t=1ll; while(b){ if(b&1) t=(t*a)%mod; b>>=1; a=(a*a)%mod; } return t; } ll dfs(ll a,ll b,ll p){ if(b==0) return 1; if(p==1) return 0; ll P=dfs(a,b-1,ph[p]); if(P<ph[p]&&P) return ksm(a,P,p); return ksm(a,P+ph[p],p); } int main(){ int t; scanf("%d",&t); init(); while(t--){ ll a,b,mod; scanf("%lld%lld%lld",&a,&b,&mod); printf("%lld\n",dfs(a,b,mod)%mod); } return 0; }