結果

問題 No.186 中華風 (Easy)
ユーザー goodbatongoodbaton
提出日時 2018-12-07 11:51:12
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,409 bytes
コンパイル時間 808 ms
コンパイル使用メモリ 96,912 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-27 00:53:06
合計ジャッジ時間 1,812 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 100010

// {a, {b, c}} : a = gcd(p, q) = bp + cq
pair<ll,pair<ll,ll>> ext_gcd(ll p, ll q) {
  if (q == 0) return {p, {1, 0}};
  auto r = ext_gcd(q, p%q);
  return {r.first, {r.second.second, r.second.first - p/q*r.second.second}};
}
 
/* 中国剰余定理 */
pair<ll,ll> CRT(ll a1, ll m1, ll a2, ll m2){
  auto eg = ext_gcd(m1, m2);
  ll d = eg.first;
  if((a2 - a1) % d) return {0, -1};
  ll m = m1 / d * m2;
  ll p = eg.second.first; //(eg.second.first + m) % m;
  ll r = ((a1 + m1 * ((a2 - a1) / d * p % (m2/d))) % m + m) % m;
  return {r, m};
}

int main(){
  ll r = 0, m = 1;

  for(int i=0;i<3;i++){
    int x, y;
    cin >> x >> y;
    auto res = CRT(r, m, x, y);
    r = res.first;
    m = res.second;
    
    if(m == -1){
      puts("-1");
      return 0;
    }
  }
  
  cout << (r == 0 ? m : r) << endl;
  
  return 0;
}
0