hdoj 5570 balls 【概率dp 求期望】

来源:互联网 发布:合肥少儿编程培训机构 编辑:程序博客网 时间:2024/05/16 10:35



balls

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 155    Accepted Submission(s): 104


Problem Description
There are n balls with m colors. The possibility of that the color of the i-th ball is color j is ai,jai,1+ai,2+...+ai,m. If the number of balls with the j-th is x, then you should pay x2 as the cost. Please calculate the expectation of the cost.
 

Input
Several test cases(about 5)

For each cases, first come 2 integers, n,m(1n1000,1m1000)

Then follows n lines with m numbers ai,j(1ai100)
 

Output
For each cases, please output the answer with two decimal places.
 

Sample Input
2 21 13 52 24 54 22 22 41 4
 

Sample Output
3.002.963.20
 



题意:给定n个球共m种颜色,给出一个n*m的矩阵a[][],其中a[i][j]表示第i个球为颜色j的概率。若颜色为j的球的个数为x,则要花费x^2。现在问花费的期望。


数组下标是从0开始的。

思路:设置状态X[i][j],若第i个球颜色为j则X[i][j] = 1,反之x[i][j] = 0。用C[j]表示颜色为j的球的个数。

则有C[j] = x[0][j] + x[1][j] + x[2][j] + ... + x[n-1][j]。

E(cost) = (0<=j<m)sigma(E(C[j]^2)) = (0<=j<m)sigma(E(x[0][j] + x[1][j] + ... + x[n-1][j])^2)。


注公式(一):2ab+2bc+2ac + a+b+c = (a+b+c)^2 - (a^2+b^2+c^2) + (a+b+c) -> n个变量同样适用。


由公式一得到(x[0][j]+...+x[n-1][j])^2表达式

E(cost) = (0<=j<m)sigma(E((x[0][j]^2+...+x[n-1][j]^2) + (2*x[b][j]*x[c][j])), 0<=b, c<n&&b!=c)。

由于期望的线性性质我们把上面的式子拆开:

E(cost) = (0<=j<m)sigma(E(x[k][j]^2), 0<=k<n) + sigma(E(2*x[b][j]*x[c][j]), 0<=b, c<n&&b!=c)。

通过计算可以得到   注:p[i][j]表示第i个球为颜色j的概率。

一、任意(0<=k<n)E(x[k][j]^2) = p[k][j]。

二、任意(0<=b, c<n&&b!=c)E(x[b][j]*x[c][j]) = p[b][j] * p[c][j]。


最终表达式E(cost) = (0<=j<m)sigma(p[k][j], 0<=k<n) + sigma(p[b][j]*p[c][j], 0<=b, c<n&&b!=c)。

注意:p[b][j]*p[c][j],p[c][j]*p[b][j] 都是存在的。

这样

E(cost) = (0<=j<m)sigma(p[k][j], 0<=k<n) + sigma(2*p[b][j]*p[c][j], 0 <= b < c < n)。

由公式一化简得到

E(cost) = (0<=j<m)[sigma(p[k][j])^2 - sigma(p[k][j]^2) + sigma(p[k][j]), 0<=k<n]。


这样时间复杂度O(nm)。


AC代码:


#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <vector>#define INF 0x3f3f3f#define eps 1e-8#define MAXN (1000+10)#define MAXM (100000)#define Ri(a) scanf("%d", &a)#define Rl(a) scanf("%lld", &a)#define Rf(a) scanf("%lf", &a)#define Rs(a) scanf("%s", a)#define Pi(a) printf("%d\n", (a))#define Pf(a) printf("%.2lf\n", (a))#define Pl(a) printf("%lld\n", (a))#define Ps(a) printf("%s\n", (a))#define W(a) while(a--)#define CLR(a, b) memset(a, (b), sizeof(a))#define MOD 1000000007#define LL long long#define lson o<<1, l, mid#define rson o<<1|1, mid+1, r#define ll o<<1#define rr o<<1|1using namespace std;double a[MAXN][MAXN];double p[MAXN][MAXN];int main(){    int n, m;    while(scanf("%d%d", &n, &m) != EOF)    {        for(int i = 0; i < n; i++)        {            double sum = 0;            for(int j = 0; j < m; j++)            {                Rf(a[i][j]);                sum += a[i][j];            }            for(int j = 0; j < m; j++)                p[i][j] = a[i][j] / sum;        }        double ans = 0;        for(int j = 0; j < m; j++)        {            double Sq = 0, S = 0;            for(int i = 0; i < n; i++)            {                Sq += p[i][j] * p[i][j];                S += p[i][j];            }            ans += S * S - Sq + S;        }        Pf(ans);    }    return 0;}



0 0
原创粉丝点击