zoukankan      html  css  js  c++  java
  • HihoCoder1339 Dice Possibility(概率DP+母函数)

    时间限制:10000ms
    单点时限:1000ms
    内存限制:256MB

    描述

    What is possibility of rolling N dice and the sum of the numbers equals to M?

    输入

    Two integers N and M. (1 ≤ N ≤ 100, 1 ≤ M ≤ 600)

    输出

    Output the possibility in percentage with 2 decimal places.

    样例输入
    2 10
    样例输出
    8.33

    母函数写法:

    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    using namespace std;
    double ans;
    double a[1010],b[1010];
    int n,m;
    void getdp()
    {
        for(int i=1;i<=6;i++) a[i]=1;
        for(int i=2;i<=n;i++){
            for(int j=m;j>=1;j--)
             for(int k=1;k<=6;k++)
              if(j-k>0) b[j]+=a[j-k];
            for(int j=1;j<=m;j++){
                a[j]=b[j];
                b[j]=0;
            }
        }
    }
    int main()
    {
        scanf("%d%d",&n,&m);
        getdp();
        ans=1.0*a[m];
        for(int i=1;i<=n;i++){
            ans/=6.0;
        }
        ans*=100;
        printf("%.2lf
    ",ans);
        return 0;
    }        

    常规DP写法:

    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    using namespace std;
    using namespace std;
    double d[101][601];
    int main()
    {
        int n,m;
        scanf("%d%d",&n,&m);
        memset(d,0,sizeof d);
        d[0][0]=1;
        for(int i=1;i<=n;i++)
        {
            for(int k=1;k<=6;k++)
            {
                for(int j=m;j>=k;j--)d[i][j]+=d[i-1][j-k]/6;
            }
        }
        printf("%0.2lf
    ",d[n][m]*100);
        return 0;
    }
  • 相关阅读:
    XGBoost参数
    算法
    Python2 和Python3 的区别
    解决ubuntu上ifconfig没有eth0/ens33且无法上网的问题
    Ubuntu 忘记root user密码 关闭图形界面
    rabbitmq消息队列
    CMDB 数据加密 最终整合API验证+AES数据加密
    CMDB API验证
    使用Python生成ASCII字符画
    google 技巧
  • 原文地址:https://www.cnblogs.com/hua-dong/p/7955709.html
Copyright © 2011-2022 走看看