P1226 【模板】快速幂

#include 
#define int long long
using namespace std;
int power(int a,int b,int p)
{
    int ans = 1;
    while(b > 0)
    {
        if(b & 1)   ans = ans * a % p;
        b >>= 1;
        a = a * a % p;
    }
    return ans;
}
signed main()
{
    int a,b,p;
    cin >> a >> b >> p;
    cout << a << "^" << b << " mod " << p << "=" << power(a,b,p) << endl;
    return 0;
}

 

你可能感兴趣的:(c++,洛谷)