• [SDOI2011]计算器


    #include<cstdio>
    #include<cmath>
    #include<tr1/unordered_map>
    using namespace std;
    using namespace std::tr1;
    typedef long long ll;
    int T,K;ll mod;
    unordered_map<int,int>a;
    inline ll fpow(ll a,ll p){
        ll res=1;
        for(;p;p>>=1,a=a*a%mod) if(p&1) res=res*a%mod;
        return res;
    }
    void exgcd(ll a,ll b,ll &d,ll &x,ll &y){
        if(!b){d=a;x=1;y=0;return ;}
        exgcd(b,a%b,d,y,x);
        y-=x*(a/b);
    }
    ll gcd(ll a,ll b){
        if(!b) return a;
        return gcd(b,a%b);
    }
    void CE(ll y,ll z){//calculate by exgcd
        ll a=y,b=-mod,d,x;
        d=gcd(a,b);
        if(z%d){printf("Orz, I cannot find x!
    ");return ;}
        a/=d;b/=d;z/=d;
        exgcd(a,b,d,x,y);
        x*=z;x%=b;
        while(x<0) x+=b;
        printf("%lld
    ",x);
    }
    void babystep_gaintstep(ll x,ll z){
        ll k=1,y=z%mod;x%=mod;
        if(!x&&!z){puts("1");return ;}
        if(!x&&z!=1){printf("Orz, I cannot find x!
    ");return ;}
        ll m=ceil(sqrt(mod-1));
        ll ni=fpow(x,m);ni=fpow(ni,mod-2);
        a.clear();
        a[1]=m+1;
        for(ll j=1;j<m;j++){
            k=k*x%mod;
            if(!a[k]) a[k]=j;
        }
        for(ll i=0;i<m;i++){
            ll u=a[y];
            if(u){
                if(u==m+1) printf("%lld
    ",i*m);
                else printf("%lld
    ",i*m+u);
                return ;
            }
            y=y*ni%mod;
        }
        printf("Orz, I cannot find x!
    ");
    }
    int main(){
        scanf("%d%d",&T,&K);
        if(K==1){
            for(int y,z,p;T--;){
                scanf("%d%d%d",&y,&z,&p);
                mod=p;
                printf("%lld
    ",fpow(y,z));
            }
        }
        else if(K==2){
            for(int y,z,p;T--;){
                scanf("%d%d%d",&y,&z,&p);
                mod=p;
                CE(y,z);
            }
        }
        else{
            for(int y,z,p;T--;){
                scanf("%d%d%d",&y,&z,&p);
                mod=p;
                babystep_gaintstep(y,z);
            }    
        }
        return 0;
    }
  • 相关阅读:
    基数排序
    希尔排序
    归并排序
    选择排序
    冒泡排序
    堆排序
    快速排序
    Python selenium 4种常用定位 id,class_name,text,xpath
    python selenium 用索引值定位
    python pycharm 建立新项目时使用上个项目所下载的第三方库方法
  • 原文地址:https://www.cnblogs.com/shenben/p/6429937.html
Copyright © 2020-2023  润新知