Social feed~35 min2s · 256 MBtarget O(rounds × streak) time, O(streak) spacedp
A social app has a daily quiz game. Each round a player answers one question. A correct answer scores 1 point and a wrong answer loses 1 point. If a player answers streak questions correctly in a row, they score a bonus, and the count of correct answers in a row starts again from 0.
The game team wants to know the expected score of a player who is correct with a fixed chance in every round. The answer is a fraction, so it is reported modulo the prime 998,244,353.
Write expected_score(rounds, win_percent, streak, bonus).
rounds rounds. Each round is won with probability win_percent / 100 and lost otherwise, independently of the other rounds.streak, add bonus points as well, and set the run to 0.a / b. Return a × b^-1 modulo 998,244,353.Input: rounds = 3, win_percent = 50, streak = 2, bonus = 5
Output: 124780546The table follows the chance of each current run length.
| Round | Chance of streak 0 | Chance of streak 1 | Expected gain this round | Expected score so far |
|---|---|---|---|---|
| 1 | 1 | 0 | 0 | 0 |
| 2 | 1/2 | 1/2 | 5/4 | 5/4 |
| 3 | 3/4 | 1/4 | 5/8 | 15/8 |
The table gives, for each round, the chance of each current run length and the expected gain of the round, as exact fractions. The expected score is the sum of the gains, and the output is that fraction written modulo the prime.
Input: rounds = 1, win_percent = 100, streak = 1, bonus = 10
Output: 11One round that is always won scores 1 point, and completes the streak of length 1, so the bonus of 10 is added too: 11.
Input: rounds = 4, win_percent = 0, streak = 3, bonus = 7
Output: 998244349Every round is lost, so the score is -4 for sure. The output is -4 modulo the prime, which is 998,244,349.
1 ≤ rounds ≤ 20,0000 ≤ win_percent ≤ 1001 ≤ streak ≤ 200 ≤ bonus ≤ 1,000The result is a whole number from 0 to 998,244,352.
Listing every sequence of wins and losses costs about 2^n steps, which is far too much for 20,000 rounds. The bonus depends on the length of the current run, but the run has only streak possible lengths. A table of the chance of each length, moved forward one round at a time, costs about rounds × streak steps, 4 × 10^5 at the limits.