快速幂取模

#include <iostream>
#include <cstring>
using namespace std;

long long a, b, c;

int main(){
    cin >> a >> b >> c;
    
    long long ans = 1, t = a, bb = b;
    
    while(b != 0){        //拆分成二进制 a^5 拆成 a^4 * a^1 
        if((b & 1)){
            ans = ((ans % c) * t) % c;
        }
        t = (t % c) * (t % c) % c;
        b = (b >> 1);
    }
    
    cout << a << "^" << bb << " mod " << c << "=" << ans;
    
    return 0;
} 

 

posted @ 2017-08-17 10:45  淡蓝色光  阅读(115)  评论(0编辑  收藏  举报