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. linux内核驱动中_IO, _IOR, _IOW, _IOWR 宏的用法与解析(引用)

    在驱动程序里, ioctl() 函数上传送的变量 cmd 是应用程序用于区别设备驱动程序请求处理内容的值.cmd除了可区别数字外,还包含有助于处理的几种相应信息. cmd的大小为 32位,共分 4 个 ...

  2. 后台启动mysql ,redis

    mysqld_safe --user=mysql & redis.conf daemonize no修改为daemonize yes

  3. EXTJS4.2 控件之Grid getRowClass 添加行背景色

    css 样式: /*交流管理系统 开始*/ tr.x-grid-record-blue .x-grid-td { background: #87CEFF; }/*grid 行颜色*/ tr.x-gri ...

  4. TOPAPI 消息通知机制

    接收用户订阅消息 public class UserSubMain { public static void main(String[] args ) throws ApiException { St ...

  5. shell复习笔记----命令与参数

    shell最基本的工作就是执行命令. 每键入一道命令, shell 就会执行. $cd work;ls -l whizprog.c 首先:格式很简单,以空白(Space 键或者 Tab键)隔开命令行中 ...

  6. c#无标题窗体点击任务栏图标正常最小化或还原

    FormBorderStyle等于System.Windows.Forms.FormBorderStyle.None的窗体,点击任务栏图标的时候,是不能象标准窗体那样最小化或还原的. protecte ...

  7. Google chrome的字体设置

    http://blog.sina.com.cn/s/blog_a3b863da01016sv3.html 谷歌浏览器(Google chrome)速度很快,很好用.问题是字体显示有时候不对:用英文版的 ...

  8. ubuntu12 开机自动转到命令行

    命令: sudo gedit /etc/default/grub 找到这一行 GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"改成 GRUB_CM ...

  9. WCF之各种WCF引用方式

    写在开头:本文内容来自 WCF全面解析中的一个经典例子,如果你已经看过了,那么可以忽略本文,本文旨在和大家分享不一样的WCF使用方法. 准备工作: 1.创建解决方案WCFService(当然名字可以任 ...

  10. 摄像头(2)调用系统拍照activity来录像

    import android.app.Activity; import android.content.Intent; import android.content.pm.PackageManager ...