Euler descending: particularly large power can be used when rapid power to significantly reduce the time complexity, and when the power as large as 10 ^ when power is not very fast 10000 line, this time you need to use Euler descending it theorem as follows:
It proved to say good-bye.
Put a topic FZU 1759
Topic Source: https://vjudge.net/problem/FZU-1759
I.e., evaluation of a ^ b% c, b thief large range, so Euler title descending board.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include<queue>
#define MAX_len 50100*4
using namespace std;
typedef long long ll;
char b[1000100];
ll a,mod;
ll euler(ll n)
{
ll i,j,res=n,temp=n;
for(i=2;i*i<=temp;i++)
{
if(temp%i==0)
{
res=res/i*(i-1);
while(temp%i==0)
temp/=i;
}
}
if(temp>1)
res=res/temp*(temp-1);
return res;
}
ll quickpow(ll a,ll n)
{
ll res=1;
while(n)
{
if(n&1)
{
res=(res*a)%mod;
}
n>>=1;
a=(a*a)%mod;
}
return res%mod;
}
int main()
{
ll i,j;
while(scanf("%lld %s %lld",&a,b,&mod)!=EOF)
{
ll len=strlen(b);
ll t1=euler(mod);
ll ans=0;
for(i=0;i<len;i++)
{
ans=(ans*10+b[i]-'0')%t1;
}
ans+=t1;
printf("%lld\n",quickpow(a,ans));
}
return 0;
}
BZOJ 3884
Topic Source: https://www.lydsy.com/JudgeOnline/problem.php?id=3884
As the index is infinite, we should go to the modulo operation.
Extended Euler's theorem: a ^ b≡a ^ (b% φ (p) + φ (p)) (mod p) a is any integer, b, p is a positive integer, and b> φ (p) (a, p need not be prime to).
f(p)=(mod p)=(mod p)
Is equal to F (Phi (P)) = (P MOD)
We found whereby f (x) is recursive, but requires the termination condition, when phi (phi (... phi (p))) == 1 when any number is equal to the MOD end condition 1 0.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include<queue>
#define MAX_len 50100*4
using namespace std;
typedef long long ll;
char b[1000100];
ll a,mod;
ll euler(ll n)
{
ll i,j,res=n,temp=n;
for(i=2;i*i<=temp;i++)
{
if(temp%i==0)
{
res=res/i*(i-1);
while(temp%i==0)
temp/=i;
}
}
if(temp>1)
res=res/temp*(temp-1);
return res;
}
ll quickpow(ll a,ll n,ll mod)
{
ll res=1;
while(n)
{
if(n&1)
{
res=(res*a)%mod;
}
n>>=1;
a=(a*a)%mod;
}
return res%mod;
}
ll solve(ll mod)
{
if(mod==1)
return 0;
return quickpow(2,solve(euler(mod))+euler(mod),mod);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
ll p;
scanf("%lld",&p);
printf("%lld\n",solve(p));
}
return 0;
}