Problem 1759 Super A^B mod C
Time Limit: 1000 mSec Memory Limit : 32768 KB
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 4 2 10 1000
Sample Output
1 24
思路:指数循环节,数据水,并没有B<C&&A,C不互质的;
#include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll __int64 #define esp 0.00000000001 const int N=1e5+10,M=1e6+10,inf=1e9+10; const int mod=1000000007; #define MAXN 10000010 ll quickpow(ll x,ll y,ll z) { ll ans=1; while(y) { if(y&1) ans*=x,ans%=z; x*=x; x%=z; y>>=1; } return ans; } ll phi(ll n) { ll i,rea=n; for(i=2;i*i<=n;i++) { if(n%i==0) { rea=rea-rea/i; while(n%i==0) n/=i; } } if(n>1) rea=rea-rea/n; return rea; } char a[M]; int main() { ll x,y,z,i,t; while(~scanf("%I64d%s%I64d",&x,a,&z)) { t=strlen(a); ll p=phi(z); ll ans=0; for(i=0;i<t;i++) ans=(ans*10+a[i]-'0')%p; ans+=p; printf("%I64d ",quickpow(x,ans,z)); } return 0; }