結果

問題 No.2125 Inverse Sum
ユーザー lloyz
提出日時 2022-11-18 23:51:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 92 ms / 2,000 ms
コード長 802 bytes
コンパイル時間 344 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 83,068 KB
最終ジャッジ日時 2024-09-20 04:16:09
合計ジャッジ時間 2,748 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import gcd

p, q = map(int, input().split())

pq_gcd = gcd(p, q)
p //= pq_gcd
q //= pq_gcd

PrimeCnt = []
qc = q
c = 0
while qc % 2 == 0:
    c += 1
    qc //= 2
if c > 0:
    PrimeCnt.append((2, 2 * c))
f = 3
while f * f <= qc:
    c = 0
    while qc % f == 0:
        c += 1
        qc //= f
    if c > 0:
        PrimeCnt.append((f, 2 * c))
    f += 2
if qc != 1:
    PrimeCnt.append((qc, 2))
Divisors = [1]
for pp, c in PrimeCnt:
    qq = 1
    n = len(Divisors)
    for _ in range(c):
        qq *= pp
        for i in range(n):
            Divisors.append(Divisors[i] * qq)
Divisors.sort()

ANS = []
for q_ in Divisors:
    if (q_ + q) % p == 0 and (q * q // q_ + q) % p == 0:
        ANS.append(((q_ + q) // p, (q * q // q_ + q) // p))
print(len(ANS))
for n, m in ANS:
    print(n, m)
0