思路:欧拉降幂后指数部分是n%f(p),f(p)=p-1,所以是n%(p-1),由于n特别大,字符串取模。
假设x是被除数.
x=(x1*10000%mod+x2*1000%mod+x3*100%mod+x4*10%mod+x5*1%mod)%mod;答案等价x%mod
字符串取mod后指数在1e7范围内,上快速幂即可.
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+1000;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar(); while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL phi[maxn],primes[maxn],cnt;
bool v[maxn];
LL quick(LL a,LL b,LL p){
LL res=1;
while(b>0){
if(b&1) res=res*a%p;
b>>=1;
a=a*a%p;
}
return res%p;
}
void Euler(LL num){
for(LL i=2;i<num;i++){
if(!v[i]){
primes[++cnt]=i;
phi[i]=i-1;
}
for(LL j=1;j<=cnt;j++){
if(primes[j]*i>num) break;
v[primes[j]*i]=1;
if(i%primes[j]==0) {
phi[i*primes[j]]=phi[i]*primes[j];
break;
}
phi[i*primes[j]]=phi[i]*(primes[j]-1);
}
}
phi[1]=1;
}
LL q_pow(LL a,LL b,LL p){
LL res=1;
while(b){
if(b&1) res=res*a>p?res*a%p+p:res*a;
b>>=1;
a=a*a>p?a*a%p+p:a*a;
}
return res;
}
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(void)
{
///Euler
LL t;t=read();
while(t--){
LL p;cin>>p;
string s;cin>>s;
LL mod=p-1;
LL sum=0;
for(LL i=0;i<s.size();i++){
sum=(sum*10%mod+(s[i]-'0')%mod)%mod;
}
LL ans=quick(p-1,sum,p)%p;
printf("%lld\n",ans);
}
return 0;
}