#P4507. [CTSC2013] 猴子大战

[CTSC2013] 猴子大战

Description

Xiao Q and Xiao M recently invented a card game called Monkey War.

At the start, Xiao Q and Xiao M each receive some monkey cards. In each round, the two players independently draw one card uniformly at random from their own current cards to fight. The winner takes both cards used in that round. If one player obtains all the monkey cards, that player wins the whole game; otherwise, the game continues.

After several matches, Xiao Q and Xiao M computed a win-rate table that gives, for every pair of monkey cards, the probability that one card beats the other. Since every fight has a decisive outcome and the win rate does not depend on order, for any two cards A and B, the probability that A beats B plus the probability that B beats A is 11.

Since he keeps losing to Xiao M, Xiao Q starts to doubt whether the cards he gets can ever win overall. He wants the probability that he wins for every initial allocation of monkey cards he might receive.

Because Xiao Q has tens of thousands of sports plans coming up at the CD City Sports Center, this problem is left to you to solve.

Input Format

The first line contains two positive integers nn and mm, the total number of monkey cards and the number of initial allocations to query.

The next nn lines each contain nn real numbers with two decimal places. Among these nn lines, the number in row ii, column jj is Pi,jP_{i,j}, the probability that card ii beats card jj. It is guaranteed that Pi,j+Pj,i=1P_{i,j} + P_{j,i} = 1. In particular, Pi,i=0.5P_{i,i} = 0.5, which has no special meaning.

The last mm lines each contain a length-nn 01 string with no spaces, representing an initial allocation of cards. If the ii-th character is '0', then card ii initially belongs to Xiao M; otherwise it belongs to Xiao Q.

Output Format

Output mm lines. Each line should contain a real number, rounded to 8 decimal places (print exactly 8 digits after the decimal point), representing Xiao Q’s probability of winning under the corresponding initial allocation.

3 4
0.50 0.60 0.40
0.40 0.50 0.70
0.60 0.30 0.50
110
011
111
000
0.71304348
0.66086957
1.00000000
0.00000000

Hint

Scoring method:

  • If each line of your output differs from the reference answer by at most 2×1062 \times 10^{-6}, you will receive credit for that test point; otherwise, you will receive no credit.
  • The reference answers differ from the true values by at most 10810^{-8}. Therefore, to be correct, your output should differ from the true values by at most 2×1061082 \times 10^{-6} \sim 10^{-8}.

Constraints:

  • For each set of testdata, nn takes the following values:
Test point ID nn Test point ID nn
1 n=2n = 2 6 n=20n = 20
2 n=5n = 5 7 n=40n = 40
3 n=7n = 7 8 n=60n = 60
4 n=8n = 8 9 n=80n = 80
5 n=10n = 10 10 n=100n = 100
  • For 100% of the testdata, 1n1001 \le n \le 100, 1mn21 \le m \le n^2. 0Pi,j10 \le P_{i,j} \le 1, Pi,jP_{i,j} has exactly 2 decimal places, and Pi,j+Pj,i=1P_{i,j} + P_{j,i} = 1. The 01 strings that represent initial allocations all have length nn and contain no other characters.

Translated by ChatGPT 5