結果
| 問題 |
No.186 中華風 (Easy)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-11-13 04:43:45 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,436 bytes |
| コンパイル時間 | 4,766 ms |
| コンパイル使用メモリ | 290,392 KB |
| 実行使用メモリ | 6,824 KB |
| 最終ジャッジ日時 | 2024-11-13 04:43:55 |
| 合計ジャッジ時間 | 6,271 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 21 WA * 2 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <typename T = ll>
T ext_gcd(T a, T b, T& x, T& y) {
x = 1; y = 0;
T x1 = 0, y1 = 1, a1 = a, b1 = b;
while (b1 > 0) {
T q = a1 / b1;
tie(x, x1) = make_tuple(x1, x - q * x1);
tie(y, y1) = make_tuple(y1, y - q * y1);
tie(a1, b1) = make_tuple(b1, a1 - q * b1);
}
return a1;
}
template <typename T = ll>
T crt_coprimes(vector<T> a, vector<T> m) {
int k = a.size();
T prod = 1, ans = 0;
for (int i = 0; i < k; i++) prod *= m[i];
for (int i = 0; i < k; i++) {
T mi = prod / m[i], ni, y;
ext_gcd<T>(mi, m[i], ni, y);
a[i] %= m[i]; ni %= m[i];
ans = (ans + a[i] * ni % m[i] * mi) % prod;
}
return (ans + prod) % prod;
}
template <typename T = ll>
bool crt_general(vector<T> a, vector<T> m, T& ans) {
int k = a.size();
map<T, pair<T, T>> mp;
for (int i = 0; i < k; i++) {
T tmp = m[i];
for (T j = 2; j * j <= tmp; j++) {
if (tmp % j == 0) {
T res = 1;
while (tmp % j == 0) {
tmp /= j; res *= j;
}
T ap = a[i] % res;
if (mp.contains(j)) {
if (mp[j].second < res) {
swap(mp[j].first, ap);
swap(mp[j].second, res);
}
if (mp[j].first % res != ap) {
return false;
}
} else {
mp[j] = make_pair(ap, res);
}
}
}
if (tmp > 1) {
if (mp.contains(tmp)) {
if ((mp[tmp].first - a[i]) % tmp) {
return false;
}
} else {
mp[tmp] = make_pair(a[i] % tmp, tmp);
}
}
}
vector<T> na, nm;
for (auto it = mp.begin(); it != mp.end(); it++) {
na.push_back(it->second.first);
nm.push_back(it->second.second);
}
ans = crt_coprimes(na, nm);
return true;
}
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
vector<ll> x(3), y(3);
for (int i = 0; i < 3; i++) {
cin >> x[i] >> y[i];
}
ll ans;
if (crt_general(x, y, ans)) {
cout << ans << "\n";
} else {
cout << -1 << "\n";
}
return 0;
}