結果

問題 No.2104 Multiply-Add
ユーザー matsup10matsup10
提出日時 2022-10-22 00:15:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,584 bytes
コンパイル時間 4,017 ms
コンパイル使用メモリ 263,368 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 00:15:32
合計ジャッジ時間 4,477 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/extc++.h>
using namespace std;
using ll = long long;
#define REP(i,n) for(int i=0;i<int(n);i++)
#define FOR(i,a,b) for(int i=a;i<=int(b);i++)
#define ALL(x) x.begin(),x.end()
#define INFL LLONG_MAX


signed main() {
  ll a, b, c, d;
  cin >> a >> b >> c >> d;

  if(gcd(a, b) != gcd(c, d)) {
    cout << -1;
    return 0;
  }

  if(a==c && b==d) {
    cout << 0;
    return 0;
  }

  vector<pair<int,ll>> ans1;

  ll ta = a, tb = b;

  if(ta==0) {
    ans1.push_back({1, 1});
    ta += tb;
  }

  if(tb==0) {
    ans1.push_back({2, 1});
    tb += ta;
  }

  if(tb < 0) {
    ans1.push_back({2, (ta > 0 ? 1 : -1) * (abs(tb) / abs(ta) + 1)});
    tb += (abs(tb) / abs(ta) + 1) * abs(ta);
  }

  if(ta < 0) { 
    ans1.push_back({1, (abs(ta) / abs(tb) + 1)});
    ta += (abs(ta) / abs(tb) + 1) * tb;
  }

  while(ta != 0) {
    if(tb > ta) {
      ans1.push_back({2, -((tb - 1) / ta)});
      tb %= ta;
      if(tb == 0) tb = ta;
    } else if (tb < ta) {
      ans1.push_back({1, -(ta / tb)});
      ta %= tb;
    } else {
      ans1.push_back({1, -1});
      ta = 0;
    }
  }


  vector<pair<int,ll>> ans2;
  ll tc = c, td = d;

  if(tc==0) {
    ans2.push_back({1, -1});
    tc += td;
  }

  if(td==0) {
    ans2.push_back({2, -1});
    td += tc;
  }

  if(td < 0) {
    ans2.push_back({2, -(tc > 0 ? 1 : -1) * (abs(td) / abs(tc) + 1)});
    td += (abs(td) / abs(tc) + 1) * abs(tc);
  }

  if(tc < 0) { 
    ans2.push_back({1, -(abs(tc) / abs(td) + 1)});
    tc += (abs(tc) / abs(td) + 1) * td;
  }
  
  while(tc != 0) {
    if(td > tc) {
      ans2.push_back({2, ((td-1) / tc)});
      td %= tc;
      if(td == 0) td = tc;
    } else if (td < tc) {
      ans2.push_back({1, (tc / td)});
      tc %= td;
    } else {
      ans2.push_back({1, 1});
      tc = 0;
    }
  }

  reverse(ALL(ans2));

  cout << ans1.size() + ans2.size() << endl;
  REP(i, ans1.size()) cout << ans1[i].first << ' ' << ans1[i].second << endl;
  REP(i, ans2.size()) cout << ans2[i].first << ' ' << ans2[i].second << endl;

  // REP(i, ans1.size()) {
  //   if(1==ans1[i].first) {
  //     cout << a + b * ans1[i].second << ' '  << b<< endl;
  //     a += b * ans1[i].second;
  //   } else {
  //     cout << a << ' ' << b + a * ans1[i].second<< endl;
  //     b += a * ans1[i].second;
  //   }
  // }
  // REP(i, ans2.size()) {
  //   if(1==ans2[i].first) {
  //     cout << a + b * ans2[i].second << ' ' << b << endl;
  //     a += b * ans2[i].second;
  //   } else {
  //     cout << a << ' ' << b + a * ans2[i].second<< endl;
  //     b += a * ans2[i].second;
  //   }
  // }
  return 0;
}
0