Mephistopheles_

F

Mar 20th, 2021
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include <functional>
  4. #include<string>
  5. #include<algorithm>
  6. #include <tuple>
  7. #include<random>
  8. #include <iomanip>
  9. #include<set>
  10. #define pr pair<int,int>
  11. using namespace std;
  12. using ll = unsigned long   long;
  13. pr cokr(pr t) {
  14.     while (t.first % 2 == 0 && t.second % 2 == 0) {
  15.         t.first /= 2;
  16.         t.second /= 2;
  17.     }
  18.     return t;
  19. }
  20. pr vvod(string s) {
  21.     int i = 0;
  22.     string u = "", u1 = "";
  23.     while (s[i] != '/') {
  24.         u += s[i];
  25.         ++i;
  26.     }
  27.     ++i;
  28.     while (i < s.size()) {
  29.         u1 += s[i];
  30.         ++i;
  31.     }
  32.     return { atoi(u.c_str()),atoi(u1.c_str()) };
  33. }
  34.  
  35. int main() {
  36.     ios::sync_with_stdio(0);
  37.     cin.tie(0);
  38.     string s;
  39.     int  t, n, x; pr a, b, sum{ 0,0 };
  40.     cin >> s;
  41.     pr d = vvod(s);
  42.     d = cokr(d);
  43.     pr y = d;
  44.     cin >> t;
  45.     vector<vector<pr>>v(t);
  46.     for (int i = 0; i < t; ++i) {
  47.         d = y;
  48.         cin >> n;
  49.         sum = { 0, 1 };
  50.         v[i].reserve(n);
  51.         for (int j = 0; j < n; ++j) {
  52.             cin >> s;
  53.             a = vvod(s);
  54.             if (a.second >= sum.second) {
  55.                 sum.first = sum.first * (a.second / sum.second) + a.first;
  56.                 sum.second = a.second;
  57.             }
  58.             else {
  59.                 a.first = a.first * (sum.second / a.second);
  60.                 sum.first += a.first;
  61.             }
  62.         }
  63.         if (sum.second >= d.second) {
  64.             d.first = d.first * (sum.second / d.second);
  65.             d.second = sum.second;
  66.         }
  67.         else
  68.             sum.first = sum.first * (d.second / sum.second);
  69.         d.first -= sum.first;
  70.         d = cokr(d);
  71.         if (d.first == 0)continue;
  72.         else {
  73.             while (1) {
  74.                 if (d.first == 1) {
  75.                     v[i].push_back(d);
  76.                     break;
  77.                 }
  78.                 if (d.first == d.second) {
  79.                     v[i].push_back({ 1,1 });
  80.                     break;
  81.                 }
  82.                 v[i].push_back({ 1,d.second });
  83.                 d = cokr({ d.first - 1,d.second });
  84.             }
  85.         }
  86.     }
  87.     for (auto i : v) {
  88.         cout << i.size() << ' ';
  89.         if (i.size() == 0) {
  90.             cout << '\n';
  91.             continue;
  92.         }
  93.         for (int j = i.size()-1; j >=0;--j) {
  94.             cout << i[j].first << '/' << i[j].second << ' ';
  95.         }
  96.         cout << '\n';
  97.     }
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment