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

field=problem&key=Fudan+Local+Programming+Contest+2012&source=1&searchmode=source" style="color:rgb(26,92,200); text-decoration:none">Fudan Local Programming Contest 2012

#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\n",cas++,ans%MOD);
    }
    return 0;
}

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

时间: 2024-10-18 22:15:12

HDOJ 4248 A Famous Stone Collector DP的相关文章

[ACM] hdu 4248 A Famous Stone Collector (DP+组合)

A Famous Stone Collector 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 li

HDU 4248 A Famous Stone Collector(DP + 组合数)

题目链接:点击打开链接 思路:DP + 组合数. 用d[i][j]表示前第i种颜色的石头, 已经用了j个的方法数, 每次枚举第i种石头放多少个, 假设放k个, 那么相当于从j个位置中选k个, 预处理组合数就行了. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> #include<vector&

hdu 4248 A Famous Stone Collector

首先发现一个很头痛的问题,下面是2个求排列组合的代码 1 memset(C,0,sizeof(C)); 2 for(int i=0;i<10010;i++) 3 { 4 C[i][0]=1; 5 for(int j=1;j<=100;j++) 6 C[i][j]=(C[i-1][j]+C[i-1][j-1])%MOD; 7 } 1 C[0][0]=1; 2 for(int i=1;i<10010;i++) 3 for(int j=0;j<=100;j++) 4 if(j==0) C

hdoj 2046 骨牌铺方格 【DP】+【斐波那契】

dp果然不是好学的... 第n个,即2*n时,可由第n-1个的竖直排列再加一个,和第n-2个中横着排两个 所以f(n) = 1×f(n-1) + 1×f(n-2): 骨牌铺方格 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 28412    Accepted Submission(s): 13771 Problem Descripti

[HDOJ - 5282] Senior&#39;s String 【DP】

题目链接:BZOJ - 5282 题目分析 LCS 就是用经典的 O(n^2) DP 解决,f[i][j] 表示 x 串前 i 个字符与 y 串前 j 个字符的 LCS 长度. f[i][j] = max(f[i - 1][j], f[i][j - 1]); if (x[i] == y[j]) f[i][j] = max(f[i][j], f[i - 1][j - 1] + 1); 然后再设置一个状态 g[i][j], 表示 x 串的前 i 个字符中,有多少个长为 f[i][j] 的子序列同时也

HDOJ 1560 DNA sequence 状压dp 或 IDA*

http://acm.hdu.edu.cn/showproblem.php?pid=1560 题意: 给不超过8个子串,每个子串最多5位,且都只包含ATCG,求最短的母串长度. 分析: 又是上个月写的,所以有点忘了..正解是IDA*.然后可以状压dp,记忆化搜索.dp[i],i用6进制表示,每位表示对应的子串匹配那么多长度所需要的最短母串长度.比如两个子串,13=2*6^1+1*6^0,dp[13]就表示第一个串匹配了第一位,第二个串匹配前两位所需要的最短母串长度. 状态讲完了,不过实际上程序里

HDOJ 4249 A Famous Equation DP

DP: DP[len][k][i][j] 再第len位,第一个数len位为i,第二个数len位为j,和的第len位为k 每一位可以从后面一位转移过来,可以进位也可以不进位 A Famous Equation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 500    Accepted Submission(s): 147 Proble

HDU 2602 Bone Collector DP(01背包)

Bone Collector Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , c

HDOJ 1069 Monkey and Banana 【DP】

Monkey and Banana Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8610 Accepted Submission(s): 4452 Problem Description A group of researchers are designing an experiment to test the IQ of a monke