結果

問題 No.2104 Multiply-Add
ユーザー Shirotsume
提出日時 2022-10-22 00:44:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 1,453 bytes
コンパイル時間 209 ms
コンパイル使用メモリ 81,988 KB
実行使用メモリ 54,368 KB
最終ジャッジ日時 2024-07-01 08:39:41
合計ジャッジ時間 2,936 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda: sys.stdin.readline().rstrip()
ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(mi())
inf = 2 ** 63 - 1
mod = 998244353
from math import gcd
a, b, c, d = mi()

if gcd(a, b) == 0 and gcd(c, d) > 0:
    print(-1)
    exit()
if gcd(c, d) != gcd(a, b):
    print(-1)
    exit()

ans = []

x = a
y = b

for i in range(200):
    if x * y == 0:
        break
    if i % 2:
        k = x // y
        ans.append((1, -k))
        x -= k * y
    else:
        k = y // x
        ans.append((2, -k))
        y -= k * x
if x == 0:
    ans.append((1, 1))
    x += y
    ans.append((2, -1))
    y -= x
if x < 0:
    ans.append((2, -1))
    y -= x
    ans.append((1, 1))
    x += y
    ans.append((1, 1))
    x += y
    ans.append((2, -1))
    y -= x
ans2 = []
x = c
y = d
for i in range(200):
    if x * y == 0:
        break
    if i % 2:
        k = x // y
        ans2.append((1, k))
        x -= k * y
    else:
        k = y // x
        ans2.append((2, k))
        y -= k * x
if x == 0:
    ans2.append((1, -1))
    x += y
    ans2.append((2, 1))
    y -= x
if x < 0:
    ans2.append((2, 1))
    y -= x
    ans2.append((1, -1))
    x += y
    ans2.append((1, -1))
    x += y
    ans2.append((2, 1))
    y -= x
ans = ans + ans2[::-1]

x, y = a, b
for t, k in ans:
    if t == 1:
        x += y * k
    else:
        y += x * k

assert x == c and y == d

print(len(ans))
for t, k in ans:
    print(t, k)
0