結果

問題 No.3331 Consecutive Cubic Sum
コンテスト
ユーザー Aralov Otabek
提出日時 2025-12-11 12:27:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 127 ms / 5,000 ms
コード長 2,670 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 307 ms
コンパイル使用メモリ 82,604 KB
実行使用メモリ 77,760 KB
最終ジャッジ日時 2025-12-11 12:27:10
合計ジャッジ時間 5,227 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 47
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys
import random
sys.setrecursionlimit(10**7)

N = int(input())

def mul(a,b,m): 
    return (a*b) % m

def f(x,c,n):
    return (mul(x,x,n) + c) % n

def rho(n):
    if n % 2 == 0:
        return 2
    if n % 3 == 0:
        return 3
    while True:
        x = random.randrange(2, n-1)
        y = x
        c = random.randrange(1, n-1)
        d = 1
        while d == 1:
            x = f(x,c,n)
            y = f(f(y,c,n),c,n)
            d = gcd(abs(x-y), n)
            if d == n:
                break
        if d > 1 and d < n:
            return d

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

def factor(n, res):
    if n == 1:
        return
    if is_prime(n):
        res.append(n)
    else:
        d = rho(n)
        factor(d,res)
        factor(n//d,res)

def is_prime(n):
    if n < 2:
        return False
    small_primes = [2,3,5,7,11,13,17,19,23]
    for p in small_primes:
        if n == p:
            return True
        if n % p == 0:
            return n == p
    return miller_rabin(n)

def miller_rabin(n):
    if n < 2:
        return False
    d = n-1
    s = 0
    while d % 2 == 0:
        d//=2; s+=1
    for a in [2,325,9375,28178,450775,9780504,1795265022]:
        if a % n == 0:
            continue
        x = pow(a,d,n)
        if x==1 or x==n-1:
            continue
        for _ in range(s-1):
            x = mul(x,x,n)
            if x==n-1:
                break
        else:
            return False
    return True

# T_n = n(n+1)/2
def solve_T(T):
    # n(n+1)//2 = T  ⇒  n^2+n-2T=0
    # n = floor((-1 + sqrt(1+8T))/2)
    import math
    D = 1 + 8*T
    r = int(math.isqrt(D))
    if r*r != D:
        return None
    n = (-1 + r)//2
    if n*(n+1)//2 == T:
        return n
    return None

# N ni faktorizatsiya
fs = []
factor(N, fs)

# divisorlar
from collections import Counter

cnt = Counter(fs)
divs = [1]
for p,e in cnt.items():
    base = []
    cur = 1
    for _ in range(e):
        cur *= p
        base.append(cur)
    new = []
    for d in divs:
        for v in base:
            new.append(d*v)
    divs += new

divs = list(set(divs))
divs.sort()

ans = []

# barcha a*b=N juftliklari
for a in divs:
    b = N // a
    # a ≤ b bo‘lishi kerak (aks holda takrorlanadi)
    if a > b:
        continue
    if (a + b) % 2 != 0:
        continue
    if (b - a) % 2 != 0:
        continue

    T_R = (a + b) // 2
    T_Lm1 = (b - a) // 2

    R = solve_T(T_R)
    if R is None:
        continue
    Lm1 = solve_T(T_Lm1)
    if Lm1 is None:
        continue

    L = Lm1 + 1
    if L <= R:
        ans.append((L, R))

ans.sort()

print(len(ans))
for L,R in ans:
    print(L, R)
0