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-backed certification.

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

Snack Party

Back to problem

Editorial

Eating probabilities and prefix sums

Let XiX_iXi​ indicate whether snack iii is eaten, and let eie_iei​ be its eating probability. By linearity of expectation, the answer for [l,r][l,r][l,r] is

E[∑i=lrXi]=∑i=lrE[Xi]=∑i=lrei.\mathbb{E}\left[\sum_{i=l}^{r}X_i\right]=\sum_{i=l}^{r}\mathbb{E}[X_i]=\sum_{i=l}^{r}e_i.E[i=l∑r​X

The eating events need not be independent. Every question refers to the same entire process, so compute the eie_iei​ values only once from the beginning.

DP on the fullness distribution

Let dp[s]dp[s]dp[s] be the probability that fullness is sss immediately before the current snack. Initially dp[0]=1dp[0]=1dp[0]=1 and all other entries are zero. For snack iii, define

qs=max⁡(0,pi−0.05s).q_s=\max(0,p_i-0.05s).qs​=max(0,pi​−0.05s).

Then

ei=∑s=020dp[s]qs.e_i=\sum_{s=0}^{20}dp[s]q_s.ei​=s=0∑20​dp[s]

Initialize a fresh distribution ndpndpndp to zero and, for every sss, add

ndp[min⁡(20,s+1)]+=dp[s]qs,ndp[\min(20,s+1)]\mathrel{+}=dp[s]q_s,ndp[min(20,s+1)]+=dp[s]qs​, ndp[max⁡(0,s−1)]+=dp[s](1−qs).ndp[\max(0,s-1)]\mathrel{+}=dp[s](1-q_s).ndp[max(0,s−1)]+=dp[s](1−q

Replace dpdpdp by ndpndpndp. Do not update in place: that would reuse an outcome of the current snack as an input to the same step.

Store F0=0F_0=0F0​=0 and Fi=Fi−1+eiF_i=F_{i-1}+e_iFi​=F. Each answer is . Do not reset the distribution at the left endpoint of a question. Keeping only the mean fullness is also insufficient in general: truncation of eating probabilities at zero and the boundary at fullness zero make future behavior depend on the distribution, not just its mean.

Correctness and complexity

Assume dpdpdp is the correct current distribution. The two transitions multiply each state probability by the given conditional probabilities and partition all outcomes into eating and not eating. They send each outcome to precisely its next fullness. Thus ndpndpndp is the correct next distribution, and the law of total probability gives the correct eie_iei​. The initial distribution is correct, so induction proves all computed probabilities correct. Linearity of expectation and prefix sums then give every requested answer.

There are 21 fullness states. The complexity is O(21N+Q)O(21N+Q)O(21N+Q) time and O(N)O(N)O(N) space including prefix sums. Read and answer questions one by one without storing them.

Small inputs and numerical handling

Subtask 1 has N≤8N\leq8N≤8. Enumerate every sequence of eating/skipping decisions. With ci=100pic_i=100p_ici​=100pi​, the eating numerator at a step is , and the skipping numerator is its complement to 100. All outcomes have common denominator . Multiply their step numerators and add each leaf weight to the numerators for the snacks eaten in that outcome. This takes time and space.

Here the denominator is at most 101610^{16}1016 and any prefix expectation numerator is at most 8⋅10168\cdot10^{16}8⋅1016, so signed 64-bit arithmetic is exact. For the full constraints, do not construct the enormous exact denominator. Use sufficiently precise floating-point values for the distribution and prefix sums. The main solution parses pip_ip as integer , computes in long double, and prints 15 digits after the decimal point. Every expected count lies between zero and the interval length.

The checker requires finite numbers and tests ∣a−b∣≤10−6max⁡(1,∣b∣)|a-b|\leq10^{-6}\max(1,|b|)∣a−b∣≤10−6max(1,∣b∣) for contestant value aaa and reference value . This is equivalent to the specified absolute-or-relative error criterion.

Independent verification

A separate implementation updates the cumulative distribution Cj=P(S≤j)C_j=P(S\leq j)Cj​=P(S≤j) instead of state probabilities. For j<20j<20j<20,

Cj′=Cj−P(S=j)qj+P(S=j+1)(1−qj+1),C20′=1.C'_j=C_j-P(S=j)q_j+P(S=j+1)(1-q_{j+1}),\qquad C'_{20}=1.Cj′​=C

Also q20=0q_{20}=0q20​=0. The usual change of minus one is blocked only when skipping at fullness zero, so

ei=E[S′]−E[S]+1−P(S=0)(1−pi)2.e_i=\frac{\mathbb{E}[S']-\mathbb{E}[S]+1-P(S=0)(1-p_i)}{2}.ei​=2E[S

Compute the mean by E[S]=∑j=019(1−Cj)\mathbb{E}[S]=\sum_{j=0}^{19}(1-C_j)E[S]=∑j=019​(1−C. This cumulative-distribution and mean-drift implementation was compared on all data, while small inputs were also checked by exact integer-numerator enumeration.

Solution written by GPT6

i
​
]
=
i=l∑r​E[Xi​]=
i=l∑r​ei​.
qs​
.
s
​
)
.
i−1​
+
ei​
Fr−Fl−1F_r-F_{l-1}Fr​−Fl−1​
max⁡(0,ci−5s)\max(0,c_i-5s)
max(0,ci​−5s)
100N100^N100N
O(N2N+Q)O(N2^N+Q)O(N2N+Q)
O(N)O(N)O(N)
i​
cic_ici​
max⁡(0,ci−5s)/100\max(0,c_i-5s)/100max(0,ci​−5s)/100
bbb
j
​
−
P(S=
j)qj​+
P(S=
j+
1)(1−
qj+1​),C20′​=
1.
′
]
−
E
[
S
]
+
1
−
P
(
S
=
0
)
(
1
−
pi​
)
​
.
j
​
)