PROBLEM LINK:
Author and Editorialist: Shubham Chauhan
DIFFICULTY:
Medium
PREREQUISITES:
Modulo Arithmetic , Tree
EXPLANATION:
Problem Naturally can be transformed to a more formal way : How many vertices will a trie contain if we add all possible strings with length 2 * N with equal number of zeroes and ones to it.
So first of all, it is obvious that upper half of this tree would be a full binary tree. Lets take a look on N = 3: level (0 - 1) vertex, level (1 - 2) vertices , level (2 - 4) vertices, level (3 - 8) vertices.
Starting from Nth level not every vertex will duplicate: only those that haven’t spent their 0’s or 1’s will.
So here is how to calculate how may vertices will be there on level i + 1 :
-
Lets assign Number of non duplicating vertices from level to PD(i) .
-
Count(i + 1) = PD(i) + (Count(i) - PD(i))*2.
-
And PD can be calculated pretty easily with binomial coefficients: PD(i) = 2 * C(i , N)
-
Everything else is implementation techniques: inverse modulo arithmetics + some fast way to calculate C(i , N) and sum counts
SOLUTIONS:
To be Update Soon