結果
| 問題 |
No.186 中華風 (Easy)
|
| コンテスト | |
| ユーザー |
yomog
|
| 提出日時 | 2021-11-09 14:13:12 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 1,255 bytes |
| コンパイル時間 | 1,854 ms |
| コンパイル使用メモリ | 196,812 KB |
| 最終ジャッジ日時 | 2025-01-25 15:00:35 |
|
ジャッジサーバーID (参考情報) |
judge6 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 23 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct chinese_remainder_theorem {
static constexpr pair<ll, ll> no_sol = {0, -1};
static ll mod(ll x, ll y) {
x %= y;
return x < 0 ? x + y : x;
}
static ll ext_gcd(ll a, ll b, ll &x, ll &y) {
if (b == 0) return x = 1, y = 0, a;
ll g = ext_gcd(b, a % b, y, x);
y -= a / b * x;
return g;
}
static pair<ll, ll> solve(vector<ll> &bs, vector<ll> &ms) {
assert(bs.size() == ms.size());
ll r = 0, m = 1;
for (int i = 0; i < (int)bs.size(); i++) {
ll p, q, d = ext_gcd(m, ms[i], p, q);
if ((bs[i] - r) % d) return no_sol;
ll tmp = (bs[i] - r) / d * p % (ms[i] / d);
r += m * tmp;
m *= ms[i] / d;
}
return {mod(r, m), m};
}
};
using crt = chinese_remainder_theorem;
int main() {
vector<ll> x(3), y(3);
bool exist_non_zero = false;
for (int i = 0; i < 3; i++) {
cin >> x[i] >> y[i];
if (x[i]) exist_non_zero = true;
}
auto [r, m] = crt::solve(x, y);
if (m == -1) {
cout << -1 << '\n';
return 0;
}
cout << (exist_non_zero ? r : m) << '\n';
return 0;
}
yomog