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. liunx命令之whereis、which、find的区别和联系

    liunx命令之whereis.which.find的区别和联系

  2. EXTJS4.2 控件之Grid 行点击事件

    listeners: { 'itemclick': function (view, record, item, index, e) { //Ext.MessageBox.alert("标题& ...

  3. html template

    https://wrapbootstrap.com/tag/single-page http://themeforest.net/ https://wrapbootstrap.com/themes h ...

  4. javascript高级编程笔记05(面向对象)

    面向对象设计 es中有两种属性:数据属性和访问器属性 数据属性: 数据属性包含一个数据值的位置,在这个位置可以读取和写入值,数据属性有4个描述其行为的特性 [[Configurable]]:表示能否通 ...

  5. mysql 连接丢失错误解决(转载)

    1.1 错误信息: Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet succes ...

  6. 关于sqlmap无法打开的问题解决办法

    sqlmap无法打开这个问题困扰了我几天,今天才从一个论坛的视频教程里看到这个办法,有可能对其他有些情况依旧没用,但是希望和我一样状况的人看到这篇文章,能收到一些启发 我之前百度了很久,google了 ...

  7. SVN 迁移

    前段时间公司的SVN服务器做升级,需要做SVN迁移,百度谷歌了解了大概,在测试环境试了一下,没什么问题,然后改在正式环境做,迁移成功.之前用的是1.6,我看了下官网有1.8,征得同意后就直接升级加迁移 ...

  8. 【数学】[BZOJ 3884] 上帝与集合的正确用法

    Description 根据一些书上的记载,上帝的一次失败的创世经历是这样的: 第一天, 上帝创造了一个世界的基本元素,称做“元”. 第二天, 上帝创造了一个新的元素,称作“α”.“α”被定义为“元” ...

  9. Window8 进不了PE如何设置BIOS

    如题,如今进入Win8时代,很多新出的机器都自带了WIN8.但是童鞋们想进PE进行操作的时候,发现进不了. 更改BIOS以下2处设置,即可使用第三方引导安装系统:Boot->Launch CSM ...

  10. 几款国产开源的Windows界面库

    上次介绍的几款图形界面库http://blog.okbase.net/vchelp/archive/23.html都是国外的开源项目,今天介绍的几款都是国人的开源项目,大部分是采用DirectUI设计 ...