MemSQL Start[c]UP 3.0 - Round 2 (onsite finalists)
F
Egg Roulette
Codeforces
3300
原题
← EG →
翻译中…

The game of Egg Roulette is played between two players. Initially $2*R*$ raw eggs and $2*C*$ cooked eggs are placed randomly into a carton. The shells are left on so there is no way to distinguish a raw egg from a cooked egg. One at a time, a player will select an egg, and then smash the egg on his/her forehead. If the egg was cooked, not much happens, but if the egg was raw, it will make quite the mess. This continues until one player has broken $*R*$ raw eggs, at which point that player is declared the loser and the other player wins.

翻译中…

The order in which players take turns can be described as a string of 'A' and 'B' characters, where the $*i*$-th character tells which player should choose the $*i*$-th egg. Traditionally, players take turns going one after the other. That is, they follow the ordering "ABABAB...". This isn't very fair though, because the second player will win more often than the first. We'd like you to find a better ordering for the players to take their turns. Let's define the unfairness of an ordering as the absolute difference between the first player's win probability and the second player's win probability. We're interested in orderings that minimize the unfairness. We only consider an ordering valid if it contains the same number of 'A's as 'B's.

翻译中…

You will also be given a string $*S*$ of length $2(*R* + *C*)$ containing only 'A', 'B', and '?' characters. An ordering is said to match $*S*$ if it only differs from $*S*$ in positions where $*S*$ contains a '?'. Of the valid orderings that minimize unfairness, how many match $*S*$?

翻译中…

Input

翻译中…

The first line of input will contain integers $*R*$ and $*C*$ $(1 ≤ *R*, *C* ≤ 20, *R* + *C* ≤ 30)$.

翻译中…

The second line of input will contain the string $*S*$ of length $2(*R* + *C*)$ consisting only of characters 'A', 'B', '?'.

翻译中…

Output

翻译中…

Print the number of valid orderings that minimize unfairness and match $*S*$.

翻译中…

Note

翻译中…

In the first test case, the minimum unfairness is $0$, and the orderings that achieve it are "ABBA" and "BAAB", neither of which match $*S*$. Note that an ordering such as "ABBB" would also have an unfairness of $0$, but is invalid because it does not contain the same number of 'A's as 'B's.

翻译中…

In the second example, the only matching ordering is "BBAAABABABBA".

翻译中…

样例

Input #1
1 1
??BB
Output #1
0
InputCopy OutputCopy
Input #2
2 4
?BA??B??A???
Output #2
1
InputCopy OutputCopy
Input #3
4 14
????A??BB?????????????AB????????????
Output #3
314
InputCopy OutputCopy
题目信息
题号
865F
时间限制
3 s
内存限制
256 MB
标签
位运算
暴力
分治
数学
折半搜索

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。