[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;
}
原文地址:https://www.cnblogs.com/shenben/p/6429937.html