/ SeriousOJ /

Record Detail

Runtime Error


  
# Status Time Cost Memory Cost
#1 Runtime Error Traceback (most recent call last): File "foo.py", line 64, in <module> File "foo.py", line 59, in main File "foo.py", line 38, in precompute IndexError: list index out of range 870ms 152.727 MiB
#2 Runtime Error Traceback (most recent call last): File "foo.py", line 64, in <module> File "foo.py", line 59, in main File "foo.py", line 38, in precompute IndexError: list index out of range 865ms 152.77 MiB

Code

MAXN = 10**6
primes = []
spf = [0] * (MAXN + 1)
pfstore = [[] for _ in range(MAXN + 1)]
res = [0] * (MAXN + 1)

# Precompute smallest prime factors (SPF) using sieve of Eratosthenes
def precompute():
    global primes, spf, pfstore, res
    
    for i in range(2, MAXN + 1):
        if spf[i] == 0:
            spf[i] = i
            primes.append(i)
        for p in primes:
            if i * p > MAXN:
                break
            spf[i * p] = p
            if i % p == 0:
                break
    
    # Precompute the prime factorizations
    for i in range(2, MAXN + 1):
        x = i
        while x > 1:
            p = spf[x]
            pfstore[i].append(p)
            x //= p
    
    # Precompute divisor counts
    maxDiv = 0
    best = -1
    for i in range(1, MAXN + 1):
        pfacs = {}
        # Factorize i and i+1, and count the prime factors
        for p in pfstore[i]:
            pfacs[p] = pfacs.get(p, 0) + 1
        for p in pfstore[i + 1]:
            pfacs[p] = pfacs.get(p, 0) + 1
        pfacs[2] -= 1
        
        # Calculate divisor count from prime factorization
        div = 1
        for e in pfacs.values():
            div *= (e + 1)
        
        if div >= maxDiv:
            maxDiv = div
            best = i
        res[i] = maxDiv

# Function to run test cases
def run_case():
    n = int(input())
    print(res[n])

# Main function to handle input/output
def main():
    precompute()
    T = int(input())
    for _ in range(T):
        run_case()

main()

Information

Submit By
Type
Submission
Problem
P1180 Maximum Divisor
Language
PyPy 3 (Python 3.9.18 PyPy 7.3.15)
Submit At
2025-04-06 18:22:46
Judged At
2025-04-06 18:22:46
Judged By
Score
0
Total Time
870ms
Peak Memory
152.77 MiB