結果

問題 No.3331 Consecutive Cubic Sum
コンテスト
ユーザー himueu
提出日時 2026-07-02 00:38:46
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 53 ms / 5,000 ms
コード長 3,367 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,836 ms
コンパイル使用メモリ 84,736 KB
実行使用メモリ 75,136 KB
最終ジャッジ日時 2026-07-02 00:38:54
合計ジャッジ時間 4,859 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 47
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

def miller_rabin(n, bases):
    m = n - 1
    e = (m & -m).bit_length() - 1
    d = n >> e
    for b in bases:
        x = pow(b, d, n)
        if x == 1:
            continue
        for _ in range(e):
            y = pow(x, 2, n)
            if y == 1:
                if x == n - 1:
                    break
                else:
                    return False
            x = y
        else:
            return False
    return True

def is_prime(n):
    if n < 2:
        return False
    if n == 2:
        return True
    if n % 2 == 0:
        return False

    if n < 2047:
        return miller_rabin(n, [2])
    if n < 9080191:
        return miller_rabin(n, [31, 73])
    if n < 4759123141:
        return miller_rabin(n, [2, 7, 61])
    if n < 1122004669633:
        return miller_rabin(n, [2, 13, 23, 1662803])
    if n < 3770579582154547:
        return miller_rabin(n, [2, 880937, 2570940, 610386380, 4130785767])
    if n < 18446744073709551617:
        return miller_rabin(n, [2, 325, 9375, 28178, 450775, 9780504, 1795265022])
    if n < 318665857834031151167461:
        return miller_rabin(n, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37])
    if n < 3317044064679887385961981:
        return miller_rabin(n, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41])
    return miller_rabin(n, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47])

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

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

    m = int(n ** 0.125) + 1

    for c in range(1, n):
        def f(a):
            return (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 prime_factorize(n):
    res = []
    while not is_prime(n) and n > 1:
        p = find_prime_factor(n)
        s = 0
        while n % p == 0:
            n //= p
            s += 1
        res.append((p, s))
    if n > 1:
        res.append((n, 1))
    return res

def divisors(n):
    div = [1]
    for k, v in prime_factorize(n):
        div = [d * k**e for d in div for e in range(v+1)]
    div.sort()
    return div

n = int(input())

ans = []
div = divisors(n*2)
ld = (len(div) + 1) // 2
def f(a):
    return a**2 * (a + 1)**2
def pred(l, w):
    return f(l + w) - f(l) <= n * 4
ng = int(n ** 0.34) + 1
for w in div:
    if w ** 4 > n * 4:
        break
    ok = 0
    while ng - ok > 1:
        mid = (ok + ng) // 2
        if pred(mid, w):
            ok = mid
        else:
            ng = mid
    l, r = ok, ok + w
    if f(r) - f(l) == n * 4:
        ans.append((l+1, r))

ans.sort()
print(len(ans))
if ans:
    print("\n".join(f"{l} {r}" for l, r in ans))
0