zoukankan      html  css  js  c++  java
  • 同余方程

    贴个板子。

    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<map>
    #include<cmath>
    #include<algorithm>
    //#include<iostream>
    using namespace std;
    
    #define LL long long
    LL gcd(LL a,LL b) {return b?gcd(b,a%b):a;}
    void ext_gcd(LL a,LL b,LL& d,LL& x,LL& y)
    {
        if (!b) {d=a;x=1;y=0;}
        else {ext_gcd(b,a%b,d,y,x);y-=a/b*x;}
    }
    LL mul_mod(LL a,LL b,LL p) {return a*b%p;}
    LL pow_mod(LL a,LL b,LL p)
    {
        LL tmp=a,ans=1;
        while (b) {if (b&1) ans=ans*tmp%p;tmp=tmp*tmp%p;b>>=1;}
        return ans;
    }
    LL china(int n,int* a,int* m)
    {
        LL M=1,d,ans=0,y;
        for (int i=1;i<=n;i++) M*=m[i];
        for (int i=1;i<=n;i++)
        {
            LL w=M/m[i],tmp;
            ext_gcd(m[i],w,d,tmp,y);
            ans=(ans+y*w*a[i])%M;
        }
        return (ans+M)%M;
    }
    LL inv(LL a,LL p)
    {
        LL d,x,y;
        ext_gcd(a,p,d,x,y);
        return d==1?(x+p)%p:-1;
    }
    LL log_mod(LL a,LL b,LL p)
    {
        LL m,v,e=1;
        m=(int)sqrt(p+0.5);v=inv(pow_mod(a,m,p),p);
        map<int,int> x;x[1]=0;
        for (int i=1;i<m;i++) {e=mul_mod(e,a,p);if (!x.count(e)) x[e]=i;}
        for (int i=0;i<m;i++)
        {
            if (x.count(b)) return i*m+x[b];
            b=mul_mod(b,v,p);
        }
        return -1;
    }
    int main()
    {
        return 0;
    }
  • 相关阅读:
    作业八
    作业七:用户体验设计案例分析
    作业六。合作编程
    作业五:需求分析
    作业四:结对编程2
    作业四:合作
    作业三:词频统计
    学习进度表
    java程序练习
    简单博客练习
  • 原文地址:https://www.cnblogs.com/Blue233333/p/8547334.html
Copyright © 2011-2022 走看看