解题思路:参见OI Wiki中的快速幂
C++代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int fast_pow(int a, int b, int p)
{
int res = 1 % p;
while (b)
{
if (b & 1)
{
res = 1LL * res * a % p;
}
a = 1LL * a * a % p;
b >>= 1;
}
return res;
}
int main()
{
int a, b, p;
cin >> a >> b >> p;
auto res = fast_pow(a, b, p);
cout << res << endl;
return 0;
}