DengklekGaneshAndDetention
TCO17 Round 2B · 2017-03-31 · by fushar
Problem Statement
There are N classrooms at the school, conveniently numbered 0 through N − 1. Each classroom has a lamp. The initial state of the lamps is given by an
Dengklek is told to visit the classrooms in order, from classroom 0 to N − 1. In each classroom i:
- If the lamp in the current classroom is off, Dengklek must do nothing.
- Otherwise, Dengklek must flip the coin in the current classroom i:
- If it lands heads up, Dengklek must call Ganesh and ask him to toggle all lamps in classrooms 0 through i − 1, inclusive.
- If it lands tails up, Dengklek must call Ganesh and ask him to toggle all lamps in classrooms i + 1 through N − 1, inclusive.
Every time Ganesh toggles a lamp from off to on, Ganesh is told to shout, "We will not come late for school again!".
The principal is now curious about the expected number of times Ganesh will shout. Return this expected number.
Due to a technical restriction, the
let val = array of N integers
val[0] = valInit
for i in 1 .. N-1:
val[i] = (val[i-1] * valMul + valAdd) mod valMod
for i in 0 .. N-1:
lamps[i] = val[i] mod 2
probs[i] = val[i] mod 101
Notes
- The returned value must have an absolute or relative error less than 10^-6.
- The author's solution does not depend on any properties of the pseudorandom number generator.
Constraints
- N will be between 1 and 1,000,000, inclusive.
- valMod will be between 1 and 1,000,000,000, inclusive.
- valInit will be between 0 and valMod − 1, inclusive.
- valMul will be between 0 and valMod − 1, inclusive.
- valAdd will be between 0 and valMod − 1, inclusive.
3 0 1 25 100 Returns: 1.375
Using the above pseudorandom number generation, we have: lamps = {0, 1, 0} probs = {0, 25, 50} The following events will happen: Dengklek visits classroom 0. The lamp is off, so Dengklek does nothing. Dengklek visits classroom 1. The lamp is on, so Dengklek flips the coin: With probability 25%, the coin lands heads, and Ganesh first toggles the lamp in classroom 0 and shouts once. Dengklek visits classroom 2. The lamp is off, so Dengklek does nothing. With probability 75%, the coin lands tails, and Ganesh toggles the lamp in classroom 2, then shouts once. Dengklek visits classroom 2. The lamp is on, so Dengklek flips the coin: With probability 50%, the coin lands heads, and Ganesh toggles the lamps in classrooms 0 and 1, then shouts once. With probability 50%, the coin lands tails. Therefore, the expected number of shouts is 25% × 1 + 75% × 50% × 2 + 75% × 50% × 1 = 1.375.
3 20 2 10 57 Returns: 1.06
This time, we have: lamps = {0, 0, 1} probs = {20, 50, 53} Nothing will happen in classrooms 0 and 1. In classroom 2, with probability 53% Ganesh toggles the lamps in classrooms 0 and 1. While he does so, he shouts twice. Therefore, the expected number of shouts is 53% × 2 = 1.06.
8 10 3 10 29 Returns: 7.8002283556316
1 0 0 0 1 Returns: 0.0
1000000 0 0 0 1 Returns: 0.0
Submissions are judged against all 43 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class DengklekGaneshAndDetention with a public method double getExpected(int N, int valInit, int valMul, int valAdd, int valMod) · 43 test cases · 2 s / 256 MB per case