DOJ logoDOJ
Problems
Recently added problemsSaved problemsWorkbooks
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord account linking.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

An Extremely Difficult Problem

Back to problem

Editorial

Small inputs

Directly evaluate and add all terms in O(N)O(N)O(N) time and O(1)O(1)O(1) extra space. This solves the subtask with N≤106N\leq10^6N≤106.

Precompute every ten million terms

Set B=107B=10^7B=107. Before submitting, compute

Pq=∑i=1qBisin⁡i(0≤q≤1000).P_q=\sum_{i=1}^{qB}i\sin\sqrt{i}\quad(0\leq q\leq1000).Pq​=i=1∑qB​

Here P0=0P_0=0P0​=0. An offline program iterates from 111 to 101010^{10}1010 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 NNN, let q=⌊N/B⌋q=\lfloor N/B\rfloorq=⌊N/B⌋. Then

SN=Pq+∑i=qB+1Nisin⁡i.S_N=P_q+\sum_{i=qB+1}^{N}i\sin\sqrt{i}.SN​=Pq​+

The stored prefix covers exactly the indices from 111 to qBqBqB, and the remaining loop covers qB+1qB+1qB+1 through NNN. These disjoint intervals cover every required term, proving the formula. If NNN is a multiple of , the loop is empty. At most terms remain.

Offline preprocessing takes O(1010)O(10^{10})O(1010) time and O(1010/B)O(10^{10}/B)O(1010/B) space. The submitted program, including its table, uses O(B)O(B) 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 xxx with sum sss and compensation ccc, perform y=x−cy=x-cy=x−c, t=s+yt=s+y, , 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 ∣A−SN∣/max⁡(1,∣SN∣)|A-S_N|/\max(1,|S_N|)∣A−SN​∣/max(1,∣SN​. 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

i
sin
i​
(
0
≤
q≤
1000).
BB
B
i=qB+1
∑
N
​
i
sin
i​
.
BB
B
B−1=9 999 999B-1=9\,999\,999B−1=9999999
O(B)
O(1010/B)O(10^{10}/B)O(1010/B)
t
=
s+
y
c=(t−s)−yc=(t-s)-yc=(t−s)−y
s=ts=ts=t
∣
)
9.114⋅10−79.114\cdot10^{-7}9.114⋅10−7
10−410^{-4}10−4