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;

}

最新文章

  1. 54B
  2. Deep Learning(深度学习)学习笔记整理
  3. iOS YSDropdownMagnify 下拉放大,向上导航显示
  4. 在Ubuntu上为Android系统编写Linux内核驱动程序(老罗学习笔记1)
  5. Web版RSS阅读器(三)——解析在线Rss订阅
  6. Kia's Calculation(HDU 4267)
  7. 设置windows密码只存在NTLM-Hash下
  8. Problem N
  9. Apple 内购
  10. Hive管理表分区的创建,数据导入,分区的删除操作
  11. mysql 函数获取子节点
  12. webWorker
  13. 问题:ORA-28000: the account is locked 用户锁住了。
  14. python开发学习-day09(队列、多路IO阻塞、堡垒机模块、mysql操作模块)
  15. TCP/IP协议簇分层详解---转
  16. Sublime Text 3打开gbk编码的文件中文乱码的问题
  17. 阻塞DOM
  18. java序列化,二进制和数据流
  19. PAT 1067. 试密码(20)
  20. butterknif

热门文章

  1. Oracle composite index column ordering
  2. 常见的排序算法之Java代码解释
  3. HDU 1452 Happy 2004(因子和的积性函数)
  4. linux网站推荐
  5. lintcode : 二叉树的层次遍历II
  6. Project Euler 89:Roman numerals 罗马数字
  7. 为什么重写equals方法还要重写hashcode方法?
  8. 什么是spring?
  9. Hibernate逍遥游记-第13章 映射实体关联关系-001用外键映射一对一(<many-to-one unique="true">、<one-to-one>)
  10. PowerDesigner连接Oracle数据库建表序列号实现自动增长