1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| #include <bits/stdc++.h> #define ll long long #define maxn 10000000 using namespace std; bool ok[maxn]; int prime[maxn],phi[maxn],cnt; ll a,m,b; void sieve() { phi[1]=1; for(ll i=2;i<maxn;++i) { if(!ok[i]) { prime[cnt++]=i; phi[i]=i-1; } for(int j=0;j<cnt;++j) { if(i*prime[j]>=maxn)break; ok[i*prime[j]]=1; if(i%prime[j]==0) { phi[i*prime[j]]=phi[i]*prime[j]; break; } else phi[i*prime[j]]=phi[i]*(prime[j]-1); } } } inline ll read(ll m){ register ll x=0,f=0;char ch=getchar(); while(!isdigit(ch)) ch=getchar(); while(isdigit(ch)){ x=x*10+ch-'0'; if(x>=m) f=1; x%=m;ch=getchar(); } return x+(f==1?m:0); }
ll fast_pow(ll a,ll b,ll p){ ll ret=1; for(;b;b>>=1,a=a*a%p) if(b&1) ret=ret*a%p; return ret; }
int main() { sieve(); scanf("%lld%lld",&a,&m); b=read(phi[m]); printf("%lld\n",fast_pow(a,b,m)); return 0; }
|