标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 103163 | Accepted: 20023 |
Description
Input
Output
Sample Input
1 2 3 4 5
Sample Output
4
通过“扩展欧几里德”,求解:ax+by=c的整数解(x, y)
这题需要long long :)
#include <cstdio> #include <iostream> #include <algorithm> using namespace std; typedef long long LL; int exgcd(int a, int b, LL &x, LL &y) { if(b==0) { x = 1; y = 0; return a; } int d = exgcd(b, a%b, x, y); int t = x; x = y; y = t-a/b*y; return d; } bool f(int a, int b, int c, LL &x, LL &y) { int d = exgcd(a, b, x, y); if(c%d) return 0; int k = c/d; x *= k, y *= k; int t = b/d; while(x<0) { x += t; } while(x-t >=0) { x -= t; } return 1; } int main () { LL x, y; int m, n, L; scanf("%d%d%d%d%d", &x, &y, &m, &n, &L); if(n<m) { swap(n, m); swap(x, y); } if(f(n-m, L, x-y, x, y)) { printf("%lld\n", x); } else { printf("Impossible\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/AcIsFun/p/5393550.html