結果
問題 |
No.186 中華風 (Easy)
|
ユーザー |
![]() |
提出日時 | 2024-06-23 18:33:29 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 1,000 bytes |
コンパイル時間 | 597 ms |
コンパイル使用メモリ | 73,616 KB |
最終ジャッジ日時 | 2025-02-22 00:13:39 |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 23 |
ソースコード
#include <iostream> #include <vector> #include <algorithm> //(val, lcm(m1, m2)): val=b1(mod m1), val=b2(mod m2) std::pair<long long, long long> crt(long long b1, long long m1, long long b2, long long m2) { long long x = 1, y = 0, x1 = 0, y1 = 1, g = m1, b = m2; while (b != 0) { long long n = g / b; g %= b; std::swap(g, b); x -= x1 * n; std::swap(x, x1); y -= y1 * n; std::swap(y, y1); } if ((b2 - b1) % g != 0) return {0, -1}; long long lc = m1 / g * m2; return {((b1 + m1 * ((b2 - b1) / g * x % (m2 / g))) % lc + lc) % lc, lc}; } using namespace std; int main() { int x[3], y[3]; for (int i = 0; i < 3; ++i) cin >> x[i] >> y[i]; pair<long long, long long> ans = crt(x[0], y[0], x[1], y[1]); if (ans.second == -1) { cout << -1 << endl; return 0; } ans = crt(ans.first, ans.second, x[2], y[2]); if (ans.second == -1) cout << -1 << endl; else { if (ans.first == 0) ans.first = ans.second; cout << ans.first << endl; } }