/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 167ms 33.41 MiB
#2 Accepted 226ms 35.074 MiB
#3 Accepted 193ms 35.324 MiB
#4 Accepted 203ms 35.242 MiB
#5 Accepted 204ms 35.285 MiB

Code

import sys

def solve():
    """
    This function precomputes all answers and then handles the test cases.
    """
    # --- Precomputation Phase ---
    # This part runs only once to prepare answers for all possible N up to 10^6.
    
    MAX_N = 10**6 + 1

    # Step 1: Calculate tau(k) for all k up to MAX_N using a sieve.
    # tau[k] will store the number of divisors of k.
    # Complexity: O(MAX_N * log(MAX_N))
    tau = [0] * MAX_N
    for i in range(1, MAX_N):
        for multiple in range(i, MAX_N, i):
            tau[multiple] += 1

    # Step 2: Compute the prefix sums to get the answer for each N.
    # answers[n] will store the total number of harmonious pairs for an army of size n.
    # Complexity: O(MAX_N)
    answers = [0] * MAX_N
    # ans[0] and ans[1] are 0, as no pairs (i, j) with i < j exist.
    for n in range(2, MAX_N):
        # The number of new harmonious pairs where the larger number is n
        # is equal to the number of proper divisors of n, which is tau[n] - 1.
        new_pairs_at_n = tau[n] - 1
        answers[n] = answers[n-1] + new_pairs_at_n
        
    # --- Query Phase ---
    # Process each test case using the precomputed answers.
    
    try:
        # Read the number of test cases.
        num_test_cases = int(sys.stdin.readline())
        
        # Process each test case.
        for _ in range(num_test_cases):
            # Read the value of N for the current test case.
            n = int(sys.stdin.readline())
            
            # The answer is precomputed, so it's a direct O(1) lookup.
            sys.stdout.write(str(answers[n]) + '\n')
            
    except (IOError, ValueError):
        # Gracefully handle potential empty input or parsing errors.
        return

# Call the main function to run the solution.
if __name__ == "__main__":
    solve()

Information

Submit By
Type
Submission
Problem
P1206 D1. GCD equal Absolute Value (Easy Version)
Language
PyPy 3 (Python 3.9.18 PyPy 7.3.15)
Submit At
2025-07-14 18:04:37
Judged At
2025-07-14 18:04:37
Judged By
Score
100
Total Time
226ms
Peak Memory
35.324 MiB