题目链接
给a,b,p。有b个a的幂
#include <iostream>
using namespace std;
typedef long long LL;
const LL N = 1e6+50;
LL phi[N], vis[N], prime[N], cnt;
void Euler()
{
for(LL i = 2;i < N;++i)
{
if(!vis[i])
{
prime[cnt++] = i;
phi[i] = i-1;
}
for(int j = 0;j < cnt && i*prime[j] < N;++j)
{
vis[i*prime[j]] = 1;
if(i%prime[j] == 0)
{
phi[i*prime[j]] = phi[i]*prime[j];
break;
}
phi[i*prime[j]] = phi[i]*(prime[j]-1);
}
}
phi[1] = 1;
}
LL q_pow(LL a, LL b, LL p)
{
LL ret = 1;
while(b)
{
if(b&1) ret = ret*a>p?ret*a%p+p:ret*a;
b >>= 1;
a = a*a>p?a*a%p+p:a*a;
}
return ret;
}
LL solve(LL a, LL b, LL m)
{
if(m == 1 || b == 0) return 1;//phi(m) < m,所以最后m为1
return q_pow(a, solve(a, b-1, phi[m]), m);
}
int main()
{
Euler();
int T;scanf("%d",&T);
while(T--)
{
LL a, b, m;
scanf("%lld%lld%lld", &a, &b, &m);
printf("%lld
", solve(a, b, m)%m);
}
return 0;
}