結果

問題 No.2104 Multiply-Add
ユーザー 👑 hitonanodehitonanode
提出日時 2022-09-05 09:53:21
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,978 bytes
コンパイル時間 7,694 ms
コンパイル使用メモリ 275,452 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-30 13:45:14
合計ジャッジ時間 9,300 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 2 ms
6,944 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 2 ms
6,944 KB
testcase_31 AC 2 ms
6,940 KB
testcase_32 AC 2 ms
6,940 KB
testcase_33 AC 2 ms
6,940 KB
testcase_34 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <numeric>
#include <utility>
#include <vector>
using namespace std;

#include "testlib.h"

vector<pair<int, int>> reduce(int a, int b) {
    // (a, b) -> (abs(gcd(a, b)), 0)
    vector<pair<int, int>> ret;
    ret.emplace_back(a, b);

    while (a and b) {
        if (abs(a) > abs(b)) {
            int r = abs(a) / abs(b);
            if (a > 0) {
                a -= r * abs(b);
            } else {
                a += r * abs(b);
            }
        } else {
            int r = abs(b) / abs(a);
            if (b > 0) {
                b -= r * abs(a);
            } else {
                b += r * abs(a);
            }
        }
        ret.emplace_back(a, b);
    }

    if (a < 0) {
        b = abs(a);
        ret.emplace_back(a, b);
        a = 0;
        ret.emplace_back(a, b);
    }

    if (a == 0) {
        a = abs(b);
        ret.emplace_back(a, b);
        b = 0;
        ret.emplace_back(a, b);
    }

    return ret;
}

constexpr int MAXI = 100000000;

int main(int argc, char *argv[]) {
    registerValidation(argc, argv);
    int a = inf.readInt(-MAXI, MAXI);
    inf.readSpace();
    int b = inf.readInt(-MAXI, MAXI);
    inf.readSpace();
    int c = inf.readInt(-MAXI, MAXI);
    inf.readSpace();
    int d = inf.readInt(-MAXI, MAXI);
    inf.readEoln();
    inf.readEof();

    if (std::gcd(abs(a), abs(b)) != std::gcd(abs(c), abs(d))) {
        puts("-1");
        return 0;
    }

    auto ab_to_i = reduce(a, b);
    auto cd_to_i = reduce(c, d);

    auto ret = ab_to_i;
    ret.insert(ret.end(), cd_to_i.rbegin(), cd_to_i.rend());

    cout << ret.size() - 1 << '\n';
    for (int i = 1; i < int(ret.size()); ++i) {
        auto [px, py] = ret.at(i - 1);
        auto [nx, ny] = ret.at(i);

        if (px != nx) {
            cout << "1 " << (nx - px) / py << '\n';
        } else if (py != ny) {
            cout << "2 " << (ny - py) / px << '\n';
        } else {
            cout << "1 0\n";
        }
    }
}
0