Ultraviolet A 11657,

Source: Internet
Author: User

Ultraviolet A 11657,

Link: Ultraviolet A 11657-Rational Billiard

Given a boundary M, N, and the position of the first ball and the second ball, the first ball moves in the direction of p and q and is rebounded when it hits the boundary, consistent with the line of light, ask if it is possible to concentrate the second ball.

Solution: refer to other people's ideas on the Internet. First, expand the corresponding multiples of the horizontal and vertical coordinates to ensure that each movement of p and q corresponds to the unit length on the new plane, then, you only need to consider the number of steps required for horizontal movement. After the number of steps required for vertical movement is reduced, whether the remaining steps meet the multiples of the cycle is enough. Consider four situations. The data for this question is very watery. At first, the coordinates were written incorrectly.

#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;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.