Submission

Status:

PPPPPPPPPP

Subtask/Task Score:

100/100

Score: 100

User: tha_smith

Problemset: Fraction

Language: cpp

Time: 0.004 second

Submitted On: 2025-10-09 22:35:51

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int n; long long a,b;
    cin >> a >> b >> n;
    for(int i=0; i<n; i++) {
        char c; long long p,q,d;
        cin >> c >> p >> q;
        if(c=='+') {
            a = a*q + b*p;
            b *= q;
        }
        else if(c=='-') {
            a = a*q - b*p;
            b *= q;
        }
        else if(c=='*') {
            a*=p;
            b*=q;
        }
        else if(c=='/') {
            a*=q;
            b*=p;
        }
        //cout << a << ' ' << b << endl;
        if(a==0) {
            a=0;
            b=1;
        }
        else {
            d = __gcd(a,b);
            a/=d;
            b/=d;
            //cout << d << ' ' << a << ' ' << b << endl;
            if(b<0) { //if d returns negative integer -> return to previous + or -
            	a*=-1;
            	b*=-1;
			}
        }
        cout << a << ' ' << b << endl;
    }
}