一、递归版快速幂
int fastPow(int a, int x, int p) { if (x==0) { return 1%p; } int tmp=fastPow(a,x/2,p); tmp=(long long)tmp*tmp%p; if (x%2==1) { tmp=(long long)tmp*a%p; } return tmp; }
二、迭代版快速幂(常用)
int fastPow2(int a, int x, int p) { int s=1%p; for (;x;x>>=1) { if (x&1) { s=(long long)s*a%p; } a=(long long)a*a%p; } return s; }