結果

問題 No.2125 Inverse Sum
ユーザー FromBooskaFromBooska
提出日時 2023-04-17 22:27:08
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,173 bytes
コンパイル時間 238 ms
コンパイル使用メモリ 82,416 KB
実行使用メモリ 82,932 KB
最終ジャッジ日時 2024-04-21 00:17:42
合計ジャッジ時間 3,067 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
52,608 KB
testcase_01 AC 47 ms
51,968 KB
testcase_02 AC 41 ms
52,480 KB
testcase_03 AC 42 ms
52,736 KB
testcase_04 AC 41 ms
52,608 KB
testcase_05 WA -
testcase_06 AC 41 ms
53,248 KB
testcase_07 AC 62 ms
64,768 KB
testcase_08 AC 42 ms
52,992 KB
testcase_09 AC 41 ms
52,736 KB
testcase_10 AC 41 ms
52,480 KB
testcase_11 AC 41 ms
52,224 KB
testcase_12 AC 41 ms
52,224 KB
testcase_13 AC 43 ms
52,608 KB
testcase_14 WA -
testcase_15 AC 43 ms
53,248 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 41 ms
52,864 KB
testcase_19 AC 44 ms
53,248 KB
testcase_20 AC 46 ms
60,252 KB
testcase_21 AC 41 ms
52,608 KB
testcase_22 AC 47 ms
53,888 KB
testcase_23 AC 44 ms
52,992 KB
testcase_24 AC 61 ms
64,384 KB
testcase_25 AC 57 ms
64,384 KB
testcase_26 AC 42 ms
52,736 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 41 ms
52,608 KB
testcase_31 WA -
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

# 変形すれば
# Q**2 = (NP-Q)*(MP-Q)
# sqrt(Q)までのQの約数からQ**2の約数を求める


# ポラード・ロー素因数分解法
# https://qiita.com/t_fuki/items/7cd50de54d3c5d063b4a#%E3%83%9D%E3%83%A9%E3%83%BC%E3%83%89%E3%83%AD%E3%83%BC%E7%B4%A0%E5%9B%A0%E6%95%B0%E5%88%86%E8%A7%A3%E6%B3%95%E3%81%AE%E3%82%A2%E3%83%AB%E3%82%B4%E3%83%AA%E3%82%BA%E3%83%A0

def gcd(a, b):
    while a:
        a, b = b%a, a
    return b

def is_prime(n):
    if n == 2:
        return 1
    if n == 1 or n%2 == 0:
        return 0

    m = n - 1
    lsb = m & -m
    s = lsb.bit_length()-1
    d = m // lsb

    test_numbers = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]

    for a in test_numbers:
        if a == n:
            continue
        x = pow(a,d,n)
        r = 0
        if x == 1:
            continue
        while x != m:
            x = pow(x,2,n)
            r += 1
            if x == 1 or r == s:
                return 0
    return 1


def find_prime_factor(n):
    if n%2 == 0:
        return 2

    m = int(n**0.125)+1

    for c in range(1,n):
        f = lambda a: (pow(a,2,n)+c)%n
        y = 0
        g = q = r = 1
        k = 0
        while g == 1:
            x = y
            while k < 3*r//4:
                y = f(y)
                k += 1
            while k < r and g == 1:
                ys = y
                for _ in range(min(m, r-k)):
                    y = f(y)
                    q = q*abs(x-y)%n
                g = gcd(q,n)
                k += m
            k = r
            r *= 2
        if g == n:
            g = 1
            y = ys
            while g == 1:
                y = f(y)
                g = gcd(abs(x-y),n)
        if g == n:
            continue
        if is_prime(g):
            return g
        elif is_prime(n//g):
            return n//g
        else:
            return find_prime_factor(g)


def factorize(n):
    res = {}
    while not is_prime(n) and n > 1:  # nが合成数である間nの素因数の探索を繰り返す
        p = find_prime_factor(n)
        s = 0
        while n%p == 0:  # nが素因数pで割れる間割り続け、出力に追加
            n //= p
            s += 1
        res[p] = s
    if n > 1:  # n>1であればnは素数なので出力に追加
        res[n] = 1
    return res

# 高速約数列挙
def divisors(num):
    factors = factorize(num)
    divs = [1]
    for p in factors:
        e = factors[p]
        if e > 0:
            k = len(divs) #それまでの素因数積、つまり約数、の数
            for i in range(e*k):
                divs.append(divs[-k]*p) 
                #なぜans[-k]なのか、どんどんappendするので[-k]で前の約数にかけていく
    return divs

# 高速約数カウント
def divisor_count(num):
    factors = factorize(num)
    count = 1
    for p in factors:
        e = factors[p]
        count *= (e+1)
    return count

P, Q = map(int, input().split())
divs = divisors(Q**2)
ans = []
for d1 in divs:
    d2 = (Q**2)//d1
    N = (d1+Q)//P
    M = (d2+Q)//P
    if N != 0 and M != 0 and (N*P-Q)*(M*P-Q) == Q**2:
        ans.append((N, M))
print(len(ans))
for N, M in ans:
    print(N, M)
0