ZOJ 3728 Collision
There‘s a round medal fixed on an ideal smooth table, Fancy is trying to throw some coins and make them slip towards the medal to collide. There‘s also a round range which shares exact the same center as the round medal, and radius of the medal is strictly less than radius of the round range. Since that the round medal is fixed and the coin is a piece of solid metal, we can assume that energy of the coin will not lose, the coin will collide and then moving as reflect.
Now assume that the center of the round medal and the round range is origin ( Namely (0, 0) ) and the coin‘s initial position is strictly outside the round range. Given radius of the medal Rm, radius of coin r, radius of the round range R, initial position (x, y) and initial speed vector (vx, vy) of the coin, please calculate the total time that any part of the coin is inside the round range.
Please note that the coin might not even touch the medal or slip through the round range.
Input
There will be several test cases. Each test case contains 7 integers Rm, R, r, x, y, vx and vy in one line. Here 1 ≤ Rm < R ≤ 2000, 1 ≤ r ≤ 1000, R + r < |(x, y)| ≤ 20000, 1 ≤ |(vx, vy)| ≤ 100.
Output
For each test case, please calculate the total time that any part of the coin is inside the round range. Please output the time in one line, an absolute error not more than 1e-3 is acceptable.
Sample Input
5 20 1 0 100 0 -1
5 20 1 30 15 -1 0
Sample Output
30.000
29.394
Author: FAN, Yuzhe
Contest: The 2013 ACM-ICPC Asia Changsha Regional Contest
題目鏈接 :http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3728
題目大意 :有一個圓硬幣半徑為r,初始位置為x,y。速度矢量為vx。vy,有一個圓形區域(圓心在原點)半徑為R。另一個圓盤(圓心在原點)半徑為Rm (Rm < R),圓盤固定不動,硬幣撞到圓盤上會被反彈,不考慮能量損失。求硬幣在圓形區域內運動的時間。
題目分析 :設硬幣的運動方程為x‘ = x + vxt。y‘ = y + vyt,分別帶入以(r + R) 和 (r + Rm)為半徑的圓方程,與大圓聯立無解或者有負解則為0,與大圓聯立有無負解與小圓聯立無解則直接解為(t2- t1)可用韋達定理處理。|t2 - t1| = sqrt(derta) / a,若與小圓聯立有負解則為0,否則解為2*(t‘),t‘為先撞到圓盤時的t
#include <cstdio> #include <cmath> int main() { double Rm, R, r, x, y, vx, vy; while(scanf("%lf %lf %lf %lf %lf %lf %lf", &Rm, &R, &r, &x, &y, &vx, &vy) != EOF) { double a = vx * vx + vy * vy; double b = 2 * (x * vx + y * vy); double c1 = x * x + y * y - (R + r) * (R + r); double c2 = x * x + y * y - (Rm + r) * (Rm + r); double derta1 = b * b - 4 * a * c1; double derta2 = b * b - 4 * a * c2; double t1 = (-b + sqrt(derta2)) / (2 * a); double t2 = (-b - sqrt(derta2)) / (2 * a); double t3 = (-b + sqrt(derta1)) / (2 * a); double t4 = (-b - sqrt(derta1)) / (2 * a); if(derta1 <= 0) printf("0.000\n"); else if(derta1 > 0 && derta2 <= 0) { if(t3 < 0 || t4 < 0) printf("0.000\n"); else printf("%.3f\n", sqrt(derta1) / a); } else if(derta2 > 0) { double t11 = fabs(t1 - x) > fabs(t2 - x) ? t2 : t1; double t22 = fabs(t3 - x) > fabs(t4 - x) ? t4 : t3; if(t11 < 0 || t22 < 0) printf("0.000\n"); else printf("%.3f\n", 2 * fabs(t22 - t11)); } } }
ZOJ 3728 Collision