本题考点:欧拉降幂

FZU 1759 题解 欧拉降幂-LMLPHP

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 Input

3 2 4
2 10 1000
Sample Output
1
23
分析:欧拉降幂的模板题
直接套公式即可:FZU 1759 题解 欧拉降幂-LMLPHP

AC代码:

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