Submission

Status:

PPPPPPPPPP

Subtask/Task Score:

100/100

Score: 100

User: solarsunny

Problemset: Fraction

Language: cpp

Time: 0.003 second

Submitted On: 2025-10-05 14:51:43

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false); 
    cin.tie(NULL);
    long long a;
    cin >> a;
    long long b;
    cin >> b;
    long long n;
    cin >> n;
    for(int i=0; i<n; i++) {
        char oper;
        long long c;
        long long d;
        cin >> oper >> c >> d;
        if(oper == '+' || oper == '-') {
            long long denom= lcm(b,d);
            if(oper == '+') {
                a = a*(denom/b) + c*(denom/d);
                b = denom;
            } else if (oper == '-') {
                a = a*(denom/b) - c*(denom/d);
                b = denom;
            }
        } else if(oper == '*' || oper == '/') {
            if(oper == '*') {
                a = a*c;
                b = b*d;
            } else if (oper == '/') {
                a = a*d;
                b = b*c;
            }
        }
        long long g = gcd(a,b);
        a = a/g;
        b = b/g;
        if(b<0) {
            a=-a;
            b=-b;
        }
        cout << a << " " << b << "\n";
    }
    
    return 0;
}