結果

問題 No.2104 Multiply-Add
ユーザー flygonflygon
提出日時 2022-10-21 23:58:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 79 ms / 2,000 ms
コード長 1,160 bytes
コンパイル時間 296 ms
コンパイル使用メモリ 87,212 KB
実行使用メモリ 71,556 KB
最終ジャッジ日時 2023-09-14 00:03:35
合計ジャッジ時間 4,901 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
71,524 KB
testcase_01 AC 72 ms
71,528 KB
testcase_02 AC 73 ms
71,504 KB
testcase_03 AC 78 ms
71,184 KB
testcase_04 AC 75 ms
71,280 KB
testcase_05 AC 75 ms
71,152 KB
testcase_06 AC 76 ms
71,448 KB
testcase_07 AC 79 ms
70,968 KB
testcase_08 AC 76 ms
71,204 KB
testcase_09 AC 75 ms
71,172 KB
testcase_10 AC 77 ms
71,228 KB
testcase_11 AC 74 ms
71,556 KB
testcase_12 AC 75 ms
71,188 KB
testcase_13 AC 74 ms
71,516 KB
testcase_14 AC 77 ms
71,140 KB
testcase_15 AC 75 ms
71,348 KB
testcase_16 AC 76 ms
70,972 KB
testcase_17 AC 75 ms
70,972 KB
testcase_18 AC 75 ms
71,192 KB
testcase_19 AC 75 ms
71,188 KB
testcase_20 AC 74 ms
71,508 KB
testcase_21 AC 75 ms
71,388 KB
testcase_22 AC 75 ms
71,516 KB
testcase_23 AC 75 ms
71,456 KB
testcase_24 AC 75 ms
71,208 KB
testcase_25 AC 76 ms
71,296 KB
testcase_26 AC 75 ms
71,200 KB
testcase_27 AC 74 ms
71,428 KB
testcase_28 AC 74 ms
71,112 KB
testcase_29 AC 75 ms
71,540 KB
testcase_30 AC 76 ms
71,428 KB
testcase_31 AC 75 ms
71,188 KB
testcase_32 AC 76 ms
71,348 KB
testcase_33 AC 75 ms
71,488 KB
testcase_34 AC 75 ms
71,140 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

a,b,c,d = map(int,input().split())
c,d,a,b = a,b,c,d
from math import gcd
if gcd(a,b) != gcd(c,d):
  print(-1)
  exit()
if a == c and b == d:
  print(0)
  exit()
cd = [[c,d]]
ab = [[a,b]]

if c == 0:
  cd.append([c+d, d])
  c,d = cd[-1]
if d == 0:
  cd.append([c, c+d])
  c,d = cd[-1]
if a == 0:
  ab.append([a+b,b])
  a,b = ab[-1]
if b == 0:
  ab.append([a, a+b])
  a,b = ab[-1]
if c < 0:
  c = c + (abs(c)//abs(d)+1)*abs(d)
  cd.append([c,d])
if d < 0:
  d = d + (abs(d)//c+1)*c
  cd.append([c,d])
while c != 0 and d != 0:
  if c < d:
    d %= c
    cd.append([c,d])
  else:
    c %= d
    cd.append([c,d])
  
if a < 0:
  a = a + (abs(a)//abs(b)+1)*abs(b)
  ab.append([a,b])
if b < 0:
  b = b + (abs(b)//a+1)*a
  ab.append([a,b])
while a != 0 and b != 0:
  if a < b:
    b %= a
    ab.append([a,b])
  else:
    a %= b
    ab.append([a,b])
if ab[-1] == cd[-1]:
  ab.pop()
  cd += ab[::-1]
else:
  ab.append([max(ab[-1]), max(ab[-1])])
  cd += ab[::-1]
print(len(cd)-1)
for i in range(1,len(cd)):
  c,d = cd[i]
  if c == cd[i-1][0]:
    dd = cd[i-1][1]
    tmp = (d - dd)//c
    print(2, tmp)
  else:
    cc = cd[i-1][0]
    tmp = (c-cc)//d
    print(1, tmp)

0