奇葩的是我居然用不了abs去得到longlong的绝对值,于是自己写了一个...
扩展欧几里得即可解决了。
#include<cstdio> #include<cstdlib> #include<iostream> #include<string> #include<set> #include<algorithm> #include<vector> #include<queue> #include<list> #include<cstring> #include<map> #include<stack> using namespace std; #define INF 0x3f3f3f3f #define maxn 1005 #define ull unsigned long long #define ll long long #define hashmod 99999839 #define mod 9997 long long x,y,m,n,l; long long lllabs(long long x){ return (x < 0) ? -x : x; } long long exgcd(long long a,long long b,long long& x,long long& y){ if(!b){x=1ll,y=0ll;return a;} ll g = exgcd(b,a%b,x,y); ll t = x; x = y; y = t - a/b*y; return g; } void solve(){ long long A = m - n,B = l,C = y-x; long long tx,ty; long long g = exgcd(lllabs(A),lllabs(B),tx,ty);//AX+BY=C if(C % g != 0){ puts("Impossible"); return; } if(A < 0) tx = -tx; B /= g; tx = tx * (C / g);//AX + BY = C tx = tx + (lllabs(tx)/B+1ll)*B; tx %= B; printf("%lld ",tx); } int main(){ // freopen("a.in","r",stdin); // freopen("b.out","w",stdout); while(~scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&l)){ solve(); } return 0; }