Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 119313 | Accepted: 25121 |
Description
我們把這兩隻青蛙分別叫做青蛙A和青蛙B,並且規定緯度線上東經0度處爲原點,由東往西爲正方向,單位長度1米,這樣我們就得到了一條首尾相接的數軸。設青蛙A的出發點座標是x,青蛙B的出發點座標是y。青蛙A一次能跳m米,青蛙B一次能跳n米,兩隻青蛙跳一次所花費的時間相同。緯度線總長L米。現在要你求出它們跳了幾次以後纔會碰面。
Input
Output
Sample Input
1 2 3 4 5
Sample Output
4
思路:解二元方程(m-n)t+kL=y-x,t,k爲未知數,求最小正整數 t 值得解,利用拓展歐幾里得來求
上代碼:
/*
我們知道:若一組 < x, y > 是ax + by = c的一組解,那麼<x−b/d, y + a/d>也是方程的一組解,d
爲gcd(a,b),這樣我們只需要讓解得的x不斷減b/d,直到再減就爲負數時,所得的x就是我們要的解。
等式ax+by=gcd(a,b)必有整數解,用拓展歐幾里得
*/
#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdio>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
#define mod 1e9+7
#define ll long long
#define maxn 200+10
ll exgcd(ll a, ll b, ll &x, ll &y)
{
if (b == 0)
{
x = 1, y = 0;
return a;
}
ll t = exgcd(b, a%b, x, y);//最小公因數
ll temp = x;
x = y, y = temp - a / b*y;
return t;
}
int main()
{
//freopen("Text.txt", "r", stdin);
ll x, y, m, n, L;
while (~scanf("%lld%lld%lld%lld%lld", &x, &y, &m, &n, &L))
{
if (m == n)
printf("Impossible\n");
else
{
if (m < n)
swap(m, n), swap(x, y);
//(m-n)t+kL=y-x
ll t, k;
ll c = y - x;//c%d!=0無解,因爲解不是整數,c=c/gcd(m-n,L)+gcd(m-n,L)
ll d = exgcd(m - n, L, t, k);
if (c%d != 0)//是否有整數解,這時的解x-L/d=x-L,求最小的正整數=(x%L+L)%L,怕爲負數
printf("Impossible\n");
else
printf("%lld\n", ((t*c / d) % L + L) % L);//之所以這樣做是怕t是負值
}
}
return 0;
}