#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;
}

 

相关文章:

  • 2021-07-28
  • 2022-02-26
  • 2022-01-01
  • 2021-06-07
  • 2021-07-24
  • 2022-12-23
猜你喜欢
  • 2021-11-11
  • 2022-12-23
  • 2021-12-23
  • 2021-09-28
相关资源
相似解决方案