結果

問題 No.187 中華風 (Hard)
コンテスト
ユーザー maine_honzuki
提出日時 2020-11-19 00:26:50
言語 C++17(gcc12)
(gcc 12.4.0 + boost 1.89.0)
コンパイル:
g++-12 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
TLE  
実行時間 -
コード長 948 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,415 ms
コンパイル使用メモリ 398,800 KB
実行使用メモリ 15,656 KB
最終ジャッジ日時 2026-06-15 05:30:41
合計ジャッジ時間 9,244 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 2 TLE * 1 -- * 22
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
#define Cint boost::multiprecision::cpp_int
using namespace std;
const int mod = 1e9 + 7;

//ax + by = gcd(a,b)
Cint extgcd(Cint a, Cint b, Cint& x, Cint& y) {
    if (b) {
        Cint d = extgcd(b, a % b, y, x);
        y -= a / b * x;
        return d;
    } else {
        x = 1;
        y = 0;
        return a;
    }
}

int main() {
    int N;
    cin >> N;
    vector<Cint> X(N), Y(N);
    for (int i = 0; i < N; i++) {
        cin >> X[i] >> Y[i];
    }
    Cint ans = X[0];
    if (ans == 0)
        ans = Y[0];
    Cint y = Y[0];
    for (int i = 1; i < N; i++) {
        Cint a, b;
        extgcd(y, Y[i], a, b);
        if (abs(ans - X[i]) % gcd(y, Y[i]) != 0) {
            cout << -1 << endl;
            return 0;
        }
        a *= (X[i] - ans) / gcd(y, Y[i]);
        ans += a * y;
        y = lcm(y, Y[i]);
    }

    ans %= mod;
    cout << ans << endl;
}
0