zoukankan      html  css  js  c++  java
  • 扩展欧几里得

    扩展欧几里得求不定方程

    题目链接:http://poj.org/problem?id=1061

    $x imes a+y imes b=gcd(a,b) ightarrow y imes b+left ( x-frac{a}{b} imes y ight ) imes left ( a\%b ight )=gcd(a,b)$

    对于知道特解$x_{1}$和$y_{1}$通解为:$x=x_{1}-t imes  frac{b}{gcd(a,b)} $   $y=y_{1}-t imes frac{a}{gcd(a,b)}$  $tepsilon Z$

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define ll long long
    ll gcdd(ll a,ll b,ll &x,ll &y)//    a*x+b*y=gcd(a,b)
    {
        if(b==0)
        {
            x=1;
            y=0;
            return a;
        }
        ll res=gcdd(b,a%b,x,y);
        ll temp=x;
        x=y;
        y=temp-a/b*y;
        return res;
    }
    int main()
    {
        ll x,y,m,n,L,k,T;
        while(cin>>x>>y>>m>>n>>L)
        {
            ll gcd=gcdd(m-n,L,k,T);
            if((y-x)%gcd!=0)
            {
                cout<<"Impossible"<<endl;
            }
            else
            {
                ll mmp=(y-x)/gcd;
                k*=mmp;
                ll k0=k%abs(L/gcd);
                if(k0<0)k0+=abs(L/gcd);
                cout<<k0<<endl;
            }
        }
        return 0;
    }
  • 相关阅读:
    【Linux】命令——基本命令
    正则表达式
    Letex
    Markdown
    文本编辑器Vim
    【Linux】集群
    【Linux】软件安装
    共线性synteny
    windows触控手势
    【Linux】bin结尾的安装包
  • 原文地址:https://www.cnblogs.com/carcar/p/9522854.html
Copyright © 2011-2022 走看看