POJ.1061 青蛙的约会 (拓展欧几里得)
题意分析
我们设两只小青蛙每只都跳了X次,由于他们相遇,可以得出他们同余,则有:
代码总览
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long 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()
{
//freopen("in.txt","r",stdin);
ll x,y,n,m,l;
while(scanf("%I64d %I64d %I64d %I64d %I64d",&x,&y,&m,&n,&l)!= EOF){
ll a = m-n, b = l, c = y-x, X, Y,gcd;
exgcd(a,b,gcd,X,Y);
if(c % gcd ){
printf("Impossible\n");
continue;
}else{
ll t = fabs(b/gcd);
X = X * (c / gcd);
ll k = X * gcd / b;
X = X - b / gcd * k;
if(X <0) X+=t;
printf("%I64d\n",X);
}
}
return 0;
}