Table of Contents

SolutionImplementation

Official Analysis (Python, Java)

Solution

We can subtract 11 from TT because we will always get the sample test case and add it to our final answer.

Let ExE_x be the expected value after at most xx submissions. We are trying to find EKE_K. To get the expected value of the final submission, we can find the relation between ExE_x and Ex1E_{x - 1}.

To do this we can find ExE_x in terms of Ex1E_{x -1} and pip_i which is the probability of getting exactly ii test cases.

After x1x-1 submissions, you expect to get Ex1E_{x-1} test cases. There are three posibilites if you decide to resubmit.

Case 1: If you get less test cases than Ex1E_{x-1}, you don't want to resubmit.

Case 2: If you get more test cases than Ex1E_{x-1}, you do want to resubmit.

Case 3: If you get the same number of test cases as Ex1E_{x-1}, it doesn't matter if you resubmit. In this case it wouldn't hurt to resubmit or stay the same so we can combine this with Case 1 (or Case 2).

The probability that you get less than or equal to Ex1E_{x-1} test cases is i=0Ex1pi{\sum_{i=0}}^{E_{x - 1}} p_i. We can multiply this by the Ex1E_{x-1} to get the expected value of ExE_x if we stop before the xxth submission and we get Ex1i=0Ex1piE_{x-1} \cdot {\sum_{i=0}}^{E_{x - 1}} p_i

The probability that you get more than Ex1E_{x-1} test cases is i=Ex1+1Tpi{\sum_{i=\lfloor E_{x-1} \rfloor + 1}}^T p_i. The expected value of getting more than Ex1E_{x-1} test cases is the sum of ii from Ex1+1{\lfloor E_{x-1} \rfloor + 1} to TT of the probability of getting ii test cases multiplied by ii or Ex1+1Tpii{\sum_{\lfloor E_{x-1} \rfloor + 1}}^T p_i i.

In addition, these formulas for both cases can help us mathematically prove that you can merge Case 3 with either Case 1 or 2. The only difference between both cases is whether the i=Ex1i = E_{x-1} will belong to the first or second summation. If we merge it with Case 1, the term of the summation will become Ex1pEx1E_{x-1} \cdot p_{E_{x-1}}. If we merge it with Case 3, it would also become Ex1pEx1E_{x-1} \cdot p_{E_{x-1}}. This happens because piEx1=piip_i \cdot E_{x-1} = p_i \cdot i is only satisfied when i=Ex1i=E_{x-1}.

Adding the two parts gives our final expression for the expected value:

Ex=Ex1i=0Ex1pi+i=Ex1+1TipiE_x = E_{x -1} \cdot \sum_{i=0}^{\lfloor E_{x - 1} \rfloor} p_i + \sum_{i=\lfloor E_{x - 1} + 1 \rfloor}^{T} i p_i

We can solve the problem by simulating each test case and recalculating the summations for each test case in O(TK)O(TK). However, this can easily be sped up to O(T2+K)O(T^2 + K) by pre-calculating pip_i using Pascal's identity and by precalculating the prefix sums of both pip_i and piip_i i so we can get the values of the summations in O(1)O(1) instead of O(T)O(T). This gives us the first 99 test cases but is not good enough.

In order to fully solve the question, we need to remove the O(K)O(K) factor. We can do this if we are able to process multiple values of ExE_x in a single step.

Claim: We can process multiple queries if the values of i=0Ex1pi{\sum_{i=0}}^{\lfloor E_{x - 1} \rfloor} p_i and i=Ex1+1Tipi{\sum_{i=\lfloor E_{x - 1} + 1 \rfloor}}^{T} i p_i don't change between queries.

Proof: Let a=i=0Ex1pia = {\sum_{i=0}}^{\lfloor E_{x - 1} \rfloor} p_i and b=i=Ex1+1Tipib = {\sum_{i=\lfloor E_{x - 1} + 1 \rfloor}}^{T} i p_i. Then our formula becomes

Ex=Ex1a+bE_x = E_{x - 1} \cdot a + b

Since aa and bb are constant, we can extend this formula and get

Ex=a(a...(aExk+b)...+b)+bE_x = a \cdot (a \cdot ... (a \cdot E_{x - k} + b) ... + b) + b

Simplifying gives us a geometric sequence.

Ex=akExk+ak1b+ak2+...+bE_x = a^k \cdot E_{x - k} + a^{k - 1}b + a^{k - 2} + ... + b

We can rewrite the second through last term using the formula for a geometric series.

Ex=akExk+b(ak1)a1E_x = a^k \cdot E_{x - k} + \frac{b(a^k - 1)}{a - 1}

Here is more information about geometric series.

However, the values of aa and bb won't always remain the same. We can binary search on the max number of submissions Bessie can make before the values of aa and bb change. aa and bb will change when the value of Ex{\lfloor E_x \rfloor} changes. The conditions for binary search are fulfilled because ExE_x is a non-decreasing function.

Since there are at most TT values of ExE_x, aa and bb can change at most TT times so the resulting time complexity will be O(T2+TlogK)O(T^2 + T \log K).

Implementation

Time Complexity: O(T2+TlogK)\mathcal{O}(T^2 + T \log K)

C++

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = double;
const int MAXT = 1000;
db prob[MAXT + 1][MAXT + 1];
db pref_prob[MAXT + 1];

Python

import math
def skip_submissions(a: float, b: float, e: float, x: float) -> float:
"""
Function that skips forward x submission
with constants a and b and current expected value e
based on the geometric series formula
"""
return pow(a, x) * e + b * (pow(a, x) - 1) / (a - 1)

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!