結果

問題 No.1931 Fraction 2
ユーザー limbo
提出日時 2025-05-22 23:13:21
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 955 bytes
コンパイル時間 2,232 ms
コンパイル使用メモリ 194,616 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-05-22 23:13:29
合計ジャッジ時間 7,428 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 3
other WA * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MOD = 998244353;

// GCD function
int gcd(int a, int b) {
    while (b) tie(a, b) = make_pair(b, a % b);
    return a;
}

// Modular function to keep result in [0, MOD)
int mod(int x) {
    x %= MOD;
    if (x < 0) x += MOD;
    return x;
}

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;

    int num = 0, den = 1;  // Start with 0/1

    for (int i = 0; i < n; ++i) {
        int x, y;
        cin >> x >> y;

        // num/den + x/y = (num * y + x * den) / (den * y)
        int new_num = num * y + x * den;
        int new_den = den * y;

        int g = gcd(abs(new_num), new_den);
        num = new_num / g;
        den = new_den / g;
    }

    // Reduce once more just to be sure
    int g = gcd(abs(num), den);
    num /= g;
    den /= g;

    cout << mod(num) << '\n';
    cout << mod(den) << '\n';
    return 0;
}
0