欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

P1516 青蛙的约会

程序员文章站 2022-06-02 19:47:25
...

P1516 青蛙的约会

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll extend_gcd(ll a,ll b,ll &x,ll &y) {
	if (b == 0) {
		x = 1; y = 0; return a;
	}
	ll gcd = extend_gcd(b, a%b, y, x);
	y -= (a/b) * x;
	return gcd;
}

int main() {
	ll x, y, m, n, L; cin >> x >> y >> m >> n >> L;
	ll x0, y0, A = n-m, C = x-y;
	if (A < 0) { A = -A; C = -C;}
	ll gcd = extend_gcd(A, L, x0, y0);
	if (C % gcd == 0) {
		ll ans = (x0*C/gcd % (L/gcd) + L/gcd) % (L/gcd);
		cout << ans << endl;
	} else {
		cout << "Impossible" << endl; 
	}
	return 0;
}
相关标签: ACM-ICPC题集