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 are one line contains three integers a, B and C, separated by A.
Output
For each testcase, output a integer, denotes the result of A^b mod C.
Sample Input
3 2 4
2 10 1000
Sample Output
1
24
train of Thought
An exponential very scary topic, obviously fast power is not directly solved, here we need to use the power down.
Abmod C=ab Modϕ (c) +ϕ (c) MoD C A^bmod~c=a^{b~mod~\phi (c) +\phi (c)}mod~c, when and only if the formula was established when B>=ϕ (c) B>=\phi (c), then we used the power down treatment, Other cases are directly fast power solutions.
Φ\phi represents the Euler function.
AC Code
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> using namespace std;
const int MAXN = 1E6+10;
typedef __int64 LL;
LL Pr[maxn],a,c;
BOOL PRIME[MAXN];
Char B[MAXN];
void Getprime ()//Sieve method prime number Table {int i,j,k=0;
memset (prime,true,sizeof (prime));
For (i=2 i<maxn; i++) {if (prime[i)) {pr[k++]=i;
for (j=i+i; j<maxn; j+=i) Prime[j]=false;
}} LL Phi (ll N)//Euler function {ll rea=n;
for (int i=0; pr[i]*pr[i]<=n; i++) {if (n%pr[i]==0) {rea=rea-rea/pr[i];
while (n%pr[i]==0) n/=pr[i];
} if (n>1) rea=rea-rea/n;
return REA;
ll Mult (ll a,ll b,ll MoD) {ll res = 1;
A%=mod;
while (b) {if (b&1) res = (res*a)%mod;
A = (a*a)%mod;
b>>=1;
return res;
} void Solve () {LL ph = phi (c);
A%=c; int Len= strlen (b);
if (Len>ll (log10 (ph))) {LL res = 0;
for (int i=0; i<len; i++) res = (RES * + b[i]-' 0 ')%ph;
printf ("%i64d\n", mult (A,res + ph,c));
else {LL MB;
SSCANF (b, "%i64d", &MB);
printf ("%i64d\n", Mult (A,mb,c));
int main () {getprime ()};
while (~SCANF ("%i64d%s%i64d", &a,b,&c)) solve ();
return 0; }