点击打开链接
Problem Description
Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000000000,1<=B<=10^1000000).
Input
There are multiply testcases. Each testcase, there is one line contains three integers A, B and C, separated by a single space.
Output
For each testcase, output an integer, denotes the result of A^B mod C.
Sample Input
3 2 42 10 1000
Sample Output
124
Source
FZU 2009 Summer Training IV--Number Theory
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
char B[maxn];
/*
欧拉降幂
*/
ll A,C;
ll pow_(ll base,ll n){
ll ans=1,mod=C;
while(n){
if(n&1){
ans=ans*base%mod;
}
base=base*base%mod;
n>>=1;
}
return ans;
}
ll phi(ll x){
ll ans=x;
ll m=sqrt(x+0.5);
for(ll i=2;i<=m;i++){
if(x%i==0){
ans=ans-ans/i;
}
while(x%i==0)x/=i;
}
if(x>1)ans=ans-ans/x;
return ans;
}
int main()
{
while(scanf("%I64d %s %I64d",&A,B,&C)==3){
ll cc=phi(C);
int len=strlen(B);
ll bb=0;
//大整数 mod
for(int i=0;i<len;i++){
bb=(bb*10+B[i]-'0')%cc;
}
//bb+=cc;
ll ans=pow_(A,bb);
printf("%I64d\n",ans);
}
return 0;
}