Codeforces Round #758 Dominoes Solution (Java/C++)

Solution:

First of all, we need to notice that after the domino is painted, there are actually only 4 states: BW, WB, WW, BB.

So we naturally began to think about how to construct an valid painting method. It is not difficult to find that the final construction must be like this: BB, WB, WB, …, WB, WW, BW, BW, …, BW, BB, WW, BB, WW.
In other words: 1. As long as BB and WW exist, then WB and BW actually have no effect on the structure. 2. The number of BB must be the same as the number of WW.
Of course, one exception is the absence of BB and WW. In this case, either all WB or all BW.

So, obviously, the number of letter B and the number of letter W must be the same. (Because the numbers of BB and WW are equal, and WB and BW do not affect the number of B and W)
And this conclusion is almost the same in reverse: when the number of letter B and letter W are the same, this coloring must be valid. Because when the numbers are the same, excluding WB and BW, the number of the remaining letter B is still the same as the number of letter W, so the number of remaining BB and WW must be the same.
So the number of coloring methods is: $C_{2n-\text{count('W')}-\text{count('B')}}^{n - \text{count('W')}}$.

But there is one exception. If all are WB and BW, because there is no BB and WW, then the painting method which include both WB and BW is invalid.
Obviously, "W?" and "?B" can only be painted as WB. "?W" and "B?" can only be painted as BW. And "??" can do both.
let use invalid to represent the number of invalid painting methods, then $invalid=2^{\text{count('??')}}$.
But there are exceptions here. If all can be painted by WB, invalid-=1. Similarly, if all can be painted BW, invalid-=1.

So, the final result is: $C_{2n-\text{count('W')}-\text{count('B')}}^{n - \text{count('W')}} - \text{invalid}$.

Code:

Java

Submission #139402852 - Codeforces
Codeforces. Programming competitions and contests, programming community

C++

Submission #139403670 - Codeforces
Codeforces. Programming competitions and contests, programming community
DigitalOcean Referral Badge 蜀ICP备19018968号