Coins
Time limit25sMemory limit1024 MB
Count the ways to add stacks of blue and red coins so that the whole row is a second-player win, for each total stack count from k to n.
- Level
Hard8 of 10
- Topics
- Game theory, Combinatorics, Dynamic programming
- Solved
- No attempts yet
Problem
Natalia and Cezary like games, especially ones they invented themselves. They decided to lay out a row of coin stacks, with exactly coins in each stack, where every coin is either blue or red. On her turn, Natalia picks any blue coin and removes it from the game together with all coins above it in its stack. On his turn, Cezary picks any red coin and removes it together with all coins above it in the same stack. The players move in turns, and whoever cannot make a legal move loses. That happens when all of their coins have already been removed.
A row of stacks is called fair if, when both players play optimally, the player who does not make the first move wins. In other words, if Natalia moves first, Cezary wins under optimal play, and if Cezary moves first, Natalia wins.
The pair has already built the first stacks, with coins each. They are now deciding how to finish the row. They concluded that the game should have no more than stacks. For each , count the fair rows of stacks with coins each that begin with the stacks they have already built. Two rows are different if there exist and such that the -th coin of the -th stack is blue in one row and red in the other.
Since the answers can be very large, print them modulo .
Input
The first line contains three integers , , and : the limit on the number of stacks, the number of coins in each stack, and the number of stacks already built. Constraints: , , . The next lines describe the existing stacks. Line is a string of exactly characters 'N' and 'C' that lists the colors of the -th stack from the bottom. If the -th character is 'N', the -th coin from the bottom is blue. Otherwise the character is 'C' and that coin is red.
Output
Print one line with integers separated by spaces. The -th integer is the number of ways to add stacks so that the final row is fair, modulo .
Hint
Explanation of the first example: if no stacks are added, a row with a single stack is not fair. Adding one NNC stack makes a two-stack row fair. There are three ways to add two stacks: [CCN, NNN], [NNN, CCN], and [NCN, NCN].