結果

問題 No.2104 Multiply-Add
ユーザー shoshoshomshoshoshom
提出日時 2022-10-22 00:48:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,563 bytes
コンパイル時間 1,998 ms
コンパイル使用メモリ 202,480 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-14 00:41:39
合計ジャッジ時間 4,023 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#include <climits>
using namespace std;

vector<pair<int, int>> solve(int& x, int& y) {
  vector<pair<int, int>> res;
  auto action = [&](int t, int k) {
    if (t == 1) {
      x += k * y;
    } else {
      y += k * x;
    }
    res.emplace_back(t, k);
  };

  if (x == 0)
    action(1, 1);
  if (y == 0)
    action(2, 1);

  if (x < 0) {
    int need = 1 - x;
    if (y < 0) {
      int q = (need - y - 1) / -y;
      action(1, -q);
    } else {
      int q = (need + y - 1) / y;
      action(1, q);
    }
  }
  if (y < 0) {
    int need = 1 - y;
    if (x < 0) {
      int q = (need - x - 1) / -x;
      action(2, -q);
    } else {
      int q = (need + x - 1) / x;
      action(2, q);
    }
  }
  int g = gcd(x, y);
  while (x > 0 && y > 0) {
    if (x < y) {
      action(2, y / x * -1);
    } else {
      action(1, x / y * -1);
    }
  }
  if (x == 0) {
    action(1, 1);
    action(2, -1);
  }
  return res;
}

int main() {
  iostream::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  if (a == c && b == d) {
    cout << 0 << '\n';
    return 0;
  }
  if (a == 0 && b == 0) {
    cout << -1 << '\n';
    return 0;
  }

  auto res1 = solve(a, b);
  auto res2 = solve(c, d);

  if (a != c || b != d) {
    cout << -1 << '\n';
    return 0;
  }
  cout << (int)res1.size() + (int)res2.size() << '\n';

  for (auto [t, k]: res1) {
    cout << t << " " << k << '\n';
  }
  reverse(res2.begin(), res2.end());

  for (auto [t, k]: res2) {
    cout << t << " " << -k << '\n';
  }

  return 0;
}
0