标签:des style os io div 时间 算法 amp
Description
Input
Output
Sample Input
1 2 3 4 5
Sample Output
4
思路:扩展欧几里得算法,设时间为t,最后在s点相遇,A走了a圈,B走了b圈,
那么我们可以推出: m*t + x = L * a + s; n*t + y = L * b + s,两式相减得:(m-n) * t + (b - a) * L = y - x,正好是扩展欧几里得的形式求两个可行解,那么对于求最小的解动脑子想想就得到了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
typedef long long ll;
using namespace std;
ll gcd(ll a, ll b) {
return b?gcd(b, a%b):a;
}
void exgcd(ll a, ll b, ll &x, ll &y) {
if (b == 0) {
x = 1;
y = 0;
return;
}
exgcd(b, a%b, x, y);
ll t = x;
x = y;
y = t - a / b * y;
return;
}
int main() {
ll x, y, n, m, l;
while (scanf("%lld%lld%lld%lld%lld", &x, &y, &m, &n, &l) != EOF) {
ll a = n-m, b = l, c = x-y, p, q;
ll g = gcd(a, b);
if (c % g) {
printf("Impossible\n");
continue;
}
a /= g, b /= g, c /= g;
exgcd(a, b, p, q);
p *= c;
ll t = p % b;
while (t < 0)
t += b;
printf("%lld\n", t);
}
return 0;
}
POJ - 1061 青蛙的约会 (扩展欧几里得算法),布布扣,bubuko.com
标签:des style os io div 时间 算法 amp
原文地址:http://blog.csdn.net/u011345136/article/details/38373775