hdu5114Collision

Collision

Time Limit: 15000/15000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 26    Accepted Submission(s): 8


Problem Description
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
  
  
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
Hint
In first example, two balls move from (1, 1) and (9, 9) both with velocity (1, 1), the ball starts from (9, 9) will rebound at point (10, 10) then move with velocity (−1, −1). The two balls will meet each other at (6, 6).
 

这题其实就是一道数论题啊,对速度正交分解,分四种情况讨论,对第四种情况解1个方程k1x-k2y=(x0+x1-y0-y1)/2,注意右侧必须为整数且是gcd(x,y)的倍数,否则无解。

最后求一个k1>0的最小正整数解。用一下扩展欧几里得即可。

时间t=k1*x-(x0+x1)/2,有了t就可以算最后的坐标x了,注意模上2x,然后如果大于x,还要用2x减去这个数。同理可以算得y。

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#define ll long long
#define eps 1e-6
using namespace std;

ll myabs(ll x){
    return x>=0?x:-x;
}
double fabs(double x){
    return x>0?x:-x;
}
void extend_gcd(ll a,ll b,ll &x,ll &y,ll &q){
    if(b==0){x=1;y=0;q=a;}
    else{
        extend_gcd(b,a%b,x,y,q);
        ll tmp=x;x=y;y=tmp-a/b*y;
    }
}
int main()
{
    int t,cas=1;
    ll x,y,x0,y0,x1,y1;
    double tt,ansx,ansy;
    cin>>t;
    while(t--){
        cin>>x>>y>>x0>>y0>>x1>>y1;
        printf("Case #%d:\n",cas++);
        if(x0==x1&&y0==y1) printf("%.1f %.1f\n",1.0*x0,1.0*y0);
        else if(x0==x1){
            tt=y-1.0*(y0+y1)/2;
            ansy=y0+tt>y?2*y-y0-tt:y0+tt;
            ansx=x0+tt>x?2*x-x0-tt:x0+tt;
            printf("%.1f %.1f\n",ansx,ansy);
        }
        else if(y0==y1){
            tt=x-1.0*(x0+x1)/2;
            ansy=x0+tt>x?2*x-x0-tt:x0+tt;
            ansx=y0+tt>y?2*y-y0-tt:y0+tt;
            printf("%.1f %.1f\n",ansx,ansy);
        }
        else{
            if((x0+x1-y0-y1)%2) {puts("Collision will not happen.");continue;}
            ll k=(x0+x1-y0-y1)/2;
            ll a,b,xt,yt,q;
            a=x,b=-y;
            extend_gcd(a,b,xt,yt,q);
            if(k%q) {puts("Collision will not happen.");continue;}
            else{
                xt*=k/q;
                ll z=xt/(y/q);
                xt-=y/q*z;
                if(xt<1) xt+=myabs(y/q);
                tt=xt*x-(x0+x1)*1.0/2;
                ll d=floor((x0+tt)/(2*x)+eps);
                double tx,ty;
                tx=x0+tt-2*x*d;
                d=floor((y0+tt)/(2*y)+eps);
                ty=y0+tt-2*y*d;
                ansx=tx>x?2*x-tx:tx;
                ansy=ty>y?2*y-ty:ty;
                printf("%.1f %.1f\n",ansx,ansy);
            }
        }
    }
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值