[DP] $ P2059 $ probability Card Game

[DP] probability \ (P2059 \) Card Game

link

Title Description

\ (N \) individuals sit in a circle playing a game. At first we put all the players clockwise from \ (1 \) to \ (N \) number. The first round is the first player \ (1 \) as the dealer. Each round will be making random (that is, at equal probability) to select a card from the pile of cards, assuming that the numbers on the cards for \ (the X-\) , then making the first number on the card to show to all players, and then press clockwise from the dealer position of number \ (X \) individuals will be executed to exit the game. Then the card is placed back into the pile of cards and reshuffle. Who was executed by the next person clockwise will serve as the next round of the dealer. Then after \ (N-1 \) after the last round leaving only one person, this game is the winner. Now that you know in advance the total \ (M \) cards, also know that the numbers on each card. Now you need to determine the probability of each player winning.

Input Format

The first row comprises two integers \ (N, M \) respectively represent the number and the total number of the player cards.

The next line comprising \ (M \) integers, respectively give the number written on each card.

Output Format

Output line containing \ (N \) solid in the form of a percentage number given to two decimal places. Given separately from the player \ (1 \) to the player \ (N \) win probability, the probability between each separated by a space, and finally no spaces.

Sample ]

5 5
2 3 5 7 11
22.72% 17.12% 15.36% 25.44% 19.36%
4 4
3 4 5 6
25.00% 25.00% 25.00% 25.00%

For (30 \) \ % of the data, there are \ (1 \ leq N \ leq 10 \)

For (50 \) \ % of the data, there are \ (1 \ leq N \ leq 30 \)

For the (100 \) \ % of the data, there are \ (1 \ leq N \ leq 50, 1 \ leq M \ leq 50, 1 \ leq \) number on each card \ (\ leq 50 \)

\(Solution\)

In fact, this question I started thinking that at all. Started to think, is the search, but it seems to record a lot of things, I thought transfer is not feasible. That ...... backwards search?

Set \ (f [i] [j ] \) to have \ (i \) individual time, the \ (j \) the probability of personal victory. Because the probability can be added, it can be pushed back.

How to transfer? Hand it found that there \ (i \) in the case of individuals, the dealer cards drawn \ (k \) , then \ (i - 1 \) personal first \ (j \) personal position is ...... Category talk. If \ (K <J \) , \ (J = J - K \) , if the \ (K> J \) , \ (J = I - K + J \) , if the \ (K = J \) , do not consider, because \ (j \) died.

So, so long transition equation:

int p = a[k] % i == 0? i:a[k] % i;
if(p > j) f[i][j] += f[i - 1][i - p + j] / m;
else if(p < j) f[i][j] += f[i - 1][j - p] / m;

\(That's~~all\)

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
long long read(){
    long long x = 0; int f = 0; char c = getchar();
    while(c < '0' || c > '9') f |= c == '-', c = getchar();
    while(c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
    return f? -x:x;
}

int n, m, a[57];
double f[57][57];
int main(){
    n = read(); m = read();
    for(int i = 1; i <= m; ++i) a[i] = read();
    f[1][1] = 1.0;
    for(int i = 2; i <= n; ++i)
        for(int j = 1; j <= i; ++j)
            for(int k = 1; k <= m; ++k){
                int p = a[k] % i == 0? i:a[k] % i;
                if(p > j) f[i][j] += f[i - 1][i - p + j] / m;
                else if(p < j) f[i][j] += f[i - 1][j - p] / m;
            }
    printf("%.2lf%%", f[n][1] * 100);
    for(int i = 2; i <= n; ++i) printf(" %.2lf%%", f[n][i] * 100);
    return 0;
}

Guess you like

Origin www.cnblogs.com/kylinbalck/p/11260150.html