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

  这道题很有意思。

  为了避免小数,所有数据*2。

  这样想,分类讨论:

    1.x轴坐标相等,y轴坐标相等:直接输出此点坐标。

    2.只有一个轴坐标不等。

    3.两轴坐标都不等。

  设x1,x2为两点x坐标,x1>x2,第一次相遇时过了tx秒,交会在坐标xp,得到:

    xp=n-(x1+tx-n),xp=x2+tx

  可得

    tx=n-(x1+x2)/2

  同理 ty=m-(y1+y2)/2

  若x1==x2或y1==y2,直接输出求出的ty或tx处理出的坐标即可。

  否则是第三种情况:

    由于x轴相遇周期是n秒,y轴是m秒,所以实际时间是

      t=n-(x1+x2)/2+n*a,

      t=m-(y1+y2)/2+m*b,

    用Exgcd解出来,但是要保证a>=0,b>=0,并且a最小。

  首先:设ta=n-(x1+x2)/2 , tb=m-(y1+y2)/2 , 问题即变为求解n*a-m*b=(tb-ta), Exgcd形式是n*a+m*b=(n,m)

  设g=(n,m),如果(tb-ta)%g!=0说明无解,现在求出的a,b,可以演化出一堆解,形式如:a+k*(m/g),b-k*(n/g) 这里k为任意整数,这个方法对原方程成立。

  现在要求解满足n*a-m*b=(tb-ta)的非负最小解,可以直接a=a*(tb-ta)/g,a=a%(m/g),此后a为非负数,最小,且符合题意。

 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long LL;
LL ta,tb,x,y,tim;
int T,cas,n,m,x1,y1,x2,y2;
LL Exgcd(LL a,LL b,LL&x,LL&y){
if(b==){x=,y=;return a;}
LL ret=Exgcd(b,a%b,y,x);
y-=a/b*x;return ret;
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d%d%d%d%d%d",&n,&m,&x1,&y1,&x2,&y2);
n*=;m*=;x1*=;y1*=;x2*=;y2*=;
ta=n-(x1+x2)/;tb=m-(y1+y2)/;
printf("Case #%d:\n",++cas);tim=-;
if(x1==x2&&y1==y2)tim=;
if(x1!=x2&&y1==y2)tim=ta;
if(x1==x2&&y1!=y2)tim=tb;
if(x1!=x2&&y1!=y2){
LL d=Exgcd(n,m,x,y);
if((tb-ta)%d==){
x=(tb-ta)/d*x;
x=(x%(m/d)+m/d)%(m/d);
tim=ta+n*x;
}
}
if(tim==-)
puts("Collision will not happen.");
else{
x1=(x1+tim)%(*n);y1=(y1+tim)%(*m);
if(x1>n)x1=*n-x1;if(y1>m)y1=*m-y1;
printf("%.1f %.1f\n",x1/2.0,y1/2.0);
}
}
return ;
}

数学(扩展欧几里得算法):HDU 5114 Collision的更多相关文章

  1. ****ural 1141. RSA Attack(RSA加密,扩展欧几里得算法)

    1141. RSA Attack Time limit: 1.0 secondMemory limit: 64 MB The RSA problem is the following: given a ...

  2. 详解扩展欧几里得算法(扩展GCD)

    浅谈扩展欧几里得(扩展GCD)算法 本篇随笔讲解信息学奥林匹克竞赛中数论部分的扩展欧几里得算法.为了更好的阅读本篇随笔,读者最好拥有不低于初中二年级(这是经过慎重考虑所评定的等级)的数学素养.并且已经 ...

  3. 扩展欧几里得算法(extgcd)

    相信大家对欧几里得算法,即辗转相除法不陌生吧. 代码如下: int gcd(int a, int b){ return !b ? gcd(b, a % b) : a; } 而扩展欧几里得算法,顾名思义 ...

  4. noip知识点总结之--欧几里得算法和扩展欧几里得算法

    一.欧几里得算法 名字非常高大上的不一定难,比如欧几里得算法...其实就是求两个正整数a, b的最大公约数(即gcd),亦称辗转相除法 需要先知道一个定理: gcd(a, b) = gcd(b, a  ...

  5. 欧几里得算法与扩展欧几里得算法_C++

    先感谢参考文献:http://www.cnblogs.com/frog112111/archive/2012/08/19/2646012.html 注:以下讨论的数均为整数 一.欧几里得算法(重点是证 ...

  6. vijos1009:扩展欧几里得算法

    1009:数论 扩展欧几里得算法 其实自己对扩展欧几里得算法一直很不熟悉...应该是因为之前不太理解的缘故吧这次再次思考,回看了某位大神的推导以及某位大神的模板应该算是有所领悟了 首先根据题意:L1= ...

  7. 浅谈扩展欧几里得算法(exgcd)

    在讲解扩展欧几里得之前我们先回顾下辗转相除法: \(gcd(a,b)=gcd(b,a\%b)\)当a%b==0的时候b即为所求最大公约数 好了切入正题: 简单地来说exgcd函数求解的是\(ax+by ...

  8. (light oj 1306) Solutions to an Equation 扩展欧几里得算法

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1306 You have to find the number of solutions ...

  9. 『扩展欧几里得算法 Extended Euclid』

    Euclid算法(gcd) 在学习扩展欧几里得算法之前,当然要复习一下欧几里得算法啦. 众所周知,欧几里得算法又称gcd算法,辗转相除法,可以在\(O(log_2b)\)时间内求解\((a,b)\)( ...

  10. 题解——洛谷P2613 【模板】有理数取余(扩展欧几里得算法+逆元)

    题面 题目描述 给出一个有理数 c=\frac{a}{b}  ​ ,求  c mod19260817  的值. 输入输出格式 输入格式: 一共两行. 第一行,一个整数 \( a \) .第二行,一个整 ...

随机推荐

  1. owa Your request can't be completed right now. Please try again later.

    Your request can't be completed right now. Please try again later.

  2. 关于搭建Android环境的时候遇到 'could not find adb.exe!'的问题

    关于'could not find adb.exe'的问题 问题原因: 文件所处位置和Android_home变量指路径不一致 文件路径: 解决方法: 直接将相关文件退拽至变量值的路径下即可 小结:a ...

  3. 自己开发开源jquery插件--给jquery.treeview加上checkbox

    很多时候需要把树状的数据显示除来,比如分类,中国省份.城市信息,等,因此这方面的javascript插件也有很多.比如性能优异的jquery.treeview和国人开发的功能强大的zTree. 我最近 ...

  4. 使用Yeoman搭建 AngularJS 应用 (7) —— 让我们搭建一个网页应用

    原文地址:http://yeoman.io/codelab/preview-inbrowser.html 开启你的服务 运行Grunt任务,通过输入下面的命令来创建一个本地Node的http服务,地址 ...

  5. @Entity设置实体lazy = false

    问题描述 在通过Hibernate查询Bean信息时报以下异常信息: org.hibernate.LazyInitializationException: could not initialize p ...

  6. 【数学/扩展欧几里得/Lucas定理】BZOJ 1951 :[Sdoi 2010]古代猪文

    Description “在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色的大草坪,他们善良勇敢相互都关心……” ——选自猪王国民歌 很久很久以前,在山的那边 ...

  7. 用 OneAPM Cloud Insight 监控 Docker 性能

    Docker 是构建和部署软件的一个新兴的轻量级的平台,也是一个减轻替代虚拟机的容器.Docker 通过给开发者提供兼容不同环境的镜像,成为解决现代基础设施的持续交付的一个流行的解决方案. 和虚拟机一 ...

  8. 利用link标签rel="alternate stylesheet"属性实现界面动态换肤

    rel="stylesheet"属性指定将一个样式表立即应用到文档.rel="alternate stylesheet"属性将其作为备用样式表而在默认情况下禁用 ...

  9. ASC #1

    开始套题训练,第一套ASC题目,记住不放过每一题,多独立思考. Problem A ZOJ 2313 Chinese Girls' Amusement 循环节 题意:给定n,为圆环长度,求k < ...

  10. mybatis知识点

    1.Mybatis比IBatis比较大的几个改进是什么 a.有接口绑定,包括注解绑定sql和xml绑定Sql , b.动态sql由原来的节点配置变成OGNL表达式, c. 在一对一,一对多的时候引进了 ...