Description
兩隻青蛙在網上相識了,它們聊得很開心,于是覺得很有必要見一面。它們很高興地發現它們住在同一條緯度線上,于是它們約定各自朝西跳,直到碰面為止。可是它們出發之前忘記了一件很重要的事情,既沒有問清楚對方的特征,也沒有約定見面的具體位置。不過青蛙們都是很樂觀的,它們覺得隻要一直朝着某個方向跳下去,總能碰到對方的。但是除非這兩隻青蛙在同一時間跳到同一點上,不然是永遠都不可能碰面的。為了幫助這兩隻樂觀的青蛙,你被要求寫一個程式來判斷這兩隻青蛙是否能夠碰面,會在什麼時候碰面。
我們把這兩隻青蛙分别叫做青蛙A和青蛙B,并且規定緯度線上東經0度處為原點,由東往西為正方向,機關長度1米,這樣我們就得到了一條首尾相接的數軸。設青蛙A的出發點坐标是x,青蛙B的出發點坐标是y。青蛙A一次能跳m米,青蛙B一次能跳n米,兩隻青蛙跳一次所花費的時間相同。緯度線總長L米。現在要你求出它們跳了幾次以後才會碰面。
Input
輸入隻包括一行5個整數x,y,m,n,L,其中x≠y < 2000000000,0 < m、n < 2000000000,0 < L < 2100000000。
Output
輸出碰面所需要的跳躍次數,如果永遠不可能碰面則輸出一行"Impossible"
Sample Input
1 2 3 4 5
Sample Output
4
擴充gcd的簡單入門題。
#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define fi first
#define se second
#define mp(i,j) make_pair(i,j)
#define pii pair<string,string>
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 0x7FFFFFFF;
const int mod = 9973;
const int N = 5e3 + 10;
const int read()
{
char ch = getchar();
while (ch<'0' || ch>'9') ch = getchar();
int x = ch - '0';
while ((ch = getchar()) >= '0'&&ch <= '9') x = x * 10 + ch - '0';
return x;
}
LL a, b, c, d, e;
LL exgcd(LL a, LL b, LL &x, LL &y)
{
if (!b) { x = 1, y = 0; return a; }
LL g = exgcd(b, a%b, x, y);
LL z = x - a / b * y;
x = y; y = z; return g;
}
int main()
{
while (scanf("%lld%lld%lld%lld%lld", &a, &b, &c, &d, &e) != EOF)
{
LL x, y, g = exgcd(c - d, e, x, y);
if ((b - a) % g) printf("Impossible\n");
else
{
LL f = (b - a) / g * x, h = e / (g > 0 ? g : -g);
printf("%lld\n", (f % h + h) % h);
}
}
return 0;
}