I. 迷途的怪物(欧拉降幂+大数取模)


思路:欧拉降幂后指数部分是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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值