Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include <functional>
- #include<string>
- #include<algorithm>
- #include <tuple>
- #include<random>
- #include <iomanip>
- #include<set>
- #define pr pair<int,int>
- using namespace std;
- using ll = unsigned long long;
- pr cokr(pr t) {
- while (t.first % 2 == 0 && t.second % 2 == 0) {
- t.first /= 2;
- t.second /= 2;
- }
- return t;
- }
- pr vvod(string s) {
- int i = 0;
- string u = "", u1 = "";
- while (s[i] != '/') {
- u += s[i];
- ++i;
- }
- ++i;
- while (i < s.size()) {
- u1 += s[i];
- ++i;
- }
- return { atoi(u.c_str()),atoi(u1.c_str()) };
- }
- int main() {
- ios::sync_with_stdio(0);
- cin.tie(0);
- string s;
- int t, n, x; pr a, b, sum{ 0,0 };
- cin >> s;
- pr d = vvod(s);
- d = cokr(d);
- pr y = d;
- cin >> t;
- vector<vector<pr>>v(t);
- for (int i = 0; i < t; ++i) {
- d = y;
- cin >> n;
- sum = { 0, 1 };
- v[i].reserve(n);
- for (int j = 0; j < n; ++j) {
- cin >> s;
- a = vvod(s);
- if (a.second >= sum.second) {
- sum.first = sum.first * (a.second / sum.second) + a.first;
- sum.second = a.second;
- }
- else {
- a.first = a.first * (sum.second / a.second);
- sum.first += a.first;
- }
- }
- if (sum.second >= d.second) {
- d.first = d.first * (sum.second / d.second);
- d.second = sum.second;
- }
- else
- sum.first = sum.first * (d.second / sum.second);
- d.first -= sum.first;
- d = cokr(d);
- if (d.first == 0)continue;
- else {
- while (1) {
- if (d.first == 1) {
- v[i].push_back(d);
- break;
- }
- if (d.first == d.second) {
- v[i].push_back({ 1,1 });
- break;
- }
- v[i].push_back({ 1,d.second });
- d = cokr({ d.first - 1,d.second });
- }
- }
- }
- for (auto i : v) {
- cout << i.size() << ' ';
- if (i.size() == 0) {
- cout << '\n';
- continue;
- }
- for (int j = i.size()-1; j >=0;--j) {
- cout << i[j].first << '/' << i[j].second << ' ';
- }
- cout << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment