快速幂取模


1
typedef long long ll; 2 ll mod_pow(ll x, ll n, ll mod){ 3 ll res = 1; 4 while( n > 0 ){ 5 if( n & 1 ) res = res * x % mod; 6 x = x * x % mod; 7 n >>= 1; 8 } 9 return res; 10 }

 递归版

1 typedef long long ll;
2 ll mod_pow(ll x, ll n, ll mod){
3     if( n == 0 ) return 1;
4     ll res = mod_pow( x * x % mod, n / 2, mod );
5     if( n & 1 ) res = res * x % mod;
6     return res;
7 }

 

posted @ 2015-11-28 10:04  Lweleth  阅读(170)  评论(0编辑  收藏  举报