Eating probabilities and prefix sums
Let indicate whether snack is eaten, and let be its eating probability. By linearity of expectation, the answer for is
The eating events need not be independent. Every question refers to the same entire process, so compute the values only once from the beginning.
DP on the fullness distribution
Let be the probability that fullness is immediately before the current snack. Initially and all other entries are zero. For snack , define
Then
Initialize a fresh distribution to zero and, for every , add
Replace by . Do not update in place: that would reuse an outcome of the current snack as an input to the same step.
Store and . 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 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 is the correct next distribution, and the law of total probability gives the correct . 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 time and space including prefix sums. Read and answer questions one by one without storing them.
Small inputs and numerical handling
Subtask 1 has . Enumerate every sequence of eating/skipping decisions. With , 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 and any prefix expectation numerator is at most , 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 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 for contestant value and reference value . This is equivalent to the specified absolute-or-relative error criterion.
Independent verification
A separate implementation updates the cumulative distribution instead of state probabilities. For ,
Also . The usual change of minus one is blocked only when skipping at fullness zero, so
Compute the mean by . 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