链接:点击打开链接
题意:A^B mod C. (1<=A,B,C<2^63)
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
long long c;
long long quickadd(long long a,long long b){ //快速乘法
long long sum=0;
while(b){
if(b&1){
sum=sum+a;
while(sum>=c) //取模比较慢,因此用减法
sum-=c;
}
b>>=1; //位运算优化
a=a+a;
while(a>=c)
a-=c;
}
return sum;
}
long long quickc(long long a,long long b){ //快速幂
long long sum=1;
while(b){
if(b&1)
sum=quickadd(sum,a);
b>>=1;
a=quickadd(a,a);
}
return sum;
}
int main(){
long long a,b;
while(scanf("%I64d%I64d%I64d",&a,&b,&c)!=EOF){
printf("%I64d\n",quickc(a,b));
}
return 0;
}