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. 大学生IT博客大赛最技术50强与最生活10强文章

    姓名 学校 文章标题 文章地址 刘成伟 井冈山大学 [mystery]-linux黑客之网络嗅探底层原理 http://infohacker.blog.51cto.com/6751239/115511 ...

  2. bnuoj 29373 Key Logger(模拟双向队列)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=29373 [题意]:模拟光标输入 [题解]:用双向列表模拟实现,这里用其他模拟会超时,注意内存的释放 ...

  3. Java-使用js进行编码,后台解码。

    1:使用js编码 var value=window.encodeURI(window.encodeURI(strValue)); 2:Java类中解码. String str=URLDecoder.d ...

  4. PD 脚本中列名注释用Name属性

    操作步骤:Database=>Generate Datatabase=>Format选项卡=>勾选 Generate name in empty comment项

  5. spring的配置模式与注解模式基础

    “依赖注入”是spring的核心特征,在Web服务器(如Tomcat)加载时,它会根据Spring的配置文件中配置的bean或者是通过注解模式而扫描并装载的bean实例自动注入到Application ...

  6. java模拟OSUnMapTbl[]

    问题描述: 任务就绪表,记录当前就绪的任务,就绪表中把64个优先级的任务分成8组,优先级的1-3bit表示OSRdyTbl[]中组别OSRedyGrp,优先级的4-6bit表示每组中就绪任务的位置,当 ...

  7. 转Spring+Hibernate+EHcache配置(二)

    Spring AOP+EHCache简单缓存系统解决方案 需要使用Spring来实现一个Cache简单的解决方案,具体需求如下:使用任意一个现有开源Cache Framework,要求可以Cache系 ...

  8. [Akka]发送一条消息的内部流程

    本想通过了解一下Akka-actor工程中主要的类的概念,来看下Akka内部运作的机制.无奈里边的类的确太多,注释中对每个类的功能也没有足够的解释.所以还是通过debug的方式,找个入手点,看一下互相 ...

  9. C++开发必看 四种强制类型转换的总结

    C风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是: TYPE b = (TYPE)a C++风格的类型转换提供了4种类型转换操作符来应对不同场合的应用. const_cast ...

  10. php获取类的实例变量

    <?php class Page {private $title; //构造函数固定名称为__construct,这样能将php的类构造函数独立于类名,以后修改类名就无需修改构造函数名称 fun ...