zoukankan      html  css  js  c++  java
  • HDU 5570 balls 期望 数学

    balls

    Time Limit: 20 Sec

    Memory Limit: 256 MB

    题目连接

    http://acm.hdu.edu.cn/showproblem.php?pid=5570

    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(1≤n≤1000,1≤m≤1000)

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

    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

    HINT

    题意

    题解:

    贡献为x^2,其实就是问你期望的对数有多少个

    假设p[i][j]表示第一个位置出现颜色为j的的概率是多少,那么我们可以推出来该颜色期望的对数的公式是

    ans = p[i][1] + p[i][2]+ ... + p[i][m] + p[i][1]*p[i][2] + p[i][1]*p[i][3] + .... + p[i][2]*p[i][1]+ ... + p[i][m]*p[i][m-1]

    然后化简,就可以得到ans = sigma(p[i][k]) + sigma(p[i][k]) * sigma(p[i][k]) - sigma(p[i][k]*p[i][k])

    就可以On来处理答案啦~ 

    代码:

    #include<iostream>
    #include<stdio.h>
    using namespace std;
    
    int n,m;
    double a[1001][1001];
    double s[1001];
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            for(int i=1;i<=n;i++)
            {
                s[i]=0;
                for(int j=1;j<=m;j++)
                {
                    scanf("%lf",&a[i][j]);
                    s[i]+=a[i][j];
                }
            }
            double ans = 0;
            for(int j=1;j<=m;j++)
            {
                double s1 = 0;
                double s2 = 0;
                for(int i=1;i<=n;i++)
                {
                    double p = a[i][j]/s[i];
                    s1+=p;
                    s2+=p*p;
                }
                ans += s1 + s1*s1 - s2;
            }
            printf("%.2f
    ",ans);
        }
    }
  • 相关阅读:
    js 数组详解(javascript array)
    CentOS 修改IP地址, DNS, 网关
    Leetcode 652.寻找重复的子树
    Leetcode 650.只有两个键的键盘
    Leetcode 649.Dota2参议院
    Leetcode 648.单词替换
    Leetcode 647.回文子串
    Leetcode 645.最长数对链
    Leetcode 643.子数组最大平均数I
    Leetcode 640.求解方程
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4986593.html
Copyright © 2011-2022 走看看