HDU 4248 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 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

分析:一類組合計數問題,設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);
    }
}


發佈了434 篇原創文章 · 獲贊 5 · 訪問量 35萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章