洛谷 - P1516
题意
两只青蛙各自朝西跳,直到碰面为止。你被要求写一个程序来判断这两只青蛙是否能够碰面,会在什么时候碰面。
我们把这两只青蛙分别叫做青蛙 A 和青蛙 B,并且规定纬度线上东经
思路
t (m - n) + k l = y - x
设跳了
- 显然
x + t \times m \equiv y + t \times n \pmod{l} - 设
x + t \times m + k \times l = y + t \times n - 变形得
t \times (m - n) + k \times l = y - x
可以将
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
ll _x, _y, m, n, l;
ll len;
ll x, y;
ll d;
ll exgcd(ll a, ll b){
if(!b){
x = 1, y = 0;
return a;
}
ll d = exgcd(b, a % b);
ll x_0 = x, y_0 = y;
x = y_0;
y = x_0 - (a / b) * y_0;
return d;
}
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> _x >> _y >> m >> n >> l;
d = exgcd(m - n, l);
len = _y - _x;
if(len % d){
cout << "Impossible";
exit(0);
}
x *= len / d;
y *= len / d;
///cout << x << ' ' << y << endl;
l /= abs(d);
x = (x % l + l) % l;
if(!x){
x += l;
}
cout << x;
return 0;
}
/***
t * (m - n) + k * l = y - x
***/