[cpp] //九度OJ 快速取模之《root(n,k)》 //http://ac.jobdu.com/problem.php?pid=1085 #include<stdio.h> int k; www.2cto.com long fang(long a,long b) { long long ret=1; while(b) { if(b&1)ret=ret*a%(k-1); a=a*a%(k-1); b>>=1; } return ret; } int main() { long long x,y; int i; while(~scanf("%lld %lld %d",&x,&y,&k)) { long ret=fang(x,y); if(!ret)ret=k-1; printf("%lld\n",ret); } return 0; }