結果

問題 No.2767 Add to Divide
ユーザー rlangevinrlangevin
提出日時 2024-05-31 21:38:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 618 bytes
コンパイル時間 316 ms
コンパイル使用メモリ 82,388 KB
実行使用メモリ 65,236 KB
最終ジャッジ日時 2024-05-31 21:38:32
合計ジャッジ時間 2,035 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
57,756 KB
testcase_01 AC 39 ms
58,196 KB
testcase_02 AC 36 ms
52,936 KB
testcase_03 AC 91 ms
64,016 KB
testcase_04 AC 37 ms
53,360 KB
testcase_05 AC 69 ms
61,640 KB
testcase_06 AC 66 ms
60,320 KB
testcase_07 AC 68 ms
59,984 KB
testcase_08 AC 85 ms
65,236 KB
testcase_09 AC 74 ms
61,512 KB
testcase_10 AC 75 ms
61,712 KB
testcase_11 AC 78 ms
62,472 KB
testcase_12 AC 78 ms
61,432 KB
testcase_13 AC 76 ms
61,916 KB
testcase_14 AC 76 ms
62,616 KB
testcase_15 AC 72 ms
61,768 KB
testcase_16 AC 74 ms
61,668 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

def div(n):
    if n <= 0:
        return []
    S = set()
    i = 1
    while i * i <= n:
        if n % i == 0:
            S.add(i)
            S.add(n//i)
        i += 1
    return sorted(list(S))

def ceil(A, d):
    return (A + d - 1)//d * d - A


T = int(input())
inf = 10 ** 18
for _ in range(T):
    A, B = map(int, input().split())
    if B % A == 0:
        print(0)
        continue
    ans = inf
    for d in div(B - A):
        x = ceil(A, d)
        if (B + x) % (A + x) == 0:
            ans = min(ans, x)
            
    print(ans) if ans != inf else print(-1) 
0