結果

問題 No.186 中華風 (Easy)
ユーザー vq3spPZjIacECS3vq3spPZjIacECS3
提出日時 2022-02-23 18:41:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,258 bytes
コンパイル時間 1,097 ms
コンパイル使用メモリ 119,428 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 11:40:22
合計ジャッジ時間 2,333 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <iomanip>
#define _USE_MATH_DEFINES
#include <math.h>
#include <functional>
#include <complex>
using namespace std;

#define rep(i, x) for (ll i = 0; i < x; ++i)
#define repn(i, x) for (ll i = 1; i <= x; ++i)

using ll = long long;
const ll INF = 1e17;
const ll MOD = 1000000007;
const ll MAX = 4000001;
const long double eps = 1E-14;

ll max(ll a, ll b)
{
    if (a > b)
    {
        return a;
    }
    return b;
}

ll min(ll a, ll b)
{
    if (a > b)
    {
        return b;
    }
    return a;
}

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

ll lcm(ll a, ll b)
{
    return a / gcd(a, b) * b;
}

using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vvvll = vector<vector<vector<ll>>>;

///////////////////////////////////////////////////////////////////

inline long long mod(long long a, long long m) { return (a % m + m) % m; }

long long extGcd(long long a, long long b, long long &p, long long &q) {
  if (b == 0) {
    p = 1;
    q = 0;
    return a;
  }
  long long d = extGcd(b, a % b, q, p);
  q -= a / b * p;
  return d;
}

// 中国剰余定理
// リターン値を (r, m) とすると解は x ≡ r (mod. m)
// 解なしの場合は (0, -1) をリターン
pair<long long, long long> ChineseRem(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(mod(r, M), M);
}

int main()
{
    vll x(3), y(3);
    rep(i, 3)
    {
        cin >> x.at(i) >> y.at(i);
    }

    pair<ll, ll> res = ChineseRem(x, y);

    if (res.second == -1)
    {
        cout << -1 << endl;
    }
    else if (res.first == 0)
    {
        cout << res.second << endl;
    }
    else
    {
        cout << res.first << endl;
    }
}
0