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 4
2 10 1000
Sample Output
1
24
Hint
Source
FZU 2009 Summer Training IV--Number Theory
题意:A^B mod C
欧拉公式 不得不说欧拉太强了!!
这道题用的是 公式三
然后 就是上代码!
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#define N 750010
#define ll long long
using namespace std;
ll ol(ll x)
{
int ans=x;
for(ll i=2;i*i<=x;i++)
{
if(x%i==0)
{
ans=ans/i*(i-1);
while(x%i==0)
{
x/=i;
}
}
}
if(x!=1)
ans=ans/x*(x-1);
return ans;
}
ll power(ll a,ll b,ll mod)
{
ll ans=1;
while(b)
{
if(b&1)
{
ans=ans*a%mod;
}
a=a*a%mod;
b>>=1;
}
return ans;
}
int main()
{
ll a,c;
char s[1000006];
while(~scanf("%lld %s %lld",&a,s,&c))
{
ll ans=0;
ll tmp=ol(c);
ll len=strlen(s);
for(ll i=0;i<len;i++)
ans=(10*ans+s[i]-'0')%tmp;
ans+=tmp;
printf("%lld\n",power(a,ans,c));
}
return 0;
}