1. 程式人生 > >【演算法模板】快速冪

【演算法模板】快速冪

#include <iostream>
#define ull unsigned long long

using namespace std;

ull fastpow(ull a,ull b,ull mod)
{
    ull ans=1;
    while(b!=0)
    {
        if(b&1)ans=ans*a%mod;
        b>>=1;
        a=a*a%mod; 
    }
    return ans%mod;
}

int main()
{
    ull a,b,mod;
    cin>>a>>b>>mod;
    cout<<fastpow(a,b,mod)%mod;
    return 0;
}