結果

問題 No.816 Beautiful tuples
コンテスト
ユーザー roaris
提出日時 2019-07-25 20:27:50
言語 Python3
(3.14.3 + numpy 2.4.2 + scipy 1.17.0)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 94 ms / 1,500 ms
コード長 800 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 484 ms
コンパイル使用メモリ 20,820 KB
実行使用メモリ 15,360 KB
最終ジャッジ日時 2026-03-23 10:32:10
合計ジャッジ時間 3,347 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

def factorize(n):
    fct = []
    div = 2
    
    while div ** 2 <= n:
        cnt = 0
        while n % div == 0:
            n //= div
            cnt += 1
        if cnt > 0:
            fct.append((div, cnt))
        div += 1
    
    if n > 1:
        fct.append((n, 1))
    
    return fct

def dfs(fct, depth, cur):
    div, cnt = fct[depth]
    
    if depth == len(fct) - 1:
        return [cur*div**i for i in range(cnt+1)]
        
    rseq = []
    
    for i in range(cnt+1):
        rseq += dfs(fct, depth+1, cur*div**i)
        
    return rseq
    
    
A, B = map(int, input().split())
fct = factorize(A+B)
C_cand = list(sorted(dfs(fct, 0, 1)))

for C in C_cand:
    if A != C and B != C and (B + C) % A == 0 and (C + A) % B == 0:
        print(C)
        break
else:
    print(-1)
0