結果
| 問題 |
No.186 中華風 (Easy)
|
| コンテスト | |
| ユーザー |
emthrm
|
| 提出日時 | 2021-09-24 15:34:53 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 2,000 ms |
| コード長 | 1,234 bytes |
| コンパイル時間 | 1,015 ms |
| コンパイル使用メモリ | 77,300 KB |
| 最終ジャッジ日時 | 2025-01-24 16:37:39 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 23 |
ソースコード
#include <algorithm>
#include <iostream>
#include <tuple>
#include <utility>
#include <vector>
long long mod_inv(long long a, int m) {
if ((a %= m) < 0) a += m;
if (std::__gcd(a, static_cast<long long>(m)) != 1) return -1;
long long b = m, x = 1, u = 0;
while (b > 0) {
long long q = a / b;
std::swap(a -= q * b, b);
std::swap(x -= q * u, u);
}
x %= m;
return x < 0 ? x + m : x;
}
template <typename T>
std::pair<T, T> simultaneous_linear_congruence(const std::vector<T> &a, const std::vector<T> &b, const std::vector<T> &m) {
const int n = a.size();
T x = 0, md = 1;
for (int i = 0; i < n; ++i) {
T l = md * a[i], r = -x * a[i] + b[i], g = std::__gcd(l, m[i]);
if (r % g != 0) return {0, -1};
x += md * (r / g * mod_inv(l / g, m[i] / g) % (m[i] / g));
md *= m[i] / g;
}
return {x < 0 ? x + md : x, md};
}
int main() {
constexpr int N = 3;
std::vector<long long> x(N), y(N);
for (int i = 0; i < N; ++i) {
std::cin >> x[i] >> y[i];
}
long long ans, mod;
std::tie(ans, mod) = simultaneous_linear_congruence(std::vector<long long>(N, 1), x, y);
if (mod == 0) {
std::cout << "-1\n";
} else {
std::cout << (ans == 0 ? mod : ans) << '\n';
}
return 0;
}
emthrm