結果

問題 No.2125 Inverse Sum
ユーザー 👑 rin204rin204
提出日時 2022-11-19 00:09:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 101 ms / 2,000 ms
コード長 1,988 bytes
コンパイル時間 1,537 ms
コンパイル使用メモリ 81,452 KB
実行使用メモリ 82,160 KB
最終ジャッジ日時 2023-10-20 08:59:46
合計ジャッジ時間 3,292 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,552 KB
testcase_01 AC 36 ms
53,552 KB
testcase_02 AC 37 ms
53,552 KB
testcase_03 AC 36 ms
53,552 KB
testcase_04 AC 36 ms
53,552 KB
testcase_05 AC 45 ms
55,600 KB
testcase_06 AC 40 ms
53,552 KB
testcase_07 AC 57 ms
67,876 KB
testcase_08 AC 45 ms
61,732 KB
testcase_09 AC 36 ms
53,556 KB
testcase_10 AC 37 ms
53,556 KB
testcase_11 AC 36 ms
53,556 KB
testcase_12 AC 38 ms
53,556 KB
testcase_13 AC 37 ms
53,556 KB
testcase_14 AC 42 ms
59,844 KB
testcase_15 AC 37 ms
53,556 KB
testcase_16 AC 42 ms
59,840 KB
testcase_17 AC 37 ms
53,556 KB
testcase_18 AC 37 ms
53,556 KB
testcase_19 AC 37 ms
53,556 KB
testcase_20 AC 41 ms
59,840 KB
testcase_21 AC 37 ms
53,556 KB
testcase_22 AC 37 ms
53,556 KB
testcase_23 AC 38 ms
53,556 KB
testcase_24 AC 54 ms
67,880 KB
testcase_25 AC 56 ms
67,880 KB
testcase_26 AC 38 ms
53,556 KB
testcase_27 AC 101 ms
82,160 KB
testcase_28 AC 91 ms
80,656 KB
testcase_29 AC 76 ms
76,884 KB
testcase_30 AC 36 ms
53,556 KB
testcase_31 AC 89 ms
80,628 KB
testcase_32 AC 49 ms
63,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import gcd

def isprime(n):
    if n <= 1:
        return False
    elif n == 2:
        return True
    elif n % 2 == 0:
        return False
    
    A = [2, 325, 9375, 28178, 450775, 9780504, 1795265022]
    s = 0
    d = n - 1
    while d % 2 == 0:
        s += 1
        d >>= 1
    
    for a in A:
        if a % n == 0:
            return True
        x = pow(a, d, n)
        if x != 1:
            for t in range(s):
                if x == n - 1:
                    break
                x = x * x % n
            else:
                return False
    return True
        
def pollard(n):
    if n % 2 == 0:
        return 2
    if isprime(n):
        return n
    
    f = lambda x:(x * x + 1) % n
    
    step = 0
    while 1:
        step += 1
        x = step
        y = f(x)
        while 1:
            p = gcd(y - x + n, n)
            if p == 0 or p == n:
                break
            if p != 1:
                return p
            x = f(x)
            y = f(f(y))

def primefact(n):
    if n == 1:
        return []
    p = pollard(n)
    if p == n:
        return [p]
    left = primefact(p)
    right = primefact(n // p)
    left += right
    return sorted(left)

def divisor_lst(n):
    if n == 1:
        return [1]
    primes = primefact(n)
    primes.append(primes[-1] + 1)
    bef = primes[0]
    cnt = 0
    ret = [1]
    for p in primes:
        if p == bef:
            cnt += 1
        else:
            times = bef
            le = len(ret)
            for _ in range(cnt):
                for i in range(le):
                    ret.append(ret[i] * times)
                times *= bef
            bef = p
            cnt = 1
    ret.sort()
    return ret

p, q = map(int, input().split())
g = gcd(p, q)
p //= g
q //= g
divs = divisor_lst(q * q)
ans = []
for i in divs:
    n = i + q
    m = q * q // i + q
    if n % p == 0 and m % p == 0:
        ans.append((n // p, m // p))
ans.sort()
print(len(ans))
for row in ans:
    print(*row)
0