快速幂取模


/*************************************************************************
 2     > File Name:        pow_mod.cpp
 3     > Author:         wangzhili
 4     > Mail:           wangstdio.h@gmail.com
 5     > Created Time:   2014年03月17日 星期一 20时59分52秒
 6  ************************************************************************/

 7 #include<iostream>          //求解a^i(mod n);
 8 typedef long long ll;
 9 using namespace std;
10 ll pow_mod(ll a, ll i, ll n){
11     if(i == 0) return 1 % n;
12     int temp = pow_mod(a, i >> 1, n);
13     temp = temp*temp%n;
14     if(i&1) temp = temp*a%n;
15     return temp;
16 }
17 
18 int main(){
19     ll n, i, a;
20     while(cin >> a >> i >> n){
21         cout << pow_mod(a, i, n) << endl;
22     }
23     return 0;
24 }

#include<iostream>
using namespace std;
typedef long long ll;
ll pow_mod(ll a, ll i, ll n){
    ll ans = 1, temp = a;
    while(n){
        if(n & 1){
            ans = (temp * ans) % n;
        }
        temp *= temp;
        temp %= n;
        n >> 1;
    }
    return ans;
}


posted on 2014-04-30 18:37  wangzhili  阅读(105)  评论(0)    收藏  举报