Small inputs
Directly evaluate and add all terms in time and extra space. This solves the subtask with .
Precompute every ten million terms
Set . Before submitting, compute
Here . An offline program iterates from to and records the prefix sum at every multiple of . Embed the resulting 1,001 constants into the submitted source. Running this entire preprocessing during judging would exceed the time limit.
For the input , let . Then
The stored prefix covers exactly the indices from to , and the remaining loop covers through . These disjoint intervals cover every required term, proving the formula. If is a multiple of , the loop is empty. At most terms remain.
Offline preprocessing takes time and space. The submitted program, including its table, uses time and space. Use 64-bit integers for the input and loop indices.
Floating-point accuracy
Use long double, sqrtl, sinl, and compensated summation in both preprocessing and the submitted program. To add a term with sum and compensation , perform , , , and , in that order. Preprocessing first sums each block and then combines the block sums with compensation. Store constants with sufficient significant digits and an L suffix, and print sufficient decimal places. Do not use fast-math options that reassociate floating-point expressions.
For large inputs, square-root errors propagate into the sine evaluation. Converting a table generated in double to long double cannot recover its lost accuracy; preserve precision from the table-generation stage. Measure the normalized error as . Including inputs with small sums, the largest observed value was approximately . The tolerance is about 110 times that value. An answer passes if either its absolute or its relative error is strictly below this tolerance. Preserve accuracy even when cancellation makes the sum small.
Solution written by GPT6