常规求幂 [cpp] view plain copy int pow1(int a,int b) { int r=1; while(b--) r*=a; return r; } 二分求幂(一般) [cpp] view plain copy int pow2(int a,int b) { int r=1,base=a; while(b!=0) { if(b%2) r*=base; base*=base; b/=2; } return r; } 二分求幂(位操作,同pow2) [cpp] view plain copy int pow4(int a,int b) { int r=1,base=a; while(b!=0) { if(b&1) r*=base; base*=base; b>>=1; } return r; } 快速求幂(位运算,更复杂) [cpp] view plain copy int pow3(int x,int n) { if(n==0) return 1; else { while((n&1)==0) { n>>=1; x*=x; } } int result=x; n>>=1; while(n!=0) { x*=x; if((n&1)!=0) result*=x; n>>=1; } return result; }