poj1061 Exgcd

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
#define ll long long

int gcd(ll a,ll b)
{
    return b==0?a:gcd(b,a%b);
}

void exgcd(ll a,ll b,ll &x,ll &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return;
    }
    ll x1,y1;
    exgcd(b,a%b,x1,y1);
    x=y1;
    y=x1-(a/b)*y1;
}
int main()
{
    ll x,y,m,n,l;
    ll t,k,p;
    while(cin>>x>>y>>m>>n>>l)
    {
        p=gcd(n-m,l);
        if((x-y)%p)
            cout<<"Impossible"<<endl;
        else
        {
            exgcd(n-m,l,t,k);
            t=t*(x-y)/p;
            t=(t%(l/p)+(l/p))%(l/p);
            cout<<t<<endl;
        }
    }
    return 0;
}
//d求得的是最大公约数
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
#define ll long long

void exgcd(ll a,ll b,ll &d,ll &x,ll &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        d=a;
        return;
    }
    ll x1,y1;
    exgcd(b,a%b,d,x1,y1);
    x=y1;
    y=x1-(a/b)*y1;
}
int main()
{
    ll x,y,m,n,l;
    ll t,k,p;
    while(cin>>x>>y>>m>>n>>l)
    {
        exgcd(n-m,l,p,t,k);
        if((x-y)%p)
            cout<<"Impossible"<<endl;
        else
        {
            t=t*(x-y)/p;
            t=(t%l+l)%l;
            cout<<t<<endl;
        }
    }
    return 0;
}

 

posted @ 2016-09-28 19:07  a_clown_cz  阅读(112)  评论(0)    收藏  举报