Collision

Time Limit: 15000/15000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others)

Total Submission(s): 864 Accepted Submission(s): 206

Problem Description
Matt is playing a naive computer game with his deeply loved pure girl.

The playground is a rectangle with walls around. Two balls are put in different positions inside the rectangle. The balls are so tiny that their volume can be ignored. Initially, two balls will move with velocity (1, 1). When a ball collides with any side of the rectangle, it will rebound without loss of energy. The rebound follows the law of refiection (i.e. the angle at which the ball is incident on the wall equals the angle at which it is reflected).

After they choose the initial position, Matt wants you to tell him where will the two balls collide for the first time.

Input
The first line contains only one integer T which indicates the number of test cases.

For each test case, the first line contains two integers x and y. The four vertices of the rectangle are (0, 0), (x, 0), (0, y) and (x, y). (1 ≤ x, y ≤ 105)

The next line contains four integers x1, y1, x2, y2. The initial position of the two balls is (x1, y1) and (x2, y2). (0 ≤ x1, x2 ≤ x; 0 ≤ y1, y2 ≤ y)

Output
For each test case, output “Case #x:” in the first line, where x is the case number (starting from 1).

In the second line, output “Collision will not happen.” (without quotes) if the collision will never happen. Otherwise, output two real numbers xc and yc, rounded to one decimal place, which indicate the position where the two balls will first collide.

Sample Input

3

10 10

1 1 9 9

10 10

0 5 5 10

10 10

1 0 1 10

Sample Output

Case #1:

6.0 6.0

Case #2:

Collision will not happen.

Case #3:

6.0 5.0



Hint

In first example, two balls move from (1, 1) and (9, 9) both with velocity (1, 1), the ball starts from (9, 9) will rebound at point (10, 10) then move with velocity (−1, −1). The two balls will meet each other at (6, 6).

Source
2014ACM/ICPC亚洲区北京站-重现赛(感谢北师和上交)


解析:扩展欧几里得。参考[http://www.cnblogs.com/TenderRun/p/5943453.html](http://www.cnblogs.com/TenderRun/p/5943453.html)。


```
#include
typedef long long ll;

ll ta,tb,x,y, time;

int T,n,m,x1,y1,x2,y2;

ll extgcd(ll a, ll b, ll &x, ll &y)

{

if(b == 0){

x = 1;

y = 0;

return a;

}

ll q = extgcd(b, a%b, y, x);

y -= a/b*x;

return q;

}

int main()

{

int cn = 0;

scanf("%d",&T);

while(T--){

scanf("%d%d%d%d%d%d", &n, &m, &x1, &y1, &x2, &y2);

n = 2 ;m = 2; x1 = 2; y1 = 2; x2 = 2; y2 = 2;

ta = n-(x1+x2)/2; tb = m-(y1+y2)/2;

printf("Case #%d:\n",++cn);

time = -1;

if(x1 == x2 && y1 == y2) time = 0;

else if(y1y2) time = ta;

else if(x1x2) time = tb;

else{

ll d = extgcd(n,m,x,y);

if((tb-ta)%d==0){

x = (tb-ta)/d
x;

x = (x%(m/d)+m/d)%(m/d);

time = ta+n
x;

}

}

if(time == -1)

puts("Collision will not happen.");

else{

x1 = (x1+time)%(2
n); y1 = (y1+ time)%(2
m);

if(x1>n) x1 = 2
n-x1;

if(y1>m) y1 = 2
m-y1;

printf("%.1f %.1f\n", x1/2.0, y1/2.0);

}

}

return 0;

}

HDU 5114 Collision的更多相关文章

  1. 数学(扩展欧几里得算法):HDU 5114 Collision

    Matt is playing a naive computer game with his deeply loved pure girl. The playground is a rectangle ...

  2. HDU 4793 Collision(2013长沙区域赛现场赛C题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4793 解题报告:在一个平面上有一个圆形medal,半径为Rm,圆心为(0,0),同时有一个圆形范围圆心 ...

  3. HDU 4793 Collision (2013长沙现场赛,简单计算几何)

    Collision Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  4. HDU 5114 扩展欧几里得

    题目大意:给你两个球的坐标 他们都往(1, 1)这个方向以相同的速度走,问你他们在哪个位置碰撞. 思路:这种题目需要把x方向和y方向分开来算周期,两个不同周期需要用扩展欧几里得来求第一次相遇. #in ...

  5. HDU 4793 Collision (解二元一次方程) -2013 ICPC长沙赛区现场赛

    题目链接 题目大意 :有一个圆硬币半径为r,初始位置为x,y,速度矢量为vx,vy,有一个圆形区域(圆心在原点)半径为R,还有一个圆盘(圆心在原点)半径为Rm (Rm < R),圆盘固定不动,硬 ...

  6. HDU 4793 Collision --解方程

    题意: 给一个圆盘,圆心为(0,0),半径为Rm, 然后给一个圆形区域,圆心同此圆盘,半径为R(R>Rm),一枚硬币(圆形),圆心为(x,y),半径为r,一定在圆形区域外面,速度向量为(vx,v ...

  7. 2014ACM/ICPC亚洲区北京站题解

    本题解不包括个人觉得太水的题(J题本人偷懒没做). 个人觉得这场其实HDU-5116要比HDU-5118难,不过赛场情况似乎不是这样.怀疑是因为老司机带错了路. 这套题,个人感觉动态规划和数论是两个主 ...

  8. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C.Ray Tracing (模拟或扩展欧几里得)

    http://codeforces.com/contest/724/problem/C 题目大意: 在一个n*m的盒子里,从(0,0)射出一条每秒位移为(1,1)的射线,遵从反射定律,给出k个点,求射 ...

  9. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C. Ray Tracing 数学

    C. Ray Tracing 题目连接: http://codeforces.com/contest/724/problem/C Description oThere are k sensors lo ...

随机推荐

  1. Spring Boot——2分钟构建spring web mvc REST风格HelloWorld

    之前有一篇<5分钟构建spring web mvc REST风格HelloWorld>介绍了普通方式开发spring web mvc web service.接下来看看使用spring b ...

  2. 玩转图片Base64编码

    什么是 base64 编码? 图片的 base64 编码就是可以将一副图片数据编码成一串字符串,使用该字符串代替图像地址. 这样做有什么意义呢?我们知道,我们所看到的网页上的每一个图片,都是需要消耗一 ...

  3. PHP中发送邮件的几种方法总结

    1. 使用 mail() 函数 没什么好讲的,就是使用系统自带的smtp系统来发送,一般是使用sendmail来发.这个按照各个系统不同而定.使用参考手册. 2. 使用管道的形式 昨天刚测试成功,使用 ...

  4. ElasticSearch Java api 详解_V1.0

    /×××××××××××××××××××××××××××××××××××××××××/ Author:xxx0624 HomePage:http://www.cnblogs.com/xxx0624/ ...

  5. [优先队列]HDOJ5289 Assignment

    题意:有多少个区间,区间内最大的数减去最小的数差小于k 对每个数它所在的区间,可以只往前找(类似dp的无后效性) 比如对位置3的数,可以往前找的区间是[3, 3], [2, 3], [1, 3], [ ...

  6. Win7 下硬盘安装Linux Mint 17

    下载Linux Mint 17镜像,放到C盘根目录:解压出mint.iso文件中casper目录下的vmliunz和initrd.lz两个文件,同样放在C盘的根目录里. 在Win7上安装EasyBCD ...

  7. ios开发--清理缓存

    ios文章原文 一段清理缓存的代码如下: dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) , ...

  8. Django模型修改及数据迁移

    Migrations Django中对Model进行修改是件麻烦的事情,syncdb命令仅仅创建数据库里还没有的表,它并不对已存在的数据表进行同步修改,也不处理数据模型的删除. 如果你新增或修改数据模 ...

  9. Ubuntu使用总结

    错误 鼠标闪烁解决 系统设置->显示—>未知显示器->关闭->应用->选择当前配置 提示sudo: unable to resolve host ,亦即无法解析主机. 原 ...

  10. The secret code

    The secret code Input file: stdinOutput file: stTime limit: 1 sec Memory limit: 256 MbAfter returnin ...