LUOGU P2485 [SDOI2011]计算器

传送门

解题思路

板子题,第一问快速幂,第二问求逆元,第三问bsgs

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>

using namespace std;
typedef long long LL;

int y,z,p,T,k;
LL ans;
map<LL,int> mp;

inline LL fast_pow(LL x,int y){
    LL ret=1;
    for(;y;y>>=1){
        if(y&1) ret=ret*x%p;
        x=x*x%p;
    }
    return ret;
}

int main(){
    scanf("%d%d",&T,&k);
    if(k==1){
        while(T--){
            scanf("%d%d%d",&y,&z,&p);
            printf("%lld\n",fast_pow(1ll*y,z)%p);
        }
        return 0;
    }
    if(k==2){
        while(T--){
            scanf("%d%d%d",&y,&z,&p);z%=p;
            if(y%p==0) {puts("Orz, I cannot find x!");continue;}
            printf("%lld\n",(LL)z*fast_pow(1ll*y,p-2)%p);
        }
        return 0;
    }
    if(k==3){
        while(T--){
            mp.clear();
            scanf("%d%d%d",&y,&z,&p);z%=p;y%=p;
            if(y==0 && z!=0) {puts("Orz, I cannot find x!");continue;}
            int m=ceil(sqrt(p));
            LL now=z%p;mp[now]=0;
            for(register int i=1;i<=m;i++) {
                now=now*y%p;
                mp[now]=i;
            }
            now=1;bool flag=false;
            LL k=fast_pow(y,m);
            for(register int i=1;i<=m;i++){
                now=now*k%p;
                if(mp[now]){
                    ans=((LL)i*m-mp[now]+p)%p;
                    flag=1;
                    break;
                }
            }
            if(flag) printf("%lld\n",ans);
            else puts("Orz, I cannot find x!");
            ans=0;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值