• 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
    ",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
    ",(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
    ",ans);
                else puts("Orz, I cannot find x!");
                ans=0;
            }
        }
        return 0;
    }
  • 相关阅读:
    unix网络编程源码编译问题
    ubuntu15.04下安装docker
    hexo博客的相关配置
    hexo的jacman主题配置
    使用github和hexo搭建静态博客
    操作系统简单认识
    github for windows安装以及教程
    编译原理第五单元习题
    python3入门之列表和元组
    Python3入门之软件安装
  • 原文地址:https://www.cnblogs.com/sdfzsyq/p/9676854.html
Copyright © 2020-2023  润新知