codeforces 601C (概率DP)

C. Kleofáš and the n-thlon
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Kleofáš is participating in an n-thlon - a tournament consisting of n different competitions in n different disciplines (numbered 1through n). There are m participants in the n-thlon and each of them participates in all competitions.

In each of these n competitions, the participants are given ranks from 1 to m in such a way that no two participants are given the same rank - in other words, the ranks in each competition form a permutation of numbers from 1 to m. The score of a participant in a competition is equal to his/her rank in it.

The overall score of each participant is computed as the sum of that participant's scores in all competitions.

The overall rank of each participant is equal to 1 + k, where k is the number of participants with strictly smaller overall score.

The n-thlon is over now, but the results haven't been published yet. Kleofáš still remembers his ranks in each particular competition; however, he doesn't remember anything about how well the other participants did. Therefore, Kleofáš would like to know his expected overall rank.

All competitors are equally good at each discipline, so all rankings (permutations of ranks of everyone except Kleofáš) in each competition are equiprobable.

Input

The first line of the input contains two space-separated integers n (1 ≤ n ≤ 100) and m (1 ≤ m ≤ 1000) — the number of competitions and the number of participants respectively.

Then, n lines follow. The i-th of them contains one integer xi (1 ≤ xi ≤ m) — the rank of Kleofáš in the i-th competition.

Output

Output a single real number – the expected overall rank of Kleofáš. Your answer will be considered correct if its relative or absolute error doesn't exceed 10 - 9.

Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .

Sample test(s)
input
4 10
2
1
2
1
output
1.0000000000000000
input
5 5
1
2
3
4
5
output
2.7500000000000000
input
3 6
2
4
2
output
1.6799999999999999
Note

In the first sample, Kleofáš has overall score 6. Nobody else can have overall score less than 6 (but it's possible for one other person to have overall score 6 as well), so his overall rank must be 1.

题意是n场比赛m个人,考诉你每一场比赛的排名问总排名的数学期望,没场比赛得到这场比赛排名的分数,总分数越少排名越高.

关键是需要算出一个人排名比他高的概率p,那么设f[i][j]表示处理了i个人以后排名为j的概率,就可以用用f[i][j] = p*f[i-1][j] + (1-p)*f[i-1][j-1],

概率p也可以用类似的方法求,设dp[i][j]表示进行了前i场比赛得到j分的概率,那么dp[i][j] = dp[i-1][j-1]+d[i-1][j-2]+...+dp[i-1][j-m] - dp[i-1][j-a[i]],发现前面一部分是一个连续的和,所以可以维护一个前缀和.

复杂度是O(n*n*m + m*m).

#include <bits/stdc++.h>
using namespace std;

double f[1111][1111]; 
double a[111];
int m, n;
double p;
double dp[2][1111111];
double all[2][1111111];

double cal (int i) {
    if (i <= 0)
        return 0;
    return all[0][i];
}

int main () {
    //freopen ("in", "r", stdin);
    int sum = 0;
    cin >> n >> m;
    memset (dp, 0, sizeof dp);
    memset (all, 0, sizeof all);
    memset (f, 0, sizeof f);
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        sum += a[i];
    }
    double pp = 1.0/(m-1);
    for (int i = 1; i <= m; i++) {
        if (a[1] == i)
            continue;
        dp[0][i] = pp;
    }
    for (int i = 1; i <= n*m; i++)
        all[0][i] = all[0][i-1]+dp[0][i];
    for (int i = 2; i <= n; i++) {
        for (int j = 1; j <= n*m; j++) {
            dp[1][j] = pp*(cal (j-1) - cal (j-m-1) - (j-a[i] >= 0 ? dp[0][j-(int)a[i]] : 0.0));
            all[1][j] = all[1][j-1]+dp[1][j];
        }
        for (int j = 1; j <= n*m; j++)
            all[0][j] = all[1][j], dp[0][j] = dp[1][j];
    }
    p = 0;
    for (int i = 1; i < sum; i++) {
        p += dp[0][i];
    }

    f[0][1] = 1.0;
    for (int i = 1; i <= m-1; i++) {
        for (int j = 1; j <= i+1; j++) {
            f[i][j] = f[i-1][j-1]*p + f[i-1][j]*(1.0-p);
        }
    }
    double ans = 0;
    for (int i = 1; i <= m; i++) {
        ans += i*f[m-1][i];
    }
    printf ("%.16f\n", ans);
    return 0;
}


你可能感兴趣的:(codeforces 601C (概率DP))