Editorial for Bubble Cup V8 H Bots
Submitting an official solution before solving the problem yourself is a bannable offence.
The problem can be transformed to a more formal one: how many vertices will a trie (data structure) contain, if we add all possible strings of length with zeroes and ones to this trie.
First, it is obvious that the upper half of this tree will be a full binary tree. Let's take a look at :
- level : vertex
- level : vertices
- level : vertices
- level : vertices.
Starting from level , not every vertex will have two children. Vertices that will have two children are those that have less than zeroes and less than ones on the path to the root.
So, here is how we calculate how many vertices there will be on level :
- Let's define as the number of vertices on level which have less than two children
- If is the number of vertices on level , then
- can be calculated easily using binomial coefficients:
Everything else in the task are implementation techniques. We need to calculate some modular multiplicative inverses, which can be done in logarithmic time using exponentiation by squaring. We can calculate binomial coefficients efficiently, by using the values calculated for the previous level.
Time complexity:
Comments
Can you share the proof?
Challenge: Prove that the answer is exactly .