Editorial for LCC '21 Contest 1 S3 - Balanced Binary Strings
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
We want to pick out the same number of as . Let be the number of and be the number of . Let be the larger of and and be the smaller of the them.
Since we want the same number of and , we should pick out the same number from of the total number and . For one taking out and , the equation would be:
But since we want all possible ways to choose identical numbers of and , we must sum from to (since is the smaller of the two). Then, we need to subtract since the empty subset should not be counted.
The answer is:
By Vandermonde's identity:
Subtracting :
or
How do we code the choose function?
But since numbers may be large, we just mod , 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:
So we write the choose function as:
Comments