結果

問題 No.186 中華風 (Easy)
ユーザー 1 Jupytor1 Jupytor
提出日時 2021-09-09 23:50:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,482 bytes
コンパイル時間 1,007 ms
コンパイル使用メモリ 112,848 KB
実行使用メモリ 19,184 KB
最終ジャッジ日時 2023-08-30 05:29:51
合計ジャッジ時間 2,631 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
16,684 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 25 ms
17,408 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 24 ms
17,268 KB
testcase_20 AC 18 ms
10,680 KB
testcase_21 AC 34 ms
19,184 KB
testcase_22 AC 33 ms
18,612 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
//#include <atcoder/>
#define flush fflush(stdout)
#define endl '\n'
#define all(v) v.begin(), v.end()
using namespace std;
//using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pl;
const int mod1 = (int)1e9 + 7, mod2 = (int)998244353;
const int INF = (int)1e9 + 10;
const ll LINF = (ll)1e18 + 10;
const int di[8] = {1, 0, -1, 0, 1, 1, -1, -1}, dj[8] = {0, 1, 0, -1, -1, 1, -1, 1};

#define rep0(i, n) for (i = 0; i < n; i++)
#define rep1(i, a, b) for (i = a; i < b; i++)
#define reprev(i, n) for (i = n - 1; i >= 0; i--)

template <typename T>
T my_abs(T x){
    return (x >= 0) ? x : -x;
}

template <typename T>
bool chmax(T &a, T b){
    if (a < b){
        a = b;
        return true;
    }
    return false;
}

template <typename T>
bool chmin(T &a, T b){
    if (a > b){
        a = b;
        return true;
    }
    return false;
}

ll safe_mod(ll a, ll m){
    return (a % m + m) % m;
}

ll extgcd(ll a, ll b, ll &p, ll &q){
    if (b == 0){
        p = 1;
        q = 0;
        return a;
    }
    ll d;
    d = extgcd(b, a % b, q, p);
    q -= a / b * p;
    return d;
}

bool incld_bit(ll bit, int i){
    return ((bit >> i) & 1) == 1;
}

// --------------------------------------------------------------------------------

Pl crt(int n, vector<ll> &r, vector<ll> &m){
    int i;
    Pl ans;
    ll b1, m1, b2, m2;
    ll d, p, q;
    ans = Pl(0, 1);
    rep0(i, n){
        b1 = ans.first;
        m1 = ans.second;
        b2 = r[i];
        m2 = m[i];
        d = extgcd(m1, m2, p, q);
        if (b1 % d != b2 % d){
            return Pl(0, 0);
        }
        ans.first = b1 + ((b2 - b1) / d * p) % (m2 / d) * m1;
        ans.second = m1 / d * m2;
        ans.first = safe_mod(ans.first, ans.second);
    }
    return ans;
}

int main(void){
    int i, j;
    int n;
    ll rin, min;
    vector<ll> r, m;

    cin >> n;
    rep0(i, n){
        cin >> rin >> min;
        r.push_back(rin);
        m.push_back(min);
    }

    Pl ans;
    ans = crt(n, r, m);

    if (ans.second == 0){
        cout << -1 << endl;
    }else{
        if (ans.first == 0){
            cout << ans.second << endl;
        }else{
            cout << ans.first << endl;
        }
    }

    return 0;
}
0