Luogu1516 青蛙的约会

推荐一篇题解 https://pks-loving.blog.luogu.org/solution-p1516

#include<iostream>
#include<cstdio>
#define ll long long
using namespace std;
ll x, y, m, n, L, ans, x1, y1;
ll exgcd(ll a, ll b, ll& x1, ll& y1){
    if(!b){
        x1=1;
        y1=0;
        return a;
    }
    ll ans=exgcd(b, a%b, y1, x1);
    y1-=a/b*x1;
    return ans;
}
int main(){
    scanf("%lld%lld%lld%lld%lld", &x, &y, &m, &n, &L);
    ll B=n-m, A=x-y;
    if(B<0) B=-B, A=-A;
    ans=exgcd(B, L, x1, y1);
    if(A%ans!=0)
        cout<<"Impossible";
    else
        cout<<((x1*(A/ans))%(L/ans)+(L/ans))%(L/ans);
    return 0;
}