zoukankan      html  css  js  c++  java
  • HDOJ 4248 A Famous Stone Collector DP


    DP: dp[i][j]前i堆放j序列长度有多少行法,

    dp[i][j]=dp[i-1][j] (不用第i堆), 

    dp[i][j]+=dp[i-1][j-k]*C[j][k] (用第i堆的k个石头)


    A Famous Stone Collector

    Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 845    Accepted Submission(s): 322


    Problem Description
    Mr. B loves to play with colorful stones. There are n colors of stones in his collection. Two stones with the same color are indistinguishable. Mr. B would like to 
    select some stones and arrange them in line to form a beautiful pattern. After several arrangements he finds it very hard for him to enumerate all the patterns. So he asks you to write a program to count the number of different possible patterns. Two patterns are considered different, if and only if they have different number of stones or have different colors on at least one position.
     

    Input
    Each test case starts with a line containing an integer n indicating the kinds of stones Mr. B have. Following this is a line containing n integers - the number of 
    available stones of each color respectively. All the input numbers will be nonnegative and no more than 100.
     

    Output
    For each test case, display a single line containing the case number and the number of different patterns Mr. B can make with these stones, modulo 1,000,000,007, 
    which is a prime number.
     

    Sample Input
    3 1 1 1 2 1 2
     

    Sample Output
    Case 1: 15 Case 2: 8
    Hint
    In the first case, suppose the colors of the stones Mr. B has are B, G and M, the different patterns Mr. B can form are: B; G; M; BG; BM; GM; GB; MB; MG; BGM; BMG; GBM; GMB; MBG; MGB.
     

    Source
     




    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    
    typedef long long int LL;
    
    const LL MOD = 1000000007LL;
    
    LL C[11000][210];
    void getC()
    {
        for(int i=0;i<11000;i++) C[i][0]=C[i][i]=1;
        for(int i=2;i<11000;i++)
            for(int j=1;j<i&&j<=200;j++)
                C[i][j]=(C[i-1][j]+C[i-1][j-1])%MOD;
    }
    
    LL dp[110][11000];
    int n,a[110];
    
    int main()
    {
        getC();
        int cas=1;
        while(scanf("%d",&n)!=EOF)
        {
            for(int i=1;i<=n;i++)
                scanf("%d",a+i);
            memset(dp,0,sizeof(dp));
            int len=0;
            dp[0][0]=1;
            for(int i=1;i<=n;i++)
            {
                len+=a[i];
                for(int j=0;j<=len;j++)
                {
                    dp[i][j]=(dp[i][j]+dp[i-1][j])%MOD;
                    for(int k=1;k<=a[i]&&j-k>=0;k++)
                    {
                        dp[i][j]=(dp[i][j]+dp[i-1][j-k]*C[j][k])%MOD;
                    }
                }
            }
            LL ans=0;
            for(int i=1;i<=len;i++)
                ans=(ans+dp[n][i])%MOD;
            printf("Case %d: %lld
    ",cas++,ans%MOD);
        }
        return 0;
    }
    



    版权声明:来自: 代码代码猿猿AC路 http://blog.csdn.net/ck_boss

  • 相关阅读:
    C语言中static修饰符的意义
    网络字节序
    socket接口详解
    Python实现常见算法[3]——汉罗塔递归
    Python实现常见算法[2]——快速排序
    Python实现常见算法[1]——冒泡排序
    关于钛星设备主动推送报文的解析
    2015自我提升计划
    python 基础(函数 命名空间和作用域,嵌套和作用域链,闭包)
    python 基础(文件操作,注册,以及函数)
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4876861.html
Copyright © 2011-2022 走看看