【洛谷】P1229快速幂

题目链接:https://www.luogu.org/problemnew/show/P1226

 

题意:求b^p % m之后的结果

 

题解:快速幂模板

 

代码:

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
#define ll long long

ll b,p,mod;

ll qpow( ll a,ll b){
    ll res = 1;
    while( b ){
        if( b&1 ){
            res = res*a%mod;
        }
        a = a*a%mod;
        b >>= 1;
    }
    return res%mod;
}

int main(){
    cin>>b>>p>>mod;
    ll ans = qpow(b,p);
    printf("%lld^%lld mod %lld=%lld\n",b,p,mod,ans);
    return 0;
}

 

posted @ 2018-10-08 17:39  甜酒果。  阅读(122)  评论(0编辑  收藏  举报