//模指数运算
#include <iostream>
#include <vector>
#include <deque>
using namespace std;
int main()
{
//输入;
int b;
cin >> b;
int n;
cin >> n;
int m;
cin >> m;
//二进制转换
int q = n;
int k = 0;
deque<int> numbers;
while (q != 0)
{
numbers.push_front(q % 2);
q /= 2;
}
//转二进制结束,生成一个deque保存二进制数;
//当时为了顺序输出能输出正确所以低位保存二进制高位,高位deque保存二进制低位,用的是push_front
int x = 1;
long long int power = b%m;
auto j = numbers.size();
for (int i = j-1; i >=0; i--)
{
if (numbers[i] == 1)
x = (x*power) % m;
power = (power*power) % m;
}//快速模算法核心步骤
cout << x;
return 0;
}
快速模算法 A^BmodC
最新推荐文章于 2024-02-28 22:16:04 发布