首页 > 代码库 > hdu2035【二分快速幂】

hdu2035【二分快速幂】

  求AˆB取模。直接二分快速幂即可。比如9ˆ9=(9ˆ2)ˆ4 * 9,将B一直模2然后A自乘,复杂度long(n)。

  

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5  6 int quickpow(int a, int b, int mod) { 7     int ans = 1; 8     while(b) { 9         if(b & 1) {10             ans = ans * a % mod;11         }12         a  = a * a % mod;13         b >>= 1;14     }    15     return ans;16 }17 18 int main() {19     int a, b;20     while(scanf("%d%d", &a, &b), a + b) {21         printf("%d\n", quickpow(a, b, 1000));22     }23     return 0;24 }25    

 

hdu2035【二分快速幂】