A Famous Stone Collector
Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 876 Accepted Submission(s): 329
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
Fudan Local Programming Contest 2012
Recommend
dp[i][j] 表示前i堆石子构成长度为j的串的方案数;
状态转移方程是:k为i堆使用的数量
dp[i][j] = (dp[i][j] + dp[i-1][j-k] * C[j][k]%M)%M;自己写挫了,上一大神代码。
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; #define M 1000000007 long long dp[110][10010]; long long C[11000][110]; void cc() { int i,j; C[0][0]=1; for(i=1;i<10010;i++) for(j=0;j<=100;j++) if(j == 0) C[i][j] = C[i-1][j]; else C[i][j] = (C[i-1][j]+C[i-1][j-1])%M; //C[i][j]=(j==0) ? C[i-1][j] : (); } int main() { cc(); int n; int cas = 1; while(cin >> n) { memset(dp,0,sizeof(dp)); dp[0][0] = 1; int sum = 0; for(int i = 1;i <= n;i++) { int t; cin >> t; sum += t; for(int k = 0;k <= t;k++) for(int j = k;j <= sum;j++) dp[i][j] = (dp[i][j] + dp[i-1][j-k] * C[j][k]%M)%M; } long long re = 0; for(int i = 1;i <= sum;i++) re = (re + dp[n][i])%M; cout << "Case " << cas++<< ": " << re << "\n"; } return 0; }