给出3个正整数A B C,求A^B Mod C。
例如,3 5 8,3^5 Mod 8 = 3。
Input 3个正整数A B C,中间用空格分隔。(1 <= A,B,C <= 10^9) Output 输出计算结果 Sample Input
3 5 8Sample Output
3
本题运用到快速幂运算和取模运算
#include<bits/stdc++.h>
using namespace std;
typedef long long llong;
int ans;
int pow_mod(llong a, llong k, llong c){
ans = 1%c;
while(k){
if(k % 2 == 1)ans = ans * a%c;
k = k / 2;
a = a*a%c;
}
return ans;
}
int main(){
llong a, b, c;
scanf("%lld%lld%lld", &a, &b, &c);
int y = pow_mod(a, b, c);
printf("%d\n", y);
return 0;
}