HDU5114(扩展欧几里得定理)

数学(扩展欧几里得算法):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

题解:分析把速度分解成x轴和y轴两个方向一共有四种情况:

1:    x1=x2 y1=y2 此时tim=0;

2:     x相等y不相等   此时tim=x-(x1+x2)/2;

3:    y相等x不相等   此时tim=y-(y1+y2)/2;

4:    x和y都相等     此时可得tx+ax=ty+by;

所以根据欧几里得定理可知ax+by=ty-tx;就可已将a和b算出来;


#include<cstring>
#include<cstdio>
#include<iostream>
#include<string>
#include<queue>
#include<vector>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<functional>
using namespace std;
#define eb(x) emplace_back(x)
#define pb(x) push_back(x)
#define ps(x) push(x)
#define clr(a,b) memset(a,b,sizeof(a))
#define _INIT ios::sync_with_stdio(false);cin.tie(nullptr);cout.precision(10); cout << fixed
#define lowbit(x) x&-x
#define rep(a,b,c) for(a=b;a<c;a++)
#define inf 0x3f3f3f3f3f3f
#define MAX_N 200000+5
#define MAX_M 25
typedef long long ll;
typedef unsigned long long ull;
const ll INF=1000000000000LL;
const ll maxn=1e7+2;
typedef priority_queue<int,vector<int>,less<int> > pql;
typedef priority_queue<int,vector<int>,greater<int> >pqg;
ll exgcd(ll a,ll b,ll &x,ll &y)//扩展欧几里得算法摸版
{
    if(b==0){x=1;y=0;return a;}
    ll d=exgcd(b,a%b,y,x);
    y-=a/b*x;
    return d;
}
int solve(ll x1,ll y1,ll x2,ll y2,ll x,ll y)
{
    ll tx=x-(x1+x2)/2,ty=y-(y1+y2)/2;
    ll tim=0;
    if(x1==x2&&y1==y2)tim=0;
    else if(x1==x2&&y1!=y2){tim=ty;}
    else if(x1!=x2&&y1==y2){tim=tx;}
    else if(x1!=x2&&y1!=y2)
    {
        ll xx,yy;
        ll d=exgcd(x,y,xx,yy);
        if((ty-tx)%d!=0){return -1;}
        xx=(ty-tx)/d*xx;
        xx=(xx%(y/d)+y/d)%(y/d);
        tim+=tx+xx*x;
    }
    x1=(x1+tim)%(2*x);y1=(y1+tim)%(2*y);
    if(x1>x)x1=2*x-x1;if(y1>y)y1=2*y-y1;
    printf("%.1f %.1f\n",x1/2.0,y1/2.0);
    return 1;
}
int main()
{
#ifndef ONLINE_JUDGE
    // freopen("data.txt","r",stdin);
#endif
    //_INIT;
    int t,ti;
    scanf("%d",&t);
    rep(ti,1,t+1)
    {
        ll x,y,x1,y1,x2,y2;
        scanf("%lld%lld",&x,&y);
        scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2);
        printf("Case #%d:\n",ti);
        if(solve(2*x1,2*y1,2*x2,2*y2,2*x,2*y)<0){printf("Collision will not happen.\n");}
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值