hdu5114 Collision (扩展欧几里德+思维)

来源:互联网 发布:轩墨宝宝 知乎 编辑:程序博客网 时间:2024/05/22 10:38

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 ≤ 10 5

The next line contains four integers x 1, y 1, x 2, y 2. The initial position of the two balls is (x 1, y 1) and (x 2, y 2). (0 ≤ x 1, x 2 ≤ x; 0 ≤ y 1, y 2 ≤ 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 x c and y c, rounded to one decimal place, which indicate the position where the two balls will first collide.
Sample Input
310 101 1 9 910 100 5 5 1010 101 0 1 10
Sample Output
Case #1:6.0 6.0Case #2:Collision will not happen.Case #3:6.0 5.0




#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <algorithm>#include <math.h>using namespace std;typedef long long LL;LL gcd(LL a,LL b){     return b?gcd(b,a%b):a;}void Ex_gcd(LL a, LL b, LL &x, LL &y){    if(b == 0)//递归出口    {        x = 1;        y = 0;        return;    }    LL x1, y1;    Ex_gcd(b, a%b, x1, y1);    x = y1;    y = x1-(a/b)*y1;}int main(){    LL  x,y,x1,x2,y1,y2;    int t;    scanf("%d",&t);    for(int cas=1;cas<=t;cas++)    {        scanf("%lld%lld",&x,&y);        scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2);        double ansx,ansy;        printf("Case #%d:\n",cas);        if(x1==x2&&y1==y2)        {            ansx=x1;ansy=y1;        }        else if(x1==x2&&y1!=y2)        {            if(y1>y2)swap(y1,y2);            double t=2*y-y1-y2;            ansx=t/2+x1;            ansy=t/2+y1;        }        else if(x1!=x2&&y1==y2)        {            if(x1>x2)swap(x1,x2);            double t=2*x-x1-x2;            ansx=t/2+x1;            ansy=t/2+y1;        }        else        {            x*=2;y*=2;x1*=2;x2*=2;y1*=2;y2*=2;///用扩欧要保证整数,所有数据乘2            LL z=gcd(x,y);            LL quanx,quany;            LL c=(2*y-y1-y2-2*x+x1+x2)/2;            if(c%z!=0){printf("Collision will not happen.\n");continue;}            else            {                LL xx=x/z,yy=y/z;                c/=z;                Ex_gcd(xx,yy,quanx,quany);///用扩欧要保证整数,所有数据乘2                quanx*=c;                quanx=(quanx%yy+yy)%yy;///最小正整数解                LL shijian=quanx*x+(2*x-x1-x2)/2;///两周期重合时间+从(X1,Y1)到重合点时间               ansx=(x1+shijian)%(2*x);///一去一回2x算一个周期               ansy=(y1+shijian)%(2*y);               //printf("  time= %d  %.1lf   %.1lf\n",shijian,ansx,ansy);                 if(ansx > x) ansx = 2*x - ansx;///超过边界对称回来                if(ansy > y) ansy= 2*y - ansy;                ansx/=2;ansy/=2;            }        }        printf("%.1lf %.1lf\n",ansx,ansy);    }    return 0;}




原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 二宝美籍大宝怎么办 小孩一洗澡就哭怎么办 孩子去外地上学学籍怎么办 非婚生子父亲想要孩子怎么办 非婚生子孩子父亲找不到了怎么办 3岁半宝宝认字怎么办 上课注意力不集中老是发呆怎么办 海绵宝宝吃了会怎么办 宝宝误吞李子核怎么办 24个月宝宝不愿意说话怎么办 孩子凉着肚子吐怎么办 教宝宝说话不会说怎么办 2岁宝宝语言退化怎么办 两周宝宝嗓子哑怎么办 幼儿园孩子上课爱说话老师怎么办 一岁宝宝不会爬怎么办 孩子说话不太清楚怎么办 十一个月宝宝不爱吃饭怎么办 14个月宝宝不爱吃饭怎么办 一岁宝宝喜欢哭怎么办 4岁儿童不会说话怎么办 8个月宝宝37.5度怎么办 8个月婴儿37.5度怎么办 5个月宝宝38度怎么办 5个月宝宝发烧怎么办 五个月小孩38度怎么办 宝宝3岁还不会说话 怎么办 两岁宝宝流口水厉害怎么办 两周宝宝不说话怎么办 两岁宝宝说话有点口吃怎么办 一周岁的宝宝脾气不好怎么办 九个月的宝宝脾气不好怎么办 两岁宝宝脾气不好怎么办 2岁宝宝脾气不好怎么办 3岁宝宝脾气不好怎么办 脾气不好吓到宝宝了怎么办 四个月宝宝脾气不好怎么办 5岁说话不清楚该怎么办 小孩快上幼儿园了不怎么说话怎么办 四岁宝宝吐字不清楚怎么办 孕28周胎儿腿短怎么办