結果

問題 No.2104 Multiply-Add
ユーザー baLoon8128baLoon8128
提出日時 2022-10-21 22:52:07
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,340 bytes
コンパイル時間 3,814 ms
コンパイル使用メモリ 230,192 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-13 23:05:14
合計ジャッジ時間 5,860 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,504 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:71:23: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   71 |             for (auto [x, y] : fr) cout << x << " " << y << '\n';
      |                       ^
main.cpp:72:23: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   72 |             for (auto [x, y] : bk) cout << x << " " << y << '\n';
      |                       ^

ソースコード

diff #

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using pii = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define repr(i, n) for (int i = (int)(n - 1); i >= 0; --i)

int gcd(int x, int y) {
    if (x < 0) x = -x;
    if (y < 0) y = -y;
    return x == 0 ? y : gcd(y % x, x);
}
int main() {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    if (a == c && b == d) {
        cout << 0 << endl;
    } else {
        if (gcd(a, b) == gcd(c, d)) {
            vector<pii> fr, bk;
            while (1) {
                if (b == 0 && a < 0) {
                    fr.emplace_back(2, -1);
                    fr.emplace_back(1, 1);
                    b = -a, a = 0;
                }
                if (a == 0) {
                    a = abs(b);
                    fr.emplace_back(1, a / b);
                    fr.emplace_back(2, -b / a);
                    b = 0;
                }
                if (b == 0) break;
                if (abs(a) > abs(b)) {
                    fr.emplace_back(1, -a / b);
                    a %= b;
                } else {
                    fr.emplace_back(2, -b / a);
                    b %= a;
                }
            }
            while (1) {
                if (d == 0 && c < 0) {
                    bk.emplace_back(2, 1);
                    bk.emplace_back(1, -1);
                    d = -c, c = 0;
                }
                if (c == 0) {
                    c = abs(d);
                    bk.emplace_back(1, -c / d);
                    bk.emplace_back(2, d / c);
                    d = 0;
                }
                if (d == 0) break;
                if (abs(c) > abs(d)) {
                    bk.emplace_back(1, c / d);
                    c %= d;
                } else {
                    bk.emplace_back(2, d / c);
                    d %= c;
                }
            }
            reverse(bk.begin(), bk.end());

            cout << (int)(fr.size() + bk.size()) << endl;
            for (auto [x, y] : fr) cout << x << " " << y << '\n';
            for (auto [x, y] : bk) cout << x << " " << y << '\n';
        } else {
            cout << -1 << endl;
        }
    }
    return 0;
}
0