Description
Input
Output
Sample Input
1 2 3 4 5
Sample Output
4
#include <iostream> using namespace std; typedef long long int64; //递归版的扩展欧几里德 int64 ex_gcd(int64 a,int64 b, int64 & x,int64 &y){ if(b == 0){ x = 1; y = 0; return a; }else{ int64 r = ex_gcd(b, a % b ,x,y); int64 tmp = x; x = y; y = tmp - a/b * y; return r; } } int main() { int64 m,n,x,y,L; while( cin >> x >> y >> m >> n >> L){ int64 A = x - y;// 构造方程 Ax + By = d (d = gcd) int64 B = n - m; int64 X0,Y0; int64 gcd = ex_gcd(B, L, X0, Y0); int64 r = L / gcd; if(A % gcd) cout << "Impossible" << endl; else cout << ( A / gcd * X0 % r + r) % r << endl; } return 0; }