POJ-1061-青蛙的约会
这道题就是简单的扩展欧几里得的运用~
什么是扩展欧几里得?请参考博客链接:
https://blog.csdn.net/qq_44624316/article/details/105467866
题面是中文~就不解释啦
设青蛙a初始坐标为xx,青蛙a一次跳m个单位,青蛙b初始坐标为yy,青蛙b一次跳n个单位。L是纬线的长度。
(其实我还百度了一下纬线是纵向还是横向啊哈哈哈,是横向哈~)
我们设跳t次后可以相遇。
那么青蛙a跳的距离:
xx + t * m;
青蛙b跳的距离为:
yy + t * n;
他们相遇要满足:
(xx + t*m) % L = (yy + t * n) % L;
那么去掉求余符号我们可以得到:
xx + t * m - yy - t * n = kL;(k是倍数关系)
结合项可以得到我们的二元一次形式:
(n - m) * t + k * L = xx - yy
二元一次的一般形式:ax + by = c
所以就可以用我们的exgcd啦~
代码部分:
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
ll xx, yy, m, n, L;
void exgcd(ll a, ll b, ll &x, ll &y)
{
if (!b)
{
x = 1;
y = 0;
return ;
}
exgcd(b, a % b, x, y);
ll temp = x;
x = y;
y = temp - (a / b) * y;
}
int main()
{
cin >> xx >> yy >> m >> n >> L;
ll a = n - m;
ll b = L;
ll c = xx - yy;
ll x, y;
ll d = __gcd(a, b);
if (c % d)
{
cout << "Impossible\n";
}
else
{
a /= d;
b /= d;
c /= d;
exgcd(a, b, x, y);
x *= c;
x %= b;
while (x < 0)
{
x += b;
}
cout << x << endl;
}
return 0;
}