Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 97776 | Accepted: 18462 |
Description
Input
Output
Sample Input
1 2 3 4 5
Sample Output
4
裸的拓展欧几里得算法,具体可以参考我的另外一篇博文: 《欧几里得 & 拓展欧几里得算法 讲解 (Euclid & Extend- Euclid Algorithm)》
#include <cmath> #include <vector> #include <cstdio> #include <string> #include <cstring> #include <iomanip> #include <iostream> #include <algorithm> using namespace std; //#pragma comment(linker, "/STACK:1024000000,1024000000") #define FIN freopen("input.txt","r",stdin) #define FOUT freopen("output.txt","w",stdout) #define CASE(T) for(scanf("%d",&T);T--;) typedef __int64 ll; void exgcd(ll a, ll b, ll& d, ll& x, ll &y) { if(!b) { d = a, x = 1, y = 0; } else { exgcd(b, a % b, d, y, x); y -= x * (a / b); } } int main() { // FIN; ll x, y, m, n, L; ll a, b, c, d, X, Y; while(~scanf("%I64d %I64d %I64d %I64d %I64d", &x, &y, &m, &n, &L)) { a = m - n, b = -L, c = y - x; exgcd(a, b, d, X, Y); if(c % d != 0) { printf("Impossible\n"); continue; } else { X = -c / d * X; X = ((L - X) % L + L) % L; printf("%I64d\n", X); } } return 0; }