poj1061 Exgcd

时间:2025-02-17 15:02:56
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
#define ll long long int gcd(ll a,ll b)
{
return b==?a:gcd(b,a%b);
} void exgcd(ll a,ll b,ll &x,ll &y)
{
if(b==)
{
x=;
y=;
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 ;
}
//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==)
{
x=;
y=;
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 ;
}