题目大意:给你两个球的坐标 他们都往(1, 1)这个方向以相同的速度走,问你他们在哪个位置碰撞。
思路:这种题目需要把x方向和y方向分开来算周期,两个不同周期需要用扩展欧几里得来求第一次相遇。
#include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk make_pair #define pii pair<int, int> #define y1 skldjfskldjg #define y2 skldfjsklejg using namespace std; const int N = 1e6 + 7; const int M = 1e5 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 +7; LL n, m, x1, x2, y1, y2; LL exgcd(LL a, LL b, LL &x, LL &y) { if(!b) { x = 1; y = 0; return a; } else { LL gcd, t; gcd = exgcd(b, a % b, x, y); t = x; x = y; y = t - (a / b) * y; return gcd; } } int main() { int T; scanf("%d", &T); for(int cas = 1; cas <= T; cas++) { LL t = -1; scanf("%lld%lld%lld%lld%lld%lld", &n, &m, &x1, &y1, &x2, &y2); n <<= 1; m <<= 1; x1 <<= 1; y1 <<= 1; x2 <<= 1; y2 <<= 1; printf("Case #%d: ", cas); LL ta = n - (x1 + x2) / 2; LL tb = m - (y1 + y2) / 2; if(x1 == x2 && y1 == y2) t = 0; else if(x1 == x2) t = tb; else if(y1 == y2) t = ta; else { LL x, y, gcd; gcd = exgcd(n, m, x, y); if((tb - ta) % gcd == 0) { x = (tb - ta) / gcd * x; x = (x % (m / gcd) + m / gcd) % (m / gcd); t = ta + n * x; } } if(t == -1) { puts("Collision will not happen."); } else { x1 = (x1 + t) % (2 * n); y1 = (y1 + t) % (2 * m); if(x1 > n) x1 = 2 * n - x1; if(y1 > m) y1 = 2 * m - y1; printf("%.1f %.1f ", x1 /2.0, y1 /2.0); } } return 0; } /* */