結果
| 問題 |
No.2104 Multiply-Add
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-10-21 21:52:02 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,771 bytes |
| コンパイル時間 | 534 ms |
| コンパイル使用メモリ | 81,728 KB |
| 実行使用メモリ | 69,348 KB |
| 最終ジャッジ日時 | 2024-07-01 06:28:28 |
| 合計ジャッジ時間 | 4,378 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 22 WA * 2 RE * 8 |
ソースコード
import sys, random
input = lambda : sys.stdin.readline().rstrip()
write = lambda x: sys.stdout.write(x+"\n"); writef = lambda x: print("{:.12f}".format(x))
debug = lambda x: sys.stderr.write(x+"\n")
YES="Yes"; NO="No"; pans = lambda v: print(YES if v else NO); INF=10**18
LI = lambda : list(map(int, input().split())); II=lambda : int(input()); SI=lambda : [ord(c)-ord("a") for c in input()]
def debug(_l_):
for s in _l_.split():
print(f"{s}={eval(s)}", end=" ")
print()
def dlist(*l, fill=0):
if len(l)==1:
return [fill]*l[0]
ll = l[1:]
return [dlist(*ll, fill=fill) for _ in range(l[0])]
a,b,c,d = list(map(int, input().split()))
a0 = a
b0 = b
c0 = c
d0 = d
def gcd(a,b):
if a==0 or b==0:
return []
if abs(a)>abs(b):
t = 1
k = a//b
na = a%b
nb = b
else:
t = 2
k = b//a
na = a
nb = b%a
return [(t,-k)] + gcd(na,nb)
import math
g1 = math.gcd(a,b)
g2 = math.gcd(c,d)
if g2!=g1:
print(-1)
else:
def sub(a,b):
l0 = []
if a<0:
if b==0:
l0.extend([(2,-1), (1, 1)])
a = 0
b = -a
elif b>0:
d,m = divmod(abs(a), abs(b))
l0.append((1, d+1))
a += (d+1)*b
else:
d,m = divmod(abs(a), abs(b))
l0.append((1, -(d+1)))
a += -(d+1)*b
if a==0:
l0.extend([(1,-1), (2,1)])
a = -b
b = 0
else:
assert a>0
d,m = divmod(abs(b), abs(a))
l0.append((2, (d+1)))
b += a*(d+1)
elif b<0:
if a==0:
l0.extend([(1,-1), (2,1)])
b = 0
a = -b
else:
# a>0
d,m = divmod(abs(b), a)
l0.append((2, d+1))
b += (d+1)*a
assert a>=0 and b>=0, breakpoint()
return a,b,l0
a,b,l0 = sub(a,b)
c,d,l1 = sub(c,d)
res = gcd(a,b)
res2 = gcd(c,d)
aa,bb = res[-1]
aa2,bb2 = res2[-1]
if aa==aa2:
pass
else:
if aa>0:
res.append((2,1))
res.append((1,-1))
else:
res.append((1,1))
res.append((2,-1))
for t,k in res2[::-1]:
res.append((t,-k))
res = l0 + res
for t,k in l1[::-1]:
res.append((t,-k))
aa = a0
bb = b0
for t,k in res:
if t==1:
aa += k*bb
else:
bb += k*aa
#assert aa==c0 and bb==d0
print(len(res))
for item in res:
write(" ".join(map(str, item)))