扩展GCD
因为 跳跃的次数相同 ,设跳了S次。
所以就有 s * m - (x -y) - s * n = l * k;
转换一下就是 s *(m - n) + k * l = x - y
就会发现是一个扩展GCD
#include <cstdio> #include <cstring> #include <iostream> using namespace std; typedef long long LL; LL Gcd(LL a,LL b){ LL r = a % b; while(r){ a = b; b = r; r = a % b; } return b; } void Ex_Gcd(LL a,LL b,LL &x,LL &y){ if(!b){ x = 1; y = 0; return ; } Ex_Gcd(b,a % b,x,y); LL temp = x; x = y; y = temp - a / b * y; return ; } int main(){ LL x,y,m,n,l; while(~scanf("%I64d%I64d%I64d%I64d%I64d",&x,&y,&m,&n,&l)){ LL a = n - m,c = x - y; LL d = Gcd(a,l); if(c % d != 0) printf("Impossible\n"); else{ LL x1,y1; Ex_Gcd(a,l,x1,y1); LL k = c / d; x1 *= k; x1 %= l; while(x1 <= 0) x1 +=l; printf("%I64d\n",x1); } } return 0; }