結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,376 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;
    ll rin, min;
    vector<ll> r, m;

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

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

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

    return 0;
}
0