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. Android Service 的一些笔记

    绑定服务: 用于间接调用服务里面的方法.如果调用者Activity被销毁了,服务也跟着销毁了,服务也会跟着销毁. 开启服务: 不可以调用服务里面的方法.如果调用者的Activity退出了,服务还会长期 ...

  3. OpenStack重启之后,dashboard登录不上去

    ubuntu 12.04装好openstack之后,安装成功,终端打出如下信息: Horizon is now available at http://192.168.0.2/Keystone is ...

  4. Excel操作--使用NPOI导入导出Excel为DataTable

    1.ExcelHelper封装 namespace NPOI操作Excel { public class ExcelHelper { /// <summary> /// DataTable ...

  5. Android Handler消息传递

    一.背景 出于性能优化考虑,Android的UI操作并不是线程安全的,这意味着如果有多个线程并发操作UI组件,可能导致线程安全问题.为了解决这个问题,Android制定了一条简单的原则:只允许UI线程 ...

  6. 告别无止境的增删改查:Java代码生成器

    对于一个比较大的业务系统,我们总是无止境的增加,删除,修改,粘贴,复制,想想总让人产生一种抗拒的心里.那有什么办法可以在正常的开发进度下自动生成一些类,配置文件,或者接口呢?   有感于马上要做个比较 ...

  7. Django admin site(二)ModelAdmin methods

    ModelAdmin methods save_model(request, obj, form, change) 此方法为admin界面用户保存model实例时的行为.request为HttpReq ...

  8. 一个小应用的dbcp和c3p0配置实例

    以下是一个小应用的数据库连接池配置,包括DBCP和C3P0的配制方法 因为是小应用,完全不涉及访问压力,所以配置上采取尽量节约数据库资源的方式 具体要求如下:初始化连接数为0连接不够,需要新创建时,每 ...

  9. MFC多文档中opencv处理图像打开、保存

    需要在C**Doc和C**View中进行相应修改 图像打开: Doc.cpp中: BOOL CCVMFCDoc::Load(IplImage** pp, LPCTSTR csFilename) { I ...

  10. 2008年我买了一本书 书名叫“PHP 6”

    上个星期天,我感觉应该整理一下我的书柜.于是,在书柜里,我发现了一本几乎完全忘记的书(我想不起来为什么要买它!):<PHP 6 – 快速简洁的Web开发> 这本书出版于2008年一月.而六 ...