Submission

Status:

PPPPPPPPPP

Subtask/Task Score:

100/100

Score: 100

User: Nuttykub

Problemset: Fraction

Language: cpp

Time: 0.003 second

Submitted On: 2026-10-04 12:49:36

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

ll gcd(ll x,ll y){
    if (x < 0) x = -x;
    if (y < 0) y = -y;
    while(y){
        ll t = x%y;
        x = y;
        y = t;
    }
    return x;
}


int main(){
    ll a,b;
    cin >> a >> b;
    ll o;
    cin >> o;


    while(o--){
        char op;
        ll c,d;
        cin >> op >> c >> d;
        if (op == '-'){
            a*=d;
            c*=b;
            b*=d;
            a-=c;
        }
        else if (op == '+') {
            a*=d;
            c*=b;
            b*=d;
            a+=c;
        }
        else if (op == '*') {
            a*=c;
            b*=d;
        }
        else if (op == '/'){
            a*=d;
            b*=c;
        }
        ll g = gcd(a,b);
        a/=g;
        b/=g;
        if(b < 0){
            a = -a;
            b=-b;
        }
        if (a != 0) cout << a << " " << b << endl;
        else cout << "0 1" << endl;
    }
    return 0;
}