結果

問題 No.2767 Add to Divide
コンテスト
ユーザー LyricalMaestro
提出日時 2026-02-05 02:34:43
言語 PyPy3
(7.3.17)
結果
AC  
実行時間 132 ms / 2,000 ms
コード長 887 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 622 ms
コンパイル使用メモリ 82,344 KB
実行使用メモリ 63,224 KB
最終ジャッジ日時 2026-02-05 02:34:47
合計ジャッジ時間 3,470 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

## https://yukicoder.me/problems/no/2767

import math

def solve(A, B):
    if B == A:
        return 0
    
    c = B - A
    sqrt_c = int(math.sqrt(c))

    divisors = []
    for p in range(1, sqrt_c + 1):
        if c % p == 0:
            q = c // p
            divisors.append(p)
            if q != p:
                divisors.append(q)

    answer_x = float("inf")
    for p in divisors:
        # k - 1 = q
        # X + A = p
        X = p - A
        if X >= 0 and (B + X) % (A + X) == 0:
            answer_x = min( answer_x, X)
    
    if answer_x == float("inf"):
        return -1
    else:
        return answer_x



def main():
    T = int(input())
    answers = []
    for _ in range(T):
        A, B = map(int ,input().split())
        ans = solve(A, B)
        answers.append(ans)

    for ans in answers:
        print(ans)





if __name__ == "__main__":
    main()
0