zoukankan      html  css  js  c++  java
  • 数学(扩展欧几里得算法):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:
    ",++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
    ",x1/2.0,y1/2.0);
    37         }    
    38     }
    39     return 0;
    40 }
  • 相关阅读:
    深入理解Linux修改hostname
    逆水行舟,不进则退
    TNS-12541: TNS:no listener TNS-12560 TNS-00511: No listener
    Linux Tomcat 6.0安装配置实践总结
    Database 'xxx' cannot be upgraded because it is read-only or has read-only file Make the database or files writeable, and rerun recovery.
    Tomcat启动找不到JRE_HOME的解决方法
    ORACLE触发器判断是否更新了某个字段
    MS SQL错误:SQL Server failed with error code 0xc0000000 to spawn a thread to process a new login or connection. Check the SQL Server error log and the Windows event logs for information about possible related problems
    MS SQL 错误:The operation could not be performed because OLE DB provider "SQLNCLI10" for linked server "test" was unable to begin a distributed transaction.
    ORA-01078: failure in processing system parameters & LRM-00109: could not open parameter file
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5943453.html
Copyright © 2011-2022 走看看