Generic Poker
Time limit1sMemory limit128 MB
Count hands of L cards (ranks 1 to M, N copies each) that match a pattern of wildcards and variables shifted by pluses, then print the probability as an irreducible fraction.
- Level
Hard8 of 10
- Topics
- Combinatorics, Brute force, Math, Implementation
- Solved
- No attempts yet
Problem
There is a deck of cards. Each card has a rank, an integer from to , and the deck contains exactly cards of each rank. Here a card of rank is written simply as .
You draw a hand of cards uniformly at random from the deck. If the drawn hand matches the given pattern, a bonus is rewarded. A pattern is described by the following grammar.
hand_pattern = card_pattern1 ' ' card_pattern2 ' ' ... ' ' card_patternL
card_pattern = '*' | var_plus
var_plus = variable | var_plus '+'
variable = 'a' | 'b' | 'c'
- hand_pattern: A hand matches the hand_pattern if the cards of the hand can be assigned one-to-one to the card_patterns so that every card_pattern matches the distinct card assigned to it.
- card_pattern
- If the card_pattern is an asterisk
*, it matches any card. - The letters
a,b, andcare variables, and all occurrences of the same variable must match cards of the same rank. A variable followed by+characters matches a card whose rank is (the rank assigned to that variable) + (the number of+characters). - If a card_pattern with a variable followed by plus characters appears, you may assume every card_pattern with that variable and through plus characters also appears. For example, if
a+++appears, thena,a+, anda++appear too.
- If the card_pattern is an asterisk
There is no restriction on which ranks different variables denote. For example, a and b may or may not match cards of the same rank.
Here are some examples. The pattern
a * b a b
matches the hand below, with a and b meaning and (or and ).
3 3 10 10 9
The same pattern also matches the following hand, where both a and b mean .
3 3 3 3 9
The pattern
a a+ a++ a+++ a++++
matches the following hand, where a means .
4 5 6 7 8
Write a program that, for a given hand_pattern, computes the probability that a hand drawn at random from the deck matches the pattern.
Input
The input is a sequence of datasets. Each dataset has the following format.
N M L
card_pattern1 card_pattern2 ... card_patternL
The first line contains three positive integers , , and : is the number of cards of each rank, is the number of ranks, and is the number of cards in a hand. They satisfy the following constraints.
The second line contains a hand_pattern consisting of card_patterns separated by single spaces.
The end of the input is indicated by a line containing three zeros separated by single spaces (0 0 0). This line is not processed.
Output
For each dataset, output on its own line the probability that the hand matches the hand_pattern as an irreducible fraction , where and . Print 0/1 when the probability is and 1/1 when it is . Each line must contain nothing but this fraction.