結果

問題 No.186 中華風 (Easy)
ユーザー ineedyourlovepineedyourlovep
提出日時 2023-01-22 00:35:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,210 bytes
コンパイル時間 2,001 ms
コンパイル使用メモリ 202,620 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-06 11:38:26
合計ジャッジ時間 2,997 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
//const int mod = 1000000007;
//const int mod = 998244353;

ll extGCD(ll a, ll b, ll &x, ll &y){
  if(b == 0) { x = 1; y = 0; return a;}
  ll d = extGCD(b, a%b, y, x);
  y -= a/b*x;
  return d;
}

//
// let (r, m) be return value, the solution is x ≡ r (mod. m)
pair<long long, long long> CRT(const vector<long long>& b, const vector<long long>& m) {
  long long r = 0, M = 1;
  for (int i = 0; i < (int)b.size(); i++) {
    long long p, q;
    long long d = extGCD(M, m[i], p, q); // p is inv of M/d (mod. m[i]/d)
    if ((b[i] - r) % d != 0) return make_pair(0, -1);
    long long tmp = (b[i] - r)/d*p%(m[i]/d);
    r += M*tmp;
    M *= m[i]/d;
  }
  return make_pair((r%M+M)%M, M);
}

int main()
{
  int n = 3;
  vector<ll> b(n), m(n);
  bool flag = false;
  rep(i, 0, n) {
    cin >> b[i] >> m[i];
    if (b[i]) flag = true;
  }
  
  auto res = CRT(b, m);
  if (res.second == -1) cout << -1 << endl;
  else if (flag) cout << res.first << endl;
  else cout << res.second << endl;
  return 0;
}
0