結果

問題 No.186 中華風 (Easy)
ユーザー m_tsubasam_tsubasa
提出日時 2019-09-17 17:55:22
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,203 bytes
コンパイル時間 1,424 ms
コンパイル使用メモリ 168,928 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-27 00:56:12
合計ジャッジ時間 2,458 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,504 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 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 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

// as + bt = GCD(a,b) a,b:const s,t:var(any)
// return GCD(a,b)
long long extGCD(long long a, long long b, long long& s,
                 long long& t) {
  s = 1, t = 0;
  while(b) {
    long long tmp = a / b;
    a -= b * tmp;
    s -= t * tmp;
    swap(a, b);
    swap(s, t);
  }
  return a;
}

// x≡b_i(mod m_i) calc min x,lcm(m_i). if not exist, return
// (-1,-1)
pair<long long, long long> ChineseRem(
    const vector<long long>& b,
    const vector<long long>& m) {
  long long r = 0, lcm = 1;
  assert(b.size() == m.size());
  long long bsize = b.size();
  for(int i = 0; i < bsize; ++i) {
    long long p, q, d, now;
    d = extGCD(lcm, m[i], p, q);
    if((b[i] - r) % d != 0) return make_pair(-1, -1);
    now = (b[i] - r) / d * p % (m[i] / d);
    r += lcm * now;
    lcm *= m[i] / d;
  }
  return make_pair((r + lcm) % lcm, lcm);
}

bool ch = 0;
vector<long long> x, y;

int main() {
  x.resize(3);
  y.resize(3);
  for(int i = 0; i < 3; ++i) {
    cin >> x[i] >> y[i];
    if(x[i]) ch = 1;
  }
  pair<long long, long long> ans = ChineseRem(x, y);
  if(!ch)
    cout << ans.second << endl;
  else
    cout << ans.first << endl;
  return 0;
}
0