#P4507. Monkey Card Wars
Monkey Card Wars
Monkey Card Wars
In the Monkey Card Wars game, two players, Q and M, each start with a set of monkey cards. In every round, each player randomly (with equal probability) selects one card from his own hand and the two cards battle. The probability that a card of type A beats a card of type B is given by a pre‐computed table \(P(A \text{ beats } B)\). Note that for any two cards A and B, the probabilities satisfy \(P(A \text{ beats } B) + P(B \text{ beats } A) = 1\).
If a player wins a round, he takes the opponent’s card while keeping his own. That is, if Q’s card battles M’s card and Q wins, then Q’s hand will include both the card he used and the opponent’s card, and vice versa. The game continues until one player obtains all the cards. Player Q is worried about the hand he is dealt and wants to calculate his probability of eventually winning the game given his initial set of cards and the opponent’s initial set.
Your task is to compute the probability that Q wins the game, given the win probability table and the initial distribution of monkey cards.
inputFormat
The input consists of the following parts:
- An integer
n
representing the number of different monkey card types. n
lines follow, each containingn
floating point numbers. The j-th number in the i-th line represents the probability that a card of type i beats a card of type j. It is guaranteed that for any two types i and j,P(i beats j) + P(j beats i) = 1
. For simplicity, the diagonal elements (when i = j) will be 0.5.- An integer
m
representing the number of cards in Q's initial hand. - A line with
m
integers, each in the range [1, n], representing the types of cards Q has. - An integer
k
representing the number of cards in M's initial hand. - A line with
k
integers, each in the range [1, n], representing the types of cards M has.
Note: Card types in the input are 1-indexed.
outputFormat
Output a single line containing a floating point number which is the probability that Q eventually wins the game. The answer should be printed with 6 decimal places.
sample
2
0.5 0.4
0.6 0.5
1
1
1
2
0.400000
</p>