結果
| 問題 |
No.2104 Multiply-Add
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-10-21 23:58:19 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 43 ms / 2,000 ms |
| コード長 | 1,160 bytes |
| コンパイル時間 | 162 ms |
| コンパイル使用メモリ | 82,352 KB |
| 実行使用メモリ | 52,992 KB |
| 最終ジャッジ日時 | 2024-07-01 08:01:06 |
| 合計ジャッジ時間 | 2,945 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 32 |
ソースコード
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)