zoukankan      html  css  js  c++  java
  • Kids and Prizes(SGU 495)

    495. Kids and Prizes

    Time limit per test: 0.25 second(s)
    Memory limit: 262144 kilobytes
    input: standard
    output: standard




    ICPC (International Cardboard Producing Company) is in the business of producing cardboard boxes. Recently the company organized a contest for kids for the best design of a cardboard box and selected M winners. There are Nprizes for the winners, each one carefully packed in a cardboard box (made by the ICPC, of course). The awarding process will be as follows:

    • All the boxes with prizes will be stored in a separate room.
    • The winners will enter the room, one at a time.
    • Each winner selects one of the boxes.
    • The selected box is opened by a representative of the organizing committee.
    • If the box contains a prize, the winner takes it.
    • If the box is empty (because the same box has already been selected by one or more previous winners), the winner will instead get a certificate printed on a sheet of excellent cardboard (made by ICPC, of course).
    • Whether there is a prize or not, the box is re-sealed and returned to the room.

    The management of the company would like to know how many prizes will be given by the above process. It is assumed that each winner picks a box at random and that all boxes are equally likely to be picked. Compute the mathematical expectation of the number of prizes given (the certificates are not counted as prizes, of course).

    Input

    The first and only line of the input file contains the values of N and M ().

    Output

    The first and only line of the output file should contain a single real number: the expected number of prizes given out. The answer is accepted as correct if either the absolute or the relative error is less than or equal to 10-9.

    Example(s)
    sample input
    sample output
    5 7
    
    3.951424
    
    sample input
    sample output
    4 3
    
    2.3125

     两种方法:

    1.设A = “所有奖品都不被取到”, P(A) = ((n - 1) / n) ^ m;

    E(A) =  n * P(A);

    E(!A) = n - E(A);

    2.P(i) 表示第i个人拿到奖品的概率;

    P(i) = P(i - 1) * (1 - P(i - 1)) + P(i - 1) * (P(i - 1) - 1 / n);

    E = ∑(P(i) * 1);

    #include <cstdio>
    #include <iostream>
    #include <sstream>
    #include <cmath>
    #include <cstring>
    #include <cstdlib>
    #include <string>
    #include <vector>
    #include <map>
    #include <set>
    #include <queue>
    #include <stack>
    #include <algorithm>
    using namespace std;
    #define ll long long
    #define _cle(m, a) memset(m, a, sizeof(m))
    #define repu(i, a, b) for(int i = a; i < b; i++)
    #define MAXN 100005
    
    double d[MAXN];
    int main()
    {
        double n, m;
        while(~scanf("%lf%lf", &n, &m))
        {
            d[1] = 1.0;
            for(int i = 2; i <= (int)m; i++)
                d[i] = (1.0 - d[i - 1]) * d[i - 1] + d[i - 1] * (d[i - 1] - 1.0 / n);
    
            double e = 0.0;
            for(int i = 1; i <= (int)m; i++) e += d[i];
            printf("%.10lf
    ", e);
        }
    
        return 0;
    }
    View Code
    #include <cstdio>
    #include <iostream>
    #include <sstream>
    #include <cmath>
    #include <cstring>
    #include <cstdlib>
    #include <string>
    #include <vector>
    #include <map>
    #include <set>
    #include <queue>
    #include <stack>
    #include <algorithm>
    using namespace std;
    #define ll long long
    #define _cle(m, a) memset(m, a, sizeof(m))
    #define repu(i, a, b) for(int i = a; i < b; i++)
    #define MAXN 1005
    
    
    int main()
    {
        double n, m;
        while(~scanf("%lf%lf", &n, &m))
            printf("%.9lf
    ", n - n * pow((n - 1) / n, m));
        return 0;
    }
    View Code
  • 相关阅读:
    Struts2+Spring+Mybatis+Junit 测试
    struts2 action 页面跳转
    Java面试题(全)
    Java面试题
    Kubernets二进制安装(17)之安装部署Dashboard
    Kubernets二进制安装(16)之安装部署traefik(ingress)
    Kubernets二进制安装(15)之安装部署coredns
    Kubernets二进制安装(14)之flannel之SNAT规则优化
    Kubernets二进制安装(13)之部署Flannel
    Docker网络模型
  • 原文地址:https://www.cnblogs.com/sunus/p/4425585.html
Copyright © 2011-2022 走看看