zoukankan      html  css  js  c++  java
  • [SDOI2011]计算器

    bsgs

    #include<cstdio>
    #include<algorithm>
    #include<iostream>
    #include<cmath>
    #include<map>
    typedef long long ll;
    inline int pow(int a,int b,const int mod){
        int ans=1;
        for(;b;b>>=1,a=ll(a)*a%mod)
            if(b&1)ans=ll(ans)*a%mod;
        return ans;
    }
    inline int phi(int mod){
        int ret=mod;
        for(int i=2;i*i<=mod;++i)
            if(mod%i==0){
                ret=ret/i*(i-1);
                while(mod%i==0)mod/=i;
            }
        if(mod!=1)ret=ret/mod*(mod-1);
        return ret;
    }
    inline int inv(int x,const int mod){return pow(x,mod-2,mod);}
    std::map<int,int> map;
    inline int solve(int y,int z,const int mod){
        map.clear();
        int q = sqrtl(mod), q2=q+10;
        for(int i=0,lm=1;i<=q;++i)
            map[lm]=i,lm=ll(lm)*y%mod;
        const int p = inv(pow(y,q,mod),mod);
        int ans=0;
        while(!map.count(z) && --q2)
            z = ll(z)*p%mod,ans+=q;
        if(map.count(z)) return ans + map[z];
        else return -1;
    }
    int main(){
        int t,k;
        std::cin >> t >> k;
        while(t--){
            int y,p,z;
            std::cin >> y >> z >> p;
            if(k == 1)
                std::cout << pow(y,z,p) << '
    ';
            if(k == 2){
                y%=p,z%=p;
                if(y==0&&z!=0){
                    std::cout << "Orz, I cannot find x!
    ";
                    continue;
                }
                std::cout << ll(z)*inv(y,p)%p << '
    ';;
            }
            if(k == 3){
                const int mod = p;
                y %= p,z%=p;
                if(y == 0 && z == 0){
                    std::cout << "1
    ";
                    continue;
                }
                if(y == 0 && z){
                    std::cout << "Orz, I cannot find x!
    ";
                    continue;
                }
                int res = solve(y,z,mod);
                if(res == -1){
                    std::cout << "Orz, I cannot find x!
    ";
                    continue;
                }
                std::cout << res << '
    ';
            }
        }
    }
  • 相关阅读:
    php环境下所有的配置文件以及作用
    获取登陆用户的ip
    curl模拟post和get请求
    linux 下安装php curl扩展
    php常用面试知识点
    git使用步骤
    laravel框架基础知识点
    ci框架基础知识点
    ajax
    Mysql 中需不需要commit
  • 原文地址:https://www.cnblogs.com/skip1978/p/10346370.html
Copyright © 2011-2022 走看看