HDU 4248 A Famous Stone Collector 组合数学dp ****

A Famous Stone Collector

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


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
 

 

Recommend
 
题意:给n种石头,每种m个。
        求组成长度 小于等于k的全排列的个数。
解题思路:
 
别人的思路:

  dp[ i ][ j ]表示:考虑前i种石头构成的长度为j的序列的个数。
  转台转移方程:
    dp[ i ][ j ] = dp[ i-1 ][ j ];   //未放入第i种颜色的石头
    for  k := 1 ~ min( j , s[ i ] )  //放入k个第i种颜色的石头
      dp[ i ][ j ] += dp[ i-1 ][ j - k ] * C[ j ][ k ]; //!!!
      Cnm = Cn-1m-1 + Cn-1 1
   其中C[ n ][ m ]表示组合数。
 
 
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 using namespace std;
 6 typedef __int64 LL;
 7 const LL mod=1000000007;
 8 
 9 LL a[102];
10 LL cnm[10002][102];
11 LL  dp[102][10002];
12 void Init()
13 {
14     LL i,j;
15     for(i=0;i<=10000;i++)
16         cnm[i][0]=1;
17     for(i=1;i<=10000;i++)
18     {
19         for(j=1;j<=i&&j<=100;j++)
20         {
21             if(i==j) cnm[i][j]=1;
22             else cnm[i][j]=( cnm[i-1][j]+cnm[i-1][j-1] )%mod;
23         }
24     }
25 }
26 void solve(LL n)
27 {
28     LL i,j,s,Sum=0,k;
29     memset(dp,0,sizeof(dp));
30     for(i=0;i<=100;i++)
31         dp[i][0]=1;
32     for(i=1,s=0;i<=n;i++)
33     {
34         s=s+a[i];
35         for(j=1;j<=s;j++)
36         {
37             dp[i][j]=dp[i-1][j];
38             for(k=1;k<=a[i];k++)
39                 if(k<=j)
40                 dp[i][j]=( dp[i][j]+(cnm[j][k]*dp[i-1][j-k])%mod )%mod;
41             if(i==n) Sum=(Sum+dp[i][j])%mod;
42         }
43     }
44     printf("%I64d\n",Sum);
45 }
46 int main()
47 {
48     LL T=0;
49     LL i,n;
50     Init();
51     while(scanf("%I64d",&n)>0)
52     {
53         for(i=1;i<=n;i++)
54             scanf("%I64d",&a[i]);
55         printf("Case %I64d: ",++T);
56         solve(n);
57     }
58     return 0;
59 }

 

posted @ 2013-12-02 15:43  芷水  阅读(288)  评论(0编辑  收藏  举报