首页 > 代码库 > uva 11657 - Rational Billiard(数学)

uva 11657 - Rational Billiard(数学)

题目链接:uva 11657 - Rational Billiard

题目大意:给定一个边界M,N,以及第一个球和第二个球的位置,第一个球以p,q的方向移动,碰到边界后被反弹,和光线的路线一致,问有没有可能集中第二个球。

解题思路:在网上参考别人的思路,首先将横纵坐标扩大相应倍数,保证p,q每移动一次对应在新平面为单位长度,然后只需要考虑横向移动所需要的步数,减掉纵向移动所需的步数后,剩余的步数是否满足周期的倍数即可。考虑四种请况。这题数据非常水,一开始坐标写错也过了。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long ll;

ll m, n, x0, y0, x1, y1, p, q;

ll gcd (ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

bool judge () {
    if (p == 0 || q == 0) {
        ll u = p * (y1-y0) - q * (x1-x0);
        return u == 0;
    }

    ll absq = q < 0 ? -q : q;
    ll absp = p < 0 ? -p : p;
    m *= absq; x0 *= absq; x1 *= absq;
    n *= absp; y0 *= absp; y1 *= absp;
    p /= absp; q /= absq;

    ll d1 = q * (y1-y0) - p * (x1-x0);
    ll d2 = q * (y1-y0) - p * (2*m-x1-x0);
    ll d3 = q * (2*n-y1-y0) - p * (x1-x0);
    ll d4 = q * (2*n-y1-y0) - p * (2*m-x1-x0);
    ll g = gcd(2*m, 2*n);

    if (d1 % g == 0) return true;
    if (d2 % g == 0) return true;
    if (d3 % g == 0) return true;
    if (d4 % g == 0) return true;
    return false;
}

int main () {
    while (scanf("%lld%lld%lld%lld%lld%lld%lld%lld", &m, &n, &x0, &y0, &x1, &y1, &p, &q) == 8) {
        if (!(m || n || x0 || y0 || x1 || y1 || p || q))
            break;
        printf("%s\n", judge() ? "HIT" : "MISS");
    }
    return 0;
}