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 24
题意:求A^B%C的值,B很大很大!!!
思路:直接采用欧拉降幂,缩小B的值,然后快速幂...
欧拉降幂:
已放弃公式的推导,直接应用吧QAQ
代码如下(模板):
#include<bits/stdc++.h>
#define ll long long
using namespace std;
char str[1000010];
ll phi(ll n)
{
ll res=n;
for(ll i=2; i*i<=n; i++)
{
if(n%i==0)
{
res=res-res/i;
while(n%i==0)
n/=i;
}
}
if(n>1)res=res-res/n;
return res;
}
ll quickpow(ll x,ll n,ll c)
{
ll res=1;
while(n)
{
if(n&1)res=res*x%c;
x=x*x%c;
n>>=1;
}
return res;
}
int main()
{
ll a,c;
while(~scanf("%lld %s %lld",&a,str,&c))
{
int l=strlen(str);
ll mod=phi(c);
ll cnt=0;
for(int i=0; i<l; i++)
cnt=(cnt*10+str[i]-'0')%mod;
cnt+=mod;
printf("%lld\n",quickpow(a,cnt,c));
}
return 0;
}