数学(扩展欧几里得算法):HDU 5114 Collision

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为非负数,最小,且符合题意。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using namespace std;
 5 typedef long long LL;
 6 LL ta,tb,x,y,tim;
 7 int T,cas,n,m,x1,y1,x2,y2;
 8 LL Exgcd(LL a,LL b,LL&x,LL&y){
 9     if(b==0){x=1,y=0;return a;}
10     LL ret=Exgcd(b,a%b,y,x);
11     y-=a/b*x;return ret;
12 }
13 int main(){
14     scanf("%d",&T);
15     while(T--){
16         scanf("%d%d%d%d%d%d",&n,&m,&x1,&y1,&x2,&y2);
17         n*=2;m*=2;x1*=2;y1*=2;x2*=2;y2*=2;
18         ta=n-(x1+x2)/2;tb=m-(y1+y2)/2;
19         printf("Case #%d:\n",++cas);tim=-1;
20         if(x1==x2&&y1==y2)tim=0;
21         if(x1!=x2&&y1==y2)tim=ta;
22         if(x1==x2&&y1!=y2)tim=tb;
23         if(x1!=x2&&y1!=y2){
24             LL d=Exgcd(n,m,x,y);
25             if((tb-ta)%d==0){
26                 x=(tb-ta)/d*x;
27                 x=(x%(m/d)+m/d)%(m/d);
28                 tim=ta+n*x;
29             }
30         }
31         if(tim==-1)
32             puts("Collision will not happen.");
33         else{
34             x1=(x1+tim)%(2*n);y1=(y1+tim)%(2*m);
35             if(x1>n)x1=2*n-x1;if(y1>m)y1=2*m-y1;
36             printf("%.1f %.1f\n",x1/2.0,y1/2.0);
37         }    
38     }
39     return 0;
40 }

 

posted @ 2016-10-09 19:06  TenderRun  阅读(1045)  评论(2编辑  收藏  举报