Advertisement
_rashed

SPOJ BILLIARD

Feb 24th, 2022
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. #define ll long long
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. const int OO = 1e9;
  6. const double EPS = 1e-6;
  7.  
  8. bool comp(double x1, double x2) {
  9.     return (fabs(x1-x2) < EPS);
  10. }
  11.  
  12. int main()
  13. {
  14.     ios_base::sync_with_stdio(false);
  15.     cin.tie(NULL);
  16.     cout.tie(NULL);
  17.     cout.precision(2);
  18.     cout << fixed;
  19.     while(true) {
  20.         double a,b,s,m,n;
  21.         cin >> a >> b >> s >> m >> n;
  22.         if(a == 0)
  23.             break;
  24.         double theta_rad = atan((n*b)/(m*a));
  25.         double theta_deg = (theta_rad*180)/acos(-1);
  26.         cout << theta_deg << " ";
  27.         double time = 0;
  28.         double speed_x = cos(theta_rad);
  29.         double speed_y = sin(theta_rad);
  30.         double init_x = a/2;
  31.         double init_y = b/2;
  32.         double curr_x = a/2;
  33.         double curr_y = b/2;
  34.         //int times = 0;
  35.         //cout << "\n";
  36.  
  37.         //ms -> time
  38.         //1/x -> s
  39.  
  40.         //1/x = s*ms
  41.         int k_m = 0;
  42.         int k_n = 0;
  43.         while(!comp(curr_x,init_x) || !comp(curr_y,init_y) || comp(time,0) || k_m < m || k_n < n) {
  44.             //dist += 1;
  45.             //curr_x += speed_x;
  46.             //curr_y += speed_y;
  47.             double curr_t = min((speed_x > 0 ? a-curr_x:curr_x-0)/fabs(speed_x),(speed_y > 0 ? b-curr_y:curr_y-0)/fabs(speed_y));
  48.             double reach_x = (((curr_x < init_x && speed_x > 0)||(curr_x > init_x && speed_x < 0)) ? fabs(curr_x-init_x)/fabs(speed_x):-1);
  49.             double reach_y = (((curr_y < init_y && speed_y > 0)||(curr_y > init_y && speed_y < 0)) ? fabs(curr_y-init_y)/fabs(speed_y):-1);
  50.             bool reached_start = false;
  51.             if(reach_x != -1 && reach_y != -1 && comp(reach_x,reach_y) && reach_x < curr_t) {
  52.                 curr_t = reach_x;
  53.                 reached_start = true;
  54.             }
  55.             curr_x += speed_x*curr_t;
  56.             curr_y += speed_y*curr_t;
  57.             time += curr_t;
  58.             if(curr_x >= a || curr_x <= 0) {
  59.                 speed_x *= -1;
  60.                 k_m++;
  61.             }
  62.             if(curr_y >= b || curr_y <= 0) {
  63.                 speed_y *= -1;
  64.                 k_n++;
  65.             }
  66.             if(reached_start && k_m == m && k_n == n) {
  67.                 break;
  68.             }
  69.             //cout << "curr_x is " << curr_x << " curr_y is " << curr_y << "\n";
  70.             //cout << "speed_x is " << speed_x << " speed_y is " << speed_y << "\n";
  71.             //cout << "k_n is " << k_n << " k_m is " << k_m << "\n";
  72.             //times++;
  73.             //if(times == 20)
  74.                 //break;
  75.         }
  76.         //cout << "k_n is " << k_n << " k_m is " << k_m << "\n";
  77.         cout << time/s << "\n";
  78.     }
  79.  
  80.     return 0;
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement