Advertisement
999ms

Untitled

Jan 31st, 2020
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define all(x) (x).begin(),(x).end()
  3.  
  4. using namespace std;
  5. using ll = long long;
  6.  
  7. struct pt {
  8.     int x;
  9.     int y;
  10.     int ind;
  11.     pt(int cx = 0, int cy = 0, int cind = 0) {
  12.         this->x = cx;
  13.         this->y = cy;
  14.         this->ind = cind;  
  15.     }
  16.     bool operator < (const pt& o) {
  17.         return x < o.x || (x == o.x && y < o.y);
  18.     }
  19.     pt operator - (const pt& o) const {
  20.         return pt(x - o.x, y - o.y);
  21.     }
  22. };
  23.  
  24. int main() {
  25.     ios_base::sync_with_stdio(false);
  26.     cin.tie(nullptr);
  27.     cout.tie(nullptr);
  28.     int ad, ac, bd, bc;
  29.     cin >> ad >> ac >> bd >> bc;
  30.     if (ad == bd && ac == bc) {
  31.         cout << "Impossible." << endl;
  32.         return 0;
  33.     }
  34.     auto get = [&] (double cosangle, int a, int b) {
  35.         return sqrt(a * a + b * b - 2 * cosangle * a * b);
  36.     };
  37.     double k = ad * 1.0 / bc;
  38.     double L1 = bd;
  39.     double L2 = ac;
  40.     /*
  41.      *  (L1 - x) / y == (L2 - y) / x == AD / BC
  42.      *  L1 - x = ky
  43.      *  L2 - y = kx
  44.      *  x = L1 - ky
  45.      *  L2 - y = k (L1 - ky)
  46.      *  L2 - y = kL1 - k**2 * y
  47.      *  (k**2 - 1) y = kL1 - L2
  48.      *  y = (kL1 - L2) / (k**2 - 1)
  49.      *  x = L1 - ky
  50.      */
  51.     if (abs(k * k - 1) < 1e-10) {
  52.         cout << "Impossible.\n";
  53.         return 0;
  54.     }
  55.     double y = (k * L1 - L2) / (k * k - 1);
  56.     double x = L1 - k * y;
  57.    
  58.     double cosA = (x * x + bc * bc - y * y) / (2 * bc * x);
  59.        
  60.     cout << fixed << setprecision(0) << "Distance is " << get(cosA, ac, ad) * 1000  << " km." << endl; 
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement