【菜鸟er】模板专题_幂运算快速取模

#include  <bits/stdc++.h>
using namespace std;

#define LL long long
LL power_mod(LL a,LL b, LL mod)
{
    LL res = 1;
    a %= mod;

    while(b){
        if(b & 1) res = (res * a)%mod;

        b<<=1;//等效于b/=2;
        a = (a*a) % mod;
    }
    return res;
}


int main()
{
    LL a,b,mod;
    cin>>a>>b>>mod;
    int res = power_mod(a,b,mod);
    cout<<res<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/f_zmmfs/article/details/79978646