就是求(m-n)*a+b*l=y-x,
类似于求解a*x+b*y=c,r=gcd(a,b),当c%r==0时有解,用exgcd求出a*x+b*y=gcd(a,b)的解,然后x*c/gcd(a,b)就是其中一个解,最后求最小正整数解,就是(x%b+b)%b,要求y的话,对应求解即可
#include<map> #include<set> #include<list> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define se second #define mp make_pair #define pb push_back #define pi acos(-1.0) #define ll long long #define mod 1000000007 #define C 0.5772156649 #define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 #define pil pair<int,ll> #define pii pair<int,int> #define ull unsigned long long #define base 1000000000000000000 #define fio ios::sync_with_stdio(false);cin.tie(0) using namespace std; ; +,maxn=+,inf=0x3f3f3f3f; ll gcd(ll a,ll b) { return b?gcd(b,a%b):a; } ll exgcd(ll a,ll b,ll &x,ll &y) { if(!b) { x=,y=; return a; } ll ans=exgcd(b,a%b,x,y); ll t=x;x=y;y=t-a/b*y; return ans; } int main() { ll x,y,n,m,l,a,b; scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&l); if(m<n)swap(x,y),swap(m,n); ll r=gcd(m-n,l); )*puts("Impossible"); exgcd(m-n,l,a,b); a*=(y-x)/r; a=(a%l+l)%l; printf("%lld\n",a); ; } /******************** ********************/