扩展欧拉定理:
\[ a^x \equiv a^{x\mathrm{\ mod\ }\varphi(p) + x \geq \varphi(p) ? \varphi(p) : 0}(\mathrm{\ mod\ }p)\]
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long ll;
ll aa, cc;
char bb[1000005];
ll getPhi(ll x){
ll ans=x;
for(ll i=2; i*i<=x; i++)
if(x%i==0){
ans -= ans / i;
while(x%i==0) x /= i;
}
if(x>1) ans -= ans / x;
return ans;
}
ll ksm(ll a, ll b, ll c){
ll re=1;
while(b){
if(b&1) re = (re * a) % c;
a = (a * a) % c;
b >>= 1;
}
return re;
}
int main(){
while(scanf("%lld %s %lld", &aa, bb, &cc)!=EOF){
ll phi=getPhi(cc);
int len=strlen(bb);
ll tmp=0;
for(int i=0; i<len; i++){
tmp = tmp * 10 + bb[i] - ‘0‘;
if(tmp>=phi) break;
}
if(tmp>=phi){
tmp = 0;
for(int i=0; i<len; i++)
tmp = (tmp * 10 + bb[i] - ‘0‘) % phi;
printf("%lld\n", ksm(aa, tmp+phi, cc));
}
else printf("%lld\n", ksm(aa, tmp, cc));
}
return 0;
}