CF982E 扩展欧几里得+计算几何

题目大意:

n ∗ m n*m nm大小的桌球台有一个球,坐标为 ( 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+bmanbm=yx,这个显然就是扩展欧几里得呀!
  • 最后根据** 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值