zoukankan      html  css  js  c++  java
  • 数学#扩展欧几里德 POJ 1061&2115&2891

    寒假做的题了,先贴那时写的代码。

     

    POJ 1061

    #include<iostream>
    #include<cstdio>
    typedef long long LL;
    using namespace std;
    
    void extend_gcd(LL a,LL b,LL &d,LL &x,LL &y)
    {
        if(b==0)
        {
            d=a;
            x=1,y=0;
        }
        else
        {
            extend_gcd(b,a%b,d,y,x);
            y-=x*(a/b);
        }
    }
    
    int main()
    {
        LL x,y,m,n,L;
        while(scanf("%I64d%I64d%I64d%I64d%I64d",&x,&y,&m,&n,&L)!=EOF)
        {
            LL d,xx,yy;
            extend_gcd(n-m,L,d,xx,yy);
            if((x-y)%d==0)
            {
                LL p=L/d;
                xx=(x-y)/d*xx;
                xx=(xx%p+p)%p;
                printf("%I64d
    ",xx);
            }
            else printf("Impossible
    ");
        }
        return 0;
    }

    POJ 2115

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<string>
    #include<cstdlib>
    #include<vector>
    #include<stack>
    #include<map>
    using namespace std;
    typedef long long ll;
    
    ll extend_gcd(ll a,ll b,ll &x,ll &y)
    {  //return d=gcd(a,n);
        if(b==0)
        {
            x=1,y=0;
            return a;
        }
        else
        {
            ll t=extend_gcd(b,a%b,x,y);
            ll xx=x,yy=y;
            x=yy;
            y=xx-(a/b)*yy;
            return t;
        }
    }
    
    int main()
    {
        ll A,B,C,k,x,y;
        while(~scanf("%I64d %I64d %I64d %I64d",&A,&B,&C,&k))
        {
            if(A==0&&B==0&&C==0&&k==0)  break;
            ll a=C,b=B-A,n=(ll)1<<k; //n=2^k
            ll d=extend_gcd(a,n,x,y);
    
            if(b%d!=0) //方程无解
                printf("FOREVER
    ");
            else
            {
                x=(x*(b/d))%n;  //x为方程ax=b(mod n)的最小解
                x=(x%(n/d)+n/d)%(n/d);  //x为方程ax=b(mod n)的最小整数解
                printf("%I64d
    ",x);
            }
        }
        return 0;
    }

    POJ 2891

    #include<iostream>
    #include<cstdio>
    using namespace std;
    typedef long long ll;
    
    void extend_gcd(ll a,ll b,ll &d,ll &x,ll &y)
    {
        if(b==0)
        {
            d=a;
            x=1,y=0;
        }
        else
        {
            extend_gcd(b,a%b,d,y,x);
            y-=x*(a/b);
        }
    }
    
    int main()
    {
        int k;
        ll a1,r1,a,r;
        while(~scanf("%d",&k))
        {
            scanf("%I64d%I64d",&a1,&r1);
            int flag=1;
            for(int i=2;i<=k;i++)
            {
                scanf("%I64d%I64d",&a,&r);
                ll d,x,y;
                extend_gcd(a1,a,d,x,y);
                if((r-r1)%d!=0)
                    flag=0;
                ll p=a/d;
                x=(r-r1)/d*x;
                x=(x%p+p)%p;
                r1=a1*x+r1;
                a1=a1*(a/d);
            }
            if(flag)
                printf("%I64d
    ",r1);
            else printf("-1
    ");
        }
        return 0;
    }
  • 相关阅读:
    程序员修炼之道阅读笔记2
    程序员修炼之道阅读笔记1
    软件体系架构的质量属性
    计算贴现率相关问题
    以《淘宝网》为例,描绘质量属性的六个常见属性场景
    第十四周总结
    软件需求模式阅读笔记
    第十三周总结
    第十二周总结
    重大技术需求进度报告六
  • 原文地址:https://www.cnblogs.com/atmacmer/p/5285837.html
Copyright © 2011-2022 走看看