Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 93841 | Accepted: 17330 |
Description
Input
Output
Sample Input
1 2 3 4 5
Sample Output
4
Source
#include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> #include <math.h> #include <ctype.h> #include <time.h> #include <queue> #include <iterator> using namespace std; long long X, x, Y, y, m, n, l, d; void exgcd(long long a, long long b, long long & d, long long& x, long long& y) { if (!b) { d = a; x = 1; y = 0; } else { exgcd(b,a%b,d,y,x); y -= x *(a/b); } } long long gcd(long long a, long long b) { if (b == 0) return a; else return gcd(b,a%b); } int main() { long long a, b, c, t; while (scanf("%lld%lld%lld%lld%lld", &X, &Y, &m, &n, &l) != EOF) { a = n - m; b = l; c = X - Y; exgcd(a, b, d, x, y); long long t = gcd(a,b); if (c % t != 0) { printf("Impossible\n"); continue; } /* 通解:x = x0 + b/t * k; y = y0 - a/t * k; */ x = x *(c / t);//一组解 y = y *(c / t); //x0 = x + b / t * k; 任意解 //找到k 使得 x0 最小 long long ans = x*t / b; ans = x - ans*b / t; if (ans < 0) ans += b / t; printf("%lld\n",ans); } return 0; }