結果

問題 No.186 中華風 (Easy)
ユーザー legosukelegosuke
提出日時 2019-06-05 11:09:37
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,220 bytes
コンパイル時間 1,555 ms
コンパイル使用メモリ 168,468 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-27 00:55:48
合計ジャッジ時間 2,536 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(c) (c).begin(),(c).end()
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define MINF(a) memset(a,0x3f,sizeof(a))
#define POW(n) (1LL<<(n))
#define IN(i,a,b) (a <= i && i <= b)
using namespace std;
template <typename T> inline bool CHMIN(T& a,T b) { if(a>b) { a=b; return 1; } return 0; }
template <typename T> inline bool CHMAX(T& a,T b) { if(a<b) { a=b; return 1; } return 0; }
template <typename T> inline void SORT(T& a) { sort(ALL(a)); }
template <typename T> inline void REV(T& a) { reverse(ALL(a)); }
template <typename T> inline void UNI(T& a) { sort(ALL(a)); a.erase(unique(ALL(a)),a.end()); }
const int MOD = 1000000007;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-10;
/* ---------------------------------------------------------------------------------------------------- */

// 拡張gcd
int ext_gcd(int a, int b, int &x, int &y) {
  if (b == 0) {
    x = 1; y = 0; return a;
  }
  int q = a/b;
  int g = ext_gcd(b,a-q*b,x,y);
  int z = x-q*y;
  x = y; y = z;
  return g;
}

// 逆元 | a と m は互いに素
int inv_mod(int a, int m) {
  int x,y;
  ext_gcd(a,m,x,y);
  x %= m;
  if (x < 0) x += m;
  return x;
}

// 中国剰余定理
// x ≡ r (mod m) の (r,m) を返す.m = lcm(m_1,m_2,...)
// 解なしの場合 (0,-1) を返す.
pair<int,int> crt(const vector<int> &b, const vector<int> &m) {
  int r = 0, M = 1;
  for (int i = 0; i < (int)b.size(); i++) {
    int p,q;
    int d = ext_gcd(M,m[i],p,q);
    if ((b[i]-r)%d != 0) return make_pair(0,-1);
    int tmp = (b[i]-r)/d*p%(m[i]/d);
    r += M*tmp;
    M *= m[i]/d;
  }
  r %= M;
  if (r < 0) r += M;
  return make_pair(r,M);
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  vector<int> b(3),m(3);
  bool all_zero = true;
  for (int i = 0; i < 3; i++) {
    cin >> b[i] >> m[i];
    if (b[i]) all_zero = false;
  }
  pii ans = crt(b,m);
  if (ans.second < 0) cout << -1 << endl;
  else if (all_zero) cout << ans.second << endl;
  else cout << ans.first << endl;

  return 0;
}
0