
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
int main()
{
int a, b, p;
cin >> a >> b >> p;
int res = 1 % p;
while (b)
{
if (b & 1) res = (long long)res * a % p;//这里的%p纯粹是为了防止数据溢出;
a = (long long)a * a % p;//利用二进制和(a*b)%p=(a%p*b%p)%p
b >>= 1;
}
cout << res << endl;
return 0;
}
本文介绍了一种快速幂取模算法的C++实现方法,该算法通过位运算和二进制思想,有效地解决了大数幂次方取模的问题,避免了数据溢出,提高了计算效率。
2786

被折叠的 条评论
为什么被折叠?



