結果

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

ソースコード

diff #

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

// GCD
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;

    __int128 num = 0, den = 1;

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

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

        // Reduce the fraction at each step
        int g = gcd((int64_t)new_num, (int64_t)new_den);
        num = new_num / g;
        den = new_den / g;
    }

    // Final reduced num/den, print modulo 998244353
    cout << mod((int64_t)num) << '\n';
    cout << mod((int64_t)den) << '\n';
    return 0;
}
0