AlexNeagu11

Journey to Jupiter

Feb 25th, 2023
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.81 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. // #define int long long
  3. #define f first
  4. #define s second
  5. using namespace std;
  6.  
  7. ifstream in("jupiter.in");
  8. #define cin in
  9.  
  10. typedef long long ll;
  11. const int MOD = 1e9 + 9;
  12. const int inf = 1e9;
  13. const double eps = 1e-9;
  14. const double PI = acos(-1);
  15.  
  16.  
  17. struct point3 {
  18.     double x, y, z;
  19.     // basic operations
  20.     point3 operator-(point3 p) {
  21.         return point3{x - p.x, y - p.y, z - p.z};
  22.     }
  23.     point3 operator/(double d) {
  24.         return {x / d, y / d, z / d};
  25.     }
  26.     double operator^(point3 p) {
  27.         return x * p.x + y * p.y + z * p.z;
  28.     }
  29.     double squareLen() {
  30.         return (*this) ^ (*this);
  31.     }
  32.     double len() {
  33.         return sqrt(squareLen());
  34.     }
  35.     point3 unitVec() {
  36.         // the same vector but of length 1 (unit)
  37.         return (*this) / len();
  38.     }
  39. };
  40.  
  41. int solveSystem(vector<vector<double>> &matrix, vector<double> &ans) {
  42.     int n = matrix.size(), m = matrix[0].size() - 1;
  43.     int row = 0;
  44.     for(int col = 0; col < m; ++col) {
  45.         int bestRow = row;
  46.         for(int i = row; i < n; ++i) {
  47.             if(abs(matrix[bestRow][col]) < abs(matrix[i][col])) {
  48.                 bestRow = i;
  49.             }
  50.         }
  51.         if(bestRow >= n || abs(matrix[bestRow][col]) < eps) {
  52.             continue;
  53.         }
  54.         for(int i = 0; i <= m; ++i) {
  55.             swap(matrix[bestRow][i], matrix[row][i]);
  56.         }
  57.        
  58.         // impart toata linia la matrix[row][col]
  59.         for(int i = col + 1; i <= m; ++i) {
  60.             matrix[row][i] = matrix[row][i] / matrix[row][col];
  61.         }
  62.  
  63.         matrix[row][col] = 1.0;
  64.         for(int i = row + 1; i < n; ++i) {
  65.             for(int j = col + 1; j <= m; ++j) {
  66.                 matrix[i][j] -= matrix[i][col] * matrix[row][j];
  67.             }
  68.             matrix[i][col] = 0.0;
  69.         }
  70.         ++row;
  71.     }
  72.  
  73.     bool infty = false;
  74.  
  75.     for(int i = n - 1; i >= 0; --i) {
  76.         bool found = false;
  77.         for(int j = 0; j <= m && !found; ++j) {
  78.             if(abs(matrix[i][j]) > eps) {
  79.                 if(j == m) {
  80.                     return 0;
  81.                 }
  82.                 ans[j] = matrix[i][m];
  83.                 for(int k = j + 1; k < m; ++k) {
  84.                     ans[j] -= ans[k] * matrix[i][k];
  85.                 }
  86.                 found = true;
  87.             }
  88.         }
  89.         if(!found) {
  90.             infty = true;
  91.         }
  92.     }
  93.     if(infty) {
  94.         return 2;
  95.     }
  96.     return 1;
  97. }
  98.  
  99. double nx, ny, nz, ax, ay, az, L, H;;
  100.  
  101. point3 solvePoint(point3 &A, point3 &normVec, double angle) {
  102.     double S = L * L / 3.0 * sin(angle);
  103.     double C = L * L / 3.0 * cos(angle);
  104.  
  105.     vector<vector<double>> systemMatrix(3, vector<double> (4, 0));
  106.    
  107.     systemMatrix[0][1] = -A.z, systemMatrix[0][2] = A.y, systemMatrix[0][3] = S * normVec.x;
  108.     systemMatrix[1][0] = A.z, systemMatrix[1][2] = -A.x, systemMatrix[1][3] = S * normVec.y;
  109.     systemMatrix[2][0] = A.x, systemMatrix[2][1] = A.y, systemMatrix[2][2] = A.z, systemMatrix[2][3] = C;
  110.    
  111.     vector<double> ans(3, 0);
  112.     int verdict = solveSystem(systemMatrix, ans);
  113.     assert(verdict == 1); // ecuatia are o singura solutie
  114.     return point3{ans[0], ans[1], ans[2]};
  115. }
  116.  
  117. void solve()
  118. {
  119.     cin >> nx >> ny >> nz >> ax >> ay >> az >> L >> H;
  120.    
  121.     point3 normVec = point3{nx, ny, nz}.unitVec();
  122.     point3 A = {ax, ay, az};
  123.  
  124.     point3 B = solvePoint(A, normVec, 2.0 * PI / 3.0);
  125.     point3 C = solvePoint(A, normVec, -2.0 * PI / 3.0);
  126.  
  127.     A = A - point3{0, 0, -H};
  128.     B = B - point3{0, 0, -H};
  129.     C = C - point3{0, 0, -H};
  130.    
  131.     cout << fixed << setprecision(10) << A.len() << ' ' << B.len() << ' ' << C.len() << '\n';
  132. }
  133.  
  134. int main() {
  135.     ios_base::sync_with_stdio(false);
  136.     cin.tie(0);
  137.     int T;
  138.     cin >> T;
  139.     while(T--) {
  140.         solve();
  141.     }
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment