結果

問題 No.2104 Multiply-Add
ユーザー shoshoshom
提出日時 2022-10-22 00:48:36
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 43 ms / 2,000 ms
コード長 1,563 bytes
コンパイル時間 1,710 ms
コンパイル使用メモリ 197,264 KB
最終ジャッジ日時 2025-02-08 10:47:56
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

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