zoukankan      html  css  js  c++  java
  • bzoj2242(高次同余方程,线性同余方程)

    #include<cstdio>
    #include<map>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    
    typedef long long ll;
    
    ll power(ll a,ll b,ll mod){
       ll ans=1%mod;
       for (;b;b>>=1){
        if(b&1) ans=ans*a%mod;
        a=a*a%mod;
       }
       return ans%mod;
    }
    
    ll exgcd(ll a,ll b,ll &x,ll &y){
       if(b==0){
        x=1;
        y=0;
        return a;
       }
       ll d=exgcd(b,a%b,x,y);
       ll z=x;
       x=y;
       y=z-(a/b)*y;
       return d;
    }
    
    ll babe(ll a,ll b,ll p){
       map<int,int>hashl;
       hashl.clear();
       b%=p;
       ll t=(int)sqrt(p)+1;
       for (int j=0;j<t;j++){
        ll val=b*power(a,j,p)%p;
        hashl[val]=j;
       }
       a=power(a,t,p);
       if(a==0) return b==0?1:-1;
       for (int i=0;i<=t;i++){
        ll val=power(a,i,p);
        ll j;
        if(hashl.find(val)==hashl.end()) j=-1;
        else j=hashl[val];
        if(j>=0&&i*t-j>=0) return i*t-j;
       }
        return -1;
    }
    
    int main(){
        int t,k;
        scanf("%d%d",&t,&k);
        ll y,z,p,x;
        if(k==1){
            for (int i=1;i<=t;i++)
            {
            scanf("%lld%lld%lld",&y,&z,&p);
            printf("%lld
    ",power(y,z,p));
            }
        }
        if(k==2){
            ll a,b;
            for (int i=1;i<=t;i++){
                scanf("%lld%lld%lld",&a,&b,&p);
                int d=exgcd(a,p,x,y);
                if(b%d) printf("Orz, I cannot find x!
    ");
                else {
                   ll s=p/d;
                   x=(b/d*x%s+s)%s;//怎样找最小解
                   printf("%lld
    ",x);
                }
            }
        }
        if(k==3){
            ll a,b;
            for (int i=1;i<=t;i++){
                scanf("%lld%lld%lld",&a,&b,&p);
                int h=babe(a,b,p);
                if(h==-1) printf("Orz, I cannot find x!
    ");
                else printf("%lld
    ",h%p);记得mod p
            }
        }
    return 0;
    }
  • 相关阅读:
    Android View 的绘制流程
    Android Studio 注释模板
    Flutter https://flutter.cn/docs学习之 工作原理
    Android 手机兼容差异
    Flutter plugin的两种方式
    本周总结
    mapreduce程序开发简单实例 WordCount
    《需求工程——软件建模与分析》阅读笔记之一
    本周总结
    本周总结
  • 原文地址:https://www.cnblogs.com/lmjer/p/9310212.html
Copyright © 2011-2022 走看看