結果

問題 No.2104 Multiply-Add
ユーザー chro_96
提出日時 2022-10-21 22:48:48
言語 C
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,123 bytes
コンパイル時間 991 ms
コンパイル使用メモリ 31,328 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-07-01 07:13:49
合計ジャッジ時間 2,359 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

int abs_int (int a) {
  if (a < 0) {
    a = -a;
  }
  return a;
}

int main () {
  int a = 0;
  int b = 0;
  int c = 0;
  int d = 0;
  
  int res = 0;
  
  int tmp1[1000][2] = {};
  int cnt1 = 0;
  
  int tmp2[1000][2] = {};
  int cnt2 = 0;
  
  int ans[1000][2] = {};
  int ans_cnt = 0;
  
  res = scanf("%d", &a);
  res = scanf("%d", &b);
  res = scanf("%d", &c);
  res = scanf("%d", &d);
  
  while (a != 0 && b != 0) {
    if (abs_int(a) < abs_int(b)) {
      int k = b/a;
      b -= k*a;
      tmp1[cnt1][0] = 2;
      tmp1[cnt1][1] = -k;
      cnt1++;
    } else {
      int k = a/b;
      a -= k*b;
      tmp1[cnt1][0] = 1;
      tmp1[cnt1][1] = -k;
      cnt1++;
    }
  }
  
  while (c != 0 && d != 0) {
    if (abs_int(c) < abs_int(d)) {
      int k = d/c;
      d -= k*c;
      tmp2[cnt2][0] = 2;
      tmp2[cnt2][1] = -k;
      cnt2++;
    } else {
      int k = c/d;
      c -= k*d;
      tmp2[cnt2][0] = 1;
      tmp2[cnt2][1] = -k;
      cnt2++;
    }
  }
  
  if (abs_int(a+b) != abs_int(c+d)) {
    printf("-1\n");
    return 0;
  }
  
  if (b != 0) {
    tmp1[cnt1][0] = 1;
    tmp1[cnt1][1] = 1;
    cnt1++;
    a += b;
    tmp1[cnt1][0] = 2;
    tmp1[cnt1][1] = -1;
    cnt1++;
    b -= a;
  }
  
  if (c != 0 && a == -c) {
    tmp1[cnt1][0] = 2;
    tmp1[cnt1][1] = 2;
    cnt1++;
    tmp1[cnt1][0] = 1;
    tmp1[cnt1][1] = -1;
    cnt1++;
    tmp1[cnt1][0] = 2;
    tmp1[cnt1][1] = 2;
    cnt1++;
  } else if (d != 0 && a == d) {
    tmp1[cnt1][0] = 2;
    tmp1[cnt1][1] = 1;
    cnt1++;
    tmp1[cnt1][0] = 1;
    tmp1[cnt1][1] = -1;
    cnt1++;
  } else if (d != 0 && a == -d) {
    tmp1[cnt1][0] = 2;
    tmp1[cnt1][1] = -1;
    cnt1++;
    tmp1[cnt1][0] = 1;
    tmp1[cnt1][1] = 1;
    cnt1++;
  }
  
  for (int i = 0; i < cnt1; i++) {
    ans[i][0] = tmp1[i][0];
    ans[i][1] = tmp1[i][1];
  }
  for (int i = 0; i < cnt2; i++) {
    ans[i+cnt1][0] = tmp2[cnt2-i-1][0];
    ans[i+cnt1][1] = -tmp2[cnt2-i-1][1];
  }
  
  ans_cnt = cnt1+cnt2;
  
  printf("%d\n", ans_cnt);
  for (int i = 0; i < ans_cnt; i++) {
    printf("%d %d\n", ans[i][0], ans[i][1]);
  }
  
  return 0;
}
0