結果

問題 No.186 中華風 (Easy)
ユーザー motimoti
提出日時 2015-11-29 23:16:57
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,180 bytes
コンパイル時間 666 ms
コンパイル使用メモリ 64,712 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-12 05:55:29
合計ジャッジ時間 1,722 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 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 1 ms
4,352 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 1 ms
4,352 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 WA -
testcase_19 AC 1 ms
4,352 KB
testcase_20 AC 1 ms
4,352 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)

typedef unsigned long long ull;

ull extgcd(ull a, ull b, ull& x, ull& y) {
  ull d = a;
  if(b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  }
  else {
    x = 1; y = 0;
  }
  return d;
}

ull mod_inverse(ull a, ull m) { ull x, y; extgcd(a, m, x, y); return (m + x % m) % m; }

namespace math {
template<class T>
pair<T, T> linear_congruence(vector<T> const& A, vector<T> const& B, vector<T> const& M) {
  T x = 0, m = 1;
  rep(i, A.size()) {
    T a = A[i] * m, b = B[i] - A[i] * x, d = __gcd(M[i], a);
    if(b % d != 0) { return {0, -1}; }
    T t = b / d * mod_inverse(a / d, M[i] / d) % (M[i] / d);
    x += m * t;
    m *= M[i] / d;
  }
  return {x % m, m};
}
}

int main() {

  vector<ull> A(3), B(3),M(3);
  rep(i, 3) {
    cin >> B[i] >> M[i];
    A[i] = 1;
  }
  
  auto ans = math::linear_congruence(A, B, M);
  if(ans.second == -1) {
    cout << -1 << endl;
    exit(0);
  }

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

  return 0;
}
0