题目链接
给定A, B, P,求(A^B) mod P。
输入
输入共一行。
第一行有三个数,N, M, P。
输出
输出共一行,表示所求。
共10组数据
对100%的数据,A, B为long long范围内的非负整数,P为int内的非负整数。
样例输入
2 5 3
样例输出
2
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include<algorithm>
#include <vector>
#include<string>
using namespace std;
unsigned long long pow_mod(unsigned long long a, unsigned long long b, unsigned long long c) {
unsigned long long ans = 1;
unsigned long long base = a % c;
while (b) {
if (b & 1) ans = (ans * base) % c;
base = (base * base) % c;
b >>= 1;
}
return ans;
}
int main()
{
unsigned long long a, n,p;
cin >> a >> n>>p;
cout<< pow_mod(a,n,p);
return 0;
}

本文深入探讨了快速幂取模算法的实现细节,通过一个具体的C++代码示例,展示了如何高效地计算(A^B)modP的问题。适用于解决大规模数值运算中常见的溢出问题,尤其是在竞赛编程和算法设计中。
1079

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



