HDU 4248 A Famous Stone Collector(组合计数)

来源:互联网 发布:淘宝的电脑主机能买吗 编辑:程序博客网 时间:2024/05/17 03:42
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
31 1 121 2
 

Sample Output
Case 1: 15Case 2: 8

Hint

分析:一类组合计数问题,设DP[i][j]:前i个组成长度为j的方案数

那么对于第i种颜色,只需考虑转移到此状态的方案数。

dp[i][j]=dp[i-1][j]+dp[i][j-k]*C(j,k)

#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<set>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int INF=0x3f3f3f3f;typedef long long LL;const int maxn=1010;const int mod=1e9+7;LL C[110*110][110];int A[110];LL dp[110][10100];void init(){    C[1][0]=C[1][1]=C[0][0]=1;    for(int i=2;i<=10000;i++)    {        C[i][0]=1;        for(int j=1;j<=100;j++)        {            if(j==i) C[i][i]=1;            else C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;        }    }}int main(){    int n;init();    int cas=1;    while(~scanf("%d",&n))    {        CLEAR(dp,0);        int S=0;        REPF(i,1,n)        {           scanf("%d",&A[i]);           S+=A[i];        }        dp[0][0]=1;        for(int i=1;i<=n;i++)        {            for(int j=0;j<=S;j++)            {                dp[i][j]=dp[i-1][j];                for(int k=1;k<=A[i];k++)                {                    if(j-k<0) continue;                    dp[i][j]=(dp[i][j]+dp[i-1][j-k]*C[j][k]%mod)%mod;                }            }        }        LL res=0;        for(int i=1;i<=S;i++)            res=(res+dp[n][i])%mod;        printf("Case %d: %lld\n",cas++,res);    }}


0 0
原创粉丝点击