結果

問題 No.186 中華風 (Easy)
ユーザー motimoti
提出日時 2015-04-20 00:34:00
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,179 bytes
コンパイル時間 462 ms
コンパイル使用メモリ 65,488 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-17 22:25:01
合計ジャッジ時間 1,440 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 WA -
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,376 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 long long ll;

ll gcd(ll a, ll b) {
  if(b == 0) { return a; }
  return gcd(b, a%b);
}

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

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

pair<ll, ll> linear_congruence(const vector<ll>& B, const vector<ll>& M)
{
  ll x = 0, m = 1;
  rep(i, B.size()) {
    ll a = m, b = B[i] - x, d = gcd(M[i],a);
    if(b % d != 0) { return std::make_pair(0,-1); }
    ll t = b / d * mod_inverse(a / d, M[i] / d) % (M[i] / d);
    x = x + m * t;
    m *= M[i] / d;
  }
  return std::make_pair(x % m, m);
}

int main() {

    vector<ll> B(3),M(3);
    rep(i, 3) {
      cin >> B[i] >> M[i];
    }
    
    auto res = linear_congruence(B, M);
    if(res.second == -1) { cout << -1 << endl; }
    else {
      if(res.first == 0) { cout << res.second << endl; }
      else { cout << res.first << endl; }
    }

  return 0;
}
0