题目大意:
n ∗ m n*m n∗m大小的桌球台有一个球,坐标为 ( x , y ) (x,y) (x,y),给球任意八个方向(上,下,左,右,左上,左下,右上,右下)的初速度,问最终能否进入四个顶点的球袋,如果可以,最终进入哪个球袋
解题思路:
-
对于上,下,左,右可以特判
-
首先看如果球的初始方向是左下,那么通过对称最终图片是这样
- 如果最终可以进入球袋,那么就要找最小的整数 a a a,满足: x + a n = y + b m → a n − b m = y − x x+an=y+bm \rightarrow an - bm = y - x x+an=y+bm→an−bm=y−x,这个显然就是扩展欧几里得呀!
- 最后根据** a a a, b b b奇偶性来判断在哪个球袋即可**
- 对于其他方向的情况,通过转换都可以是初始方向左下的情况,代码有解释
AC代码:
#include <bits/stdc++.h>
#define ft first
#define sd second
#define pb push_back
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0) //不能跟puts混用
#define seteps(N) fixed << setprecision(N)
#define endl "\n"
const int maxn = 1e5 + 10;
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
const ll mod = 1e9 + 7;
int n, m, x, y, vx, vy;
void Exgcd(ll a, ll b, ll &x, ll &y) {
if (b == 0) x = 1, y = 0;
else Exgcd(b, a % b, y, x), y -= a / b * x;
}
void init() {
if (vx == 1) x = n - x;//若是向右,那么相当于把图横着翻转
if (vy == 1) y = m - y;//相当于竖着翻转
}
int main() {
cin >> n >> m >> x >> y >> vx >> vy;
if (vx == 0) {
if (x == 0 || x == n) {
if (x == 0) {
if (vy == -1) cout << 0 << " " << 0 << endl;
else cout << 0 << " " << m << endl;
}
else {
if (vy == -1) cout << n << " " << 0 << endl;
else cout << n << " " << m << endl;
}
}
else cout << -1 << endl;
}
else if (vy == 0) {
if (y == 0 || y == m) {
if (y == 0) {
if (vx == -1) cout << 0 << " " << 0 << endl;
else cout << n << " " << 0 << endl;
}
else {
if (vx == -1) cout << 0 << " " << m << endl;
else cout << n << " " << m << endl;
}
}
else cout << -1 << endl;
}
else {
init(); //处理不是左下的方向
ll a = n, b = m, ex, ey, c = y - x;
ll d = __gcd(a, b);
if (c % d) {
cout << -1 << endl;
return 0;
}
a /= d, b /= d, c /= d;
Exgcd(a, b, ex, ey);
ex *= c;
ex = (ex % b + b) % b;
a *= d, b *= d;
ey = (a * ex + x - y) / b;
ll ax = 0, ay = 0;
if (ex & 1) ax = n;
if (ey & 1) ay = m;
if (vx == 1) ax = n - ax;//最后再转回来
if (vy == 1) ay = m - ay;
cout << ax << " " << ay << endl;
}
return 0;
}