Collision
Time Limit: 15000/15000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 1623 Accepted Submission(s): 435
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.
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)
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.
对我来说这不是水题!!!
为了避免小数,所有数据*2(后面会体会到的)。
这样想,分类讨论:
1.x轴坐标相等,y轴坐标相等:直接输出此点坐标。
2.只有一个轴坐标不等。
3.两轴坐标都不等。
设x1,x2为两点x坐标,x1>x2,第一次相遇时过了tx秒,交会在坐标xp,得到:
xp = n - (x1 + ta - n), xp = x2 + ta
可得
ta = n - (x1 + x2) / 2
同理 tb = m - (y1 + y2) / 2
若x1==x2或y1==y2,直接输出求出的tb或ta处理出的坐标即可。
否则是第三种情况:
由于x轴相遇周期是n秒,y轴是m秒,所以实际时间是
time = n - (x1 + x2) / 2 + n * a,
time = 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), nb-k*(n/g) 这里k为任意整数,这个方法对原方程成立。
现在要求解满足n*a-m*b=(tb-ta)的非负最小解,可以直接a=a*(tb-ta)/g,a=a%(m/g),此后a为非负数,最小,且符合题意。
#include <iostream>
#include <cstdio>
using namespace std;
long long gcd(long long a, long long b){
return b == 0 ? a : gcd(b, a % b);
}
void ex_gcd(long long a, long long b, long long &u, long long &v){
if(b == 0){
u = 1, v = 0;
return ;
}
ex_gcd(b, a%b, v, u);
v = v - a/b *u;
return ;
}
int main(){
long long n, m, x1, y1, x2, y2;
long long t, ca = 0;
cin >> t;
while(t--){
ca++;
scanf("%lld%lld%lld%lld%lld%lld", &n, &m, &x1, &y1, &x2, &y2);
printf("Case #%lld:\n", ca);
n *= 2; //都乘以2避免后面ta,tb出现小数
m *= 2;
x1 *= 2;
y1 *= 2;
x2 *= 2;
y2 *= 2;
long long time = 0;
long long ta = n - (x1 + x2)/2;
long long tb = m - (y1 + y2)/2;
if(x1 == x2 && y1 == y2){
time = 0;
}
else if(x1 == x2 && y1 != y2){
time = tb;
}
else if(y1 == y2 && x1 != x2){
time = ta;
}
else{
long long g = gcd(n, m);
if((ta - tb) % g != 0){
time = -1; //不会相撞
}
else{
long long a, b;
// n /= g;
// m /= g;
// (ta - tb) /= g;
ex_gcd(n / g, m / g, a, b);
a = a * (tb - ta) / g;
a = (a % (m / g) + (m / g)) % (m / g); //必须对(m/g)而不是对m,???
time = ta + n * a; //x和y运动时间是一样的
}
}
if(time == -1){
printf("Collision will not happen.\n");
}
else{
x1 = (x1 + time) % (2 * n); //利用周期化为合理范围
y1 = (y1 + time) % (2 * m);
if(x1 > n) x1 = 2 * n - x1; //保证在0~n范围内,可以画图示意一下
if(y1 > m) y1 = 2 * m - y1;
printf("%.1lf %.1lf\n", x1 / 2.0, y1 / 2.0);
}
}
return 0;
}