結果
| 問題 |
No.2104 Multiply-Add
|
| コンテスト | |
| ユーザー |
risujiroh
|
| 提出日時 | 2022-10-21 23:07:05 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 2,000 ms |
| コード長 | 1,186 bytes |
| コンパイル時間 | 3,194 ms |
| コンパイル使用メモリ | 250,328 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-01 07:26:48 |
| 合計ジャッジ時間 | 4,334 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 32 |
ソースコード
#include <bits/stdc++.h>
std::vector<std::pair<int, int>> go(int x, int y) {
std::vector<std::pair<int, int>> ret;
auto op = [&](int t, int k) {
ret.emplace_back(t, k);
if (t == 1) {
x += k * y;
} else {
y += k * x;
}
};
while (true) {
if (x && y) {
if (std::abs(x) < std::abs(y)) {
op(2, -y / x);
} else {
op(1, -x / y);
}
} else {
if (x) {
op(2, 1);
op(1, -1);
}
assert(x == 0);
if (y < 0) {
op(1, -1);
op(2, 2);
op(1, -1);
}
assert(x == 0 && 0 <= y);
break;
}
}
return ret;
}
void solve(std::istream& is, std::ostream& os) {
int a, b, c, d;
is >> a >> b >> c >> d;
if (std::gcd(a, b) != std::gcd(c, d)) {
os << "-1\n";
return;
}
auto op1 = go(a, b);
auto op2 = go(c, d);
os << op1.size() + op2.size() << '\n';
for (auto [t, k] : op1) {
os << t << ' ' << k << '\n';
}
std::reverse(op2.begin(), op2.end());
for (auto [t, k] : op2) {
os << t << ' ' << -k << '\n';
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
solve(std::cin, std::cout);
}
risujiroh