Codeforces Codeforces Round #484 (Div. 2) E. Billiard

题目连接:

http://codeforces.com/contest/982/problem/E

Description

Consider a billiard table of rectangular size $n \times m$ with four pockets. Let's introduce a coordinate system with the origin at the lower left corner (see the picture).

There is one ball at the point $(x, y)$ currently. Max comes to the table and strikes the ball. The ball starts moving along a line that is parallel to one of the axes or that makes a $45^{\circ}$ angle with them. We will assume that:

  1. the angles between the directions of the ball before and after a collision with a side are equal,
  2. the ball moves indefinitely long, it only stops when it falls into a pocket,
  3. the ball can be considered as a point, it falls into a pocket if and only if its coordinates coincide with one of the pockets,
  4. initially the ball is not in a pocket.

Note that the ball can move along some side, in this case the ball will just fall into the pocket at the end of the side.

Your task is to determine whether the ball will fall into a pocket eventually, and if yes, which of the four pockets it will be.

Sample Input

4 3 2 2 -1 1

Sample Output

0 0

题意

给定一个球和方向,问能不能在盒子里停下来

Giving a ball and vector, judge it will stop in the box or not

官方题解以及机器翻译。。:

如果您在平面上相对于其两侧对称地反射矩形,则球的新轨迹将更容易。线性轨迹如果是正确的。一个可能的解决方案是

  • 如果矢量与轴成90度角,则写入if-s。
  • 否则,转动场以使影响矢量变为(1,1)。
  • 写出球的直线运动方程: - 1·x + 1·y + C = 0。如果我们用球的初始位置代替,我们可以找到系数C.
  • 请注意,在平面的无限平铺中,可以以(k1·n,k2·m)的形式表示任何孔的坐标。
  • 用球的线的方程中的点的坐标代替。丢番图方程a·k1 + B·k2 = Cis。如果C |可以解决GCD(A,B)。否则,没有解决方案。
  • 在这个丢番图方程的所有解中,我们对正半轴上的最小值感兴趣。
  • 通过查找k1,k2可以很容易地得到相应口袋的坐标
  • 如果需要,将场转回。

If you symmetrically reflect a rectangle on the plane relative to its sides, the new trajectory of the ball will be much easier. Linear trajectory if be correct. One possible solution is:

  • If the vector is directed at an angle of 90 degrees to the axes, then write the if-s.
  • Otherwise, turn the field so that the impact vector becomes (1, 1).
  • Write the equation of the direct motion of the ball:  – 1·x + 1·y + C = 0. If we substitute the initial position of the ball, we find the coefficient C.
  • Note that in the infinite tiling of the plane the coordinates of any holes representable in the form (k1·n, k2·m).
  • Substitute the coordinates of the points in the equation of the line of the ball. The Diophantine equation a·k1 + B·k2 = Cis obtained. It is solvable if C | gcd(A, B). Otherwise, there are no solutions.
  • Of all the solutions of this Diophantine equation, we are interested in the smallest on the positive half-axis.
  • By finding k1, k2 it is easy to get the coordinates of the corresponding pocket
  • Rotate the field back if required.

代码

#include <bits/stdc++.h>

using namespace std;

long long x, y, xx, yy;
long long vx, vy;
long long fx, fy;
long long c; long long ex_gcd(long long a, long long b, long long &xa, long long &ya) {
if (!b) {
xa = c;
ya = 0;
return a;
}
long long ret = ex_gcd(b, a % b, xa, ya);
long long temp = xa;
xa = ya;
ya = temp - (a / b) * ya;
return ret;
} int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cerr.tie(nullptr); cin >> x >> y >> xx >> yy >> vx >> vy;
if (!vx) {
if (xx == 0 || xx == x) {
if (vy == 1) {
cout << xx << " " << y;
} else {
cout << xx << " " << 0;
}
} else
return 0 * puts("-1");
return 0;
}
if (!vy) {
if (yy == 0 || yy == y) {
if (vx == 1) {
cout << x << " " << yy;
} else {
cout << 0 << " " << yy;
}
} else
return 0 * puts("-1");
return 0;
} if (vx == -1) fx = 1, xx = x - xx;
if (vy == -1) fy = 1, yy = y - yy; c = xx - yy;
if (c % __gcd(x, y))
return 0 * puts("-1");
c /= __gcd(x, y);
long long m = y / __gcd(x, y);
long long xxx, yyy;
ex_gcd(x, y, xxx, yyy);
xxx = (xxx % m + m - 1) % m + 1;
yyy = -(yy - xx + x * xxx) / y;
long long ansn = x, ansm = y;
if (xxx % 2 == 0) ansn = x - ansn;
if (yyy % 2 == 0) ansm = y - ansm;
if (fx) ansn = x - ansn;
if (fy) ansm = y - ansm;
cout << ansn << " " << ansm;
}

Codeforces Codeforces Round #484 (Div. 2) E. Billiard的更多相关文章

  1. 【数论】【扩展欧几里得】Codeforces Round #484 (Div. 2) E. Billiard

    题意:给你一个台球桌面,一个台球的初始位置和初始速度方向(只可能平行坐标轴或者与坐标轴成45度角),问你能否滚进桌子四个角落的洞里,如果能,滚进的是哪个洞. 如果速度方向平行坐标轴,只需分类讨论,看它 ...

  2. Codeforces Codeforces Round #484 (Div. 2) D. Shark

    Codeforces Codeforces Round #484 (Div. 2) D. Shark 题目连接: http://codeforces.com/contest/982/problem/D ...

  3. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  4. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  5. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  6. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

  7. Codeforces Beta Round #76 (Div. 2 Only)

    Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...

  8. Codeforces Beta Round #75 (Div. 2 Only)

    Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...

  9. Codeforces Beta Round #74 (Div. 2 Only)

    Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...

随机推荐

  1. 初学python笔记----字符串

    ---恢复内容开始--- 1.在python中,用引号括起来的都是字符串,引号可以是单引号,也可以是双引号 2.修改字符串的大小写 3.字符串拼接用“+” 4.制表符("\t"), ...

  2. ALV 动态行列

    动态ALV显示的行列,解决部分报表需求. 运行截图如下: 选择屏幕: ALV输出效果1: ALV输出效果2:: ABAP代码: *&------------------------------ ...

  3. C# 读取TXT文本数据 添加到数据库

    protected void Button1_Click(object sender, EventArgs e) { //使用FileStream读取文件 FileStream fileStream ...

  4. 项目总结之Oauth2.0免登陆及相关知识点总结

    简介Oauth2.0授权步骤 授权码模式的基本步骤 原文链接地址 (A)用户访问客户端,后者将前者导向认证服务器. (B)用户选择是否给予客户端授权. (C)假设用户给予授权,认证服务器将用户导向客户 ...

  5. HttpSession原理及Session冲突

    一.摘要         本文讨论了web服务器靠session id识别客户端.以及透过原理分析session冲突的原因,发现session冲突的原因是保存session id信息的cookie发生 ...

  6. 牛客小白月赛13 小A的回文串(Manacher)

    链接:https://ac.nowcoder.com/acm/contest/549/B来源:牛客网 题目描述 小A非常喜欢回文串,当然我们都知道回文串这种情况是非常特殊的.所以小A只想知道给定的一个 ...

  7. 【转】Cisco交换机策略路由

    [转自]https://blog.csdn.net/kkfloat/article/details/39940623 1.概念 1)策略路由(PBR)是一种比基于目标网络进行路由更加灵活的数据包路由转 ...

  8. python爬虫系列之初识爬虫

    前言 我们这里主要是利用requests模块和bs4模块进行简单的爬虫的讲解,让大家可以对爬虫有了初步的认识,我们通过爬几个简单网站,让大家循序渐进的掌握爬虫的基础知识,做网络爬虫还是需要基本的前端的 ...

  9. Python基础-python流程控制之顺序结构和分支结构(五)

    流程控制 流程:计算机执行代码的顺序,就是流程 流程控制:对计算机代码执行顺序的控制,就是流程控制 流程分类:顺序结构.选择结构(分支结构).循环结构 顺序结构 一种代码自上而下执行的结构,是pyth ...

  10. project5 大数据

    [概念] build和run不是一样的,要看输出,不要误解. [方法论] 先要搞懂每个方法的使用,不能乱写.文件名不要写成字符串内容. 又忘记用lab code了,该死. programcreek是个 ...