题意
有两只青蛙,在一个首位相接的路上,路的长度为, 它们的起点不同,一个在,一个在,它们的弹跳力也不同,一个青蛙一次可以跳米,一个青蛙一次可以跳米,问你他们同时起跳的话,多久可以相遇(也可能这辈子都遇不到了)
思路
假设跳了次他们相遇了,那么我们可以得到如下方程 经过移项,我们可以得到
因为和都是常数,所以我们得到如下方程
这个不就是拓展exgcd来求解a和b吗,无解的情况就是
只是因为有负数,所以处理起来比较复杂,并且因为我们要求出最小正数,所以我们也要特殊处理下
代码
/**
* author: andif
* created: 23.07.2023 22:29:47
**/
#include<bits/stdc++.h>
using namespace std;
#define de(x) cerr << #x << " = " << x << endl
#define dd(x) cerr << #x << " = " << x << " "
#define rep(i, a, b) for(int i = a; i < b; ++i)
#define per(i, a, b) for(int i = a; i > b; --i)
#define mt(a, b) memset(a, b, sizeof(a))
#define sz(a) (int)a.size()
#define fi first
#define se second
#define qc ios_base::sync_with_stdio(0);cin.tie(0)
#define eb emplace_back
#define all(a) a.begin(), a.end()
using ll = long long;
using db = double;
using pii = pair<int, int>;
using pdd = pair<db, db>;
using pll = pair<ll, ll>;
using vi = vector<int>;
const db eps = 1e-9;
ll exgcd(ll a, ll b, ll & x, ll & y) {
if (!b) return x = 1, y = 0, a;
ll g = exgcd(b, a % b, x, y);
tie(x, y) = make_tuple(y, x - y * (a / b));
return g;
}
int main() {
ll x, y, m, n, L;
cin >> x >> y >> m >> n >> L;
ll a, b;
ll g = n - m > 0 ? exgcd(n - m, L, a, b) : exgcd(m - n, L, a, b);
ll c = n - m > 0 ? x - y : y - x;
if (c % g) return cout << "Impossible" << '\n', 0;
ll d = c / g > 0 ? c / g : -c / g;
if (c >= 0) {
if (a >= 0) {
cout << a * d - (a * d) / (L / g) * (L / g) << '\n';
} else {
cout << a * d + (-a * d + (L / g) - 1) / (L / g) * (L / g) << '\n';
}
} else {
a = -a;
if (a >= 0) {
cout << a * d - (a * d) / (L / g) * (L / g) << '\n';
} else {
cout << a * d + (-a * d + (L / g) - 1) / (L / g) * (L / g) << '\n';
}
}
return 0;
}