码迷,mamicode.com
首页 > 其他好文 > 详细

cf-787a

时间:2017-05-24 00:45:53      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:out   等价   int   nbsp   namespace   https   需要   include   exgcd   

https://vjudge.net/problem/709847/origin

拓展欧几里德:a*x+b=c*y+d; -> a*x+c*y=d-b;

代码:

#include<bits/stdc++.h>
using namespace std;

#define ll long long

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 tmp=x;
    x=y;
    y=tmp-a/b*y;
    return d;
}

int main(){
    ll a,b,C,D,x,y;
    while(cin>>a>>b>>C>>D){
        if(b<D){//若b<D,那么y<0,
            //原方程式等价于y=(a*x+b-d)/c,
            //下面的处理可保证x不小于0,但是不能保证y,
            //所以需要保证(a*x+b-d)%c==0,即:保证:b>D;若不成立需交换;
            swap(a,C);
            swap(b,D);
        }
        ll d = exgcd(a,C,x,y);
        ll c = D-b;
        if(c%d)  cout<<"-1"<<endl;
        else{
            x=x*(c/d);
            x=(x%(C/d)+(C/d))%(C/d);
            cout<<b+a*x<<endl;
        }
    }
    return 0;
}

cf-787a

标签:out   等价   int   nbsp   namespace   https   需要   include   exgcd   

原文地址:http://www.cnblogs.com/sunowsir/p/6896863.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!