本题考点:欧拉降幂
Super A^B mod C
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 Input3 2 4
2 10 1000
Sample Output
1
23
分析:欧拉降幂的模板题
直接套公式即可:
View Code

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
ll a,p;
char b[10000005];
ll phi(ll x)
{
ll res=x;
ll a=x;
for(int i=2;i*i<=x;i++)
{
if(a%i==0)
{
res=res/i*(i-1);
while(a%i==0) a/=i;
}
}
if(a>1) res=res/a*(a-1);
return res;
}
ll qp(ll a,ll b,ll p)
{
ll ans=1;
while(b)
{
if(b&1)
{
ans=(ans*a)%p;
}
a=(a*a)%p;
b>>=1;
}
return ans;
}
int main()
{
//freopen("input.txt","r",stdin);
while(scanf("%lld %s %lld",&a,&b,&p)!=EOF)
{
ll len=strlen(b);
ll res=0;
ll pp=phi(p);
for(int i=0;i<len;i++)
{
res=(res*10+b[i]-'0')%pp;
}
printf("%lld\n",qp(a,res,p));
}
return 0;
}
