Editorial for LCC '21 Contest 1 S3 - Balanced Binary Strings


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.

Author: hewmatt10

We want to pick out the same number of 0 as 1. Let a be the number of 0 and b be the number of 1. Let m be the larger of a and b and n be the smaller of the them.

Since we want the same number of 0 and 1, we should pick out the same number from of the total number 0 and 1. For one taking out i 0 and i 1, the equation would be:

\displaystyle {n \choose i} {m \choose i}

But since we want all possible ways to choose identical numbers of 0 and 1, we must sum from 0 to n (since n is the smaller of the two). Then, we need to subtract 1 since the empty subset should not be counted.

The answer is:

\displaystyle \sum_{i=0}^{n} {n \choose i} {m \choose i} = \sum_{i=0}^{n} {n \choose i} {m \choose m - i}

By Vandermonde's identity:

\displaystyle \sum_{i=0}^{n} {n \choose i} {m \choose m - i} = {n + m \choose m} = {n + m \choose n}

Subtracting 1:

\displaystyle {n + m \choose m} - 1

or

\displaystyle {n + m \choose n} - 1

How do we code the choose function?

\displaystyle {{n + m} \choose n} = \frac{(n+m)!}{n!m!}

But since numbers may be large, we just mod 10^9+7, which is a prime number. In modular arithmetic, we cannot divide, so we have to multiply the mod inverse instead.

By Fermat's Little Theorem:

\displaystyle a^{k-1} \equiv 1 (mod k)

\displaystyle a^{k-2} \equiv a^{-1} (mod j)

So we write the choose function as:

\displaystyle (n+m)! (n!)^{-1}(m!)^{-1}


Comments

There are no comments at the moment.