結果
問題 |
No.3072 Speedrun Query
|
ユーザー |
![]() |
提出日時 | 2025-05-14 12:55:54 |
言語 | PyPy3 (7.3.15) |
結果 |
RE
|
実行時間 | - |
コード長 | 3,059 bytes |
コンパイル時間 | 386 ms |
コンパイル使用メモリ | 82,664 KB |
実行使用メモリ | 80,876 KB |
最終ジャッジ日時 | 2025-05-14 12:57:42 |
合計ジャッジ時間 | 7,377 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | RE * 1 |
other | RE * 21 |
ソースコード
# -*- coding: utf-8 -*- import sys import decimal # Set the precision for decimal module calculations. # The problem requires a very high precision (absolute or relative error <= 10^-15). # Standard floats (double precision) might not be sufficient, especially for # square roots of large numbers close to perfect squares, or when summing many numbers. # The maximum possible sum can be around N * sqrt(10^18) = 10^6 * 10^9 = 10^15. # To maintain precision when adding potentially small sqrt values to a large running sum, # and to ensure the final result meets the 1e-15 tolerance, we need sufficient # working precision. A precision of 40 decimal digits provides a good safety margin. decimal.getcontext().prec = 40 # Read the number of elements in the sequence N = int(sys.stdin.readline()) # Initialize the running sum as a Decimal object with value 0 current_sum = decimal.Decimal(0) # Use optimized I/O functions for potentially large input/output # sys.stdin.readline is generally faster than input() # sys.stdout.write is generally faster than print() readline = sys.stdin.readline write = sys.stdout.write # Process each number in the sequence for _ in range(N): # Read the next number as a string and remove leading/trailing whitespace x_str = readline().strip() # Convert the string representation to a Decimal object. # We assume the input consists of valid non-negative integers as per constraints. x_dec = decimal.Decimal(x_str) # Calculate the square root only if the number is positive. # The square root of 0 is 0, and adding 0 does not change the sum. # The problem constraints state x_i >= 0. if x_dec > 0: # Compute the square root using Decimal's arbitrary precision sqrt method. # The calculation uses the precision set in the context (40 digits). sqrt_x = x_dec.sqrt() # Add the computed square root to the running sum. # Decimal addition maintains the precision. current_sum += sqrt_x # Format the current cumulative sum for output. # The problem requires high precision, but also has an output size limit (~20MB). # Printing with a fixed high number of decimal places (like %.17f) might exceed the size limit. # The format specifier "{:.17g}" prints the number using up to 17 significant digits. # It uses fixed-point notation for numbers where it's suitable, and scientific # notation for very large or very small numbers, potentially reducing output length. # This format is chosen as a balance between satisfying the 1e-15 precision requirement # (often met via the relative error condition for large results) and managing output size. output_str = f"{current_sum:.17g}" # Write the formatted result to standard output, followed by a newline character. write(output_str + '\n') # Flushing stdout might be necessary in some environments, but typically not required # in competitive programming platforms as the buffer flushes upon program termination # or when it fills up. # sys.stdout.flush()