这类取模问题需要注意数据的范围。
这题A,B,C都达到了long long的范围,那么你做乘法就会有一个溢出的问题。
所以你需要一个特殊的mul_mod函数来处理乘法。。。这个函数援引自aekdycoin的博客。
#include "cstdio"
#define LL long long
/*
LL mul_mod(LL a,LL b,LL n){
if(b==1) return a%n;
if(b%2==0) return mul_mod((2*a)%n, (b/2)%n, n);
return mul_mod((2*a)%n, (b/2)%n, n)+a;
}*/
LL mul_mod(LL a,LL b,LL c)
{
LL ret=0,tmp=a%c;
while(b)
{
if(b&0x1)if((ret+=tmp)>=c)ret-=c;
if((tmp<<=1)>=c)tmp-=c;
b>>=1;
}
return ret;
}
LL pow_mod(LL a,LL p,LL n){
if(p==0) return 1%n;
LL ans=pow_mod(a,p/2,n);
ans=mul_mod(ans, ans, n);
if(p%2==1) ans=mul_mod(ans, a, n);
return ans%n;
}
int main(){
LL a,b,c;
while(scanf("%lld%lld%lld",&a,&b,&c)!=EOF){
a%=c;
printf("%lld\n",pow_mod(a, b, c));
}
return 0;
}