hdoj 5570 balls 【概率dp 求期望】



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 2 1 1 3 5 2 2 4 5 4 2 2 2 2 4 1 4
 

Sample Output
      
      
3.00 2.96 3.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|1
using 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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值