結果

問題 No.186 中華風 (Easy)
ユーザー snbnsnbn
提出日時 2020-07-03 15:22:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,906 bytes
コンパイル時間 1,005 ms
コンパイル使用メモリ 108,068 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-27 01:00:22
合計ジャッジ時間 2,104 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int64_t i = 0; i < (int64_t)(n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (int64_t)(n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)

#define chmax(a, b) (a) = max(a, b)
#define chmin(a, b) (a) = min(a, b)

tuple<int64_t, int64_t, int64_t> xgcd(int64_t a, int64_t b) {
  if (b == 0) {
    return make_tuple(1, 0, a);
  }
  int64_t z, w, d;
  tie(z, w, d) = xgcd(b, a % b);
  return make_tuple(w, z - a / b * w, d);
}

tuple<int64_t, int64_t> chinese_rem(int64_t a1, int64_t a2, int64_t m1,
                                    int64_t m2) {
  int64_t x, y, d;
  tie(x, y, d) = xgcd(m1, m2);

  const int64_t c = a2 - a1;
  if (c % d != 0) {
    return make_tuple(0, 0);
  }
  const int64_t tmp = c / d * x % (m2 / d);
  const int64_t sln = a1 + tmp * m1;
  const int64_t M = m1 / d * m2;

  return make_tuple((sln % M + M) % M, M);
}

tuple<int64_t, int64_t> chinese_rem(const vector<int64_t>& a,
                                    const vector<int64_t>& MOD) {
  if (a.size() != MOD.size()) {
    stringstream msg;
    msg << "a.size() is " << a.size() << ", but MOD.size()" << MOD.size()
        << "\n";
    throw msg.str();
  }
  int64_t sln = 0, M = 1;
  for (int i = 0; i < a.size(); i++) {
    tie(sln, M) = chinese_rem(sln, a[i], M, MOD[i]);
    if (M == 0) {
      return make_tuple(0, 0);
    }
  }

  return make_tuple(sln, M);
}

int main() {
  vector<int64_t> x(3), y(3);
  rep(i, 3) { cin >> x[i] >> y[i]; }

  int64_t sln, M;
  tie(sln, M) = chinese_rem(x, y);
  cout << (M != 0 ? (sln + M - 1) % M + 1 : -1) << endl;

  return 0;
}
0