http://acm.hdu.edu.cn/showproblem.php?pid=4248
题解:
给定n种颜色的石头,每种颜色有si颗,同种颜色的石头不区分。问能构成多少种不同的石头序列(不同的序列是指:1.石头数不同;2.石头数相同,至少一个位置的石头颜色不同) 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 ]; 其中C[ n ][ m ]表示组合数。
#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #define ll long long #define mod 1000000007 using namespace std; long long dp[110][10010],s[110],c[10010][110],n; void init(long long n,long long m) { long long i,j; memset(c,0,sizeof(c)); for(i=0;i<=m;i++)c[0][i]=c[1][i]=1; for(i=0;i<=m;i++)c[i][i]=1; for(i=0;i<=n;i++)c[i][0]=1; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { if(i!=j) c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod; } } } void DP(){ memset(dp,0,sizeof(dp)); long long t=s[1],m; for(int i=1; i<=n; i++) dp[i][0]=1; for(int j=1; j<=s[1]; j++) dp[1][j]=1; for(int i=2; i<=n; i++){ t+=s[i]; for(int j=1; j<=t; j++){ m=min((ll)j,s[i]); dp[i][j]=dp[i-1][j]; for(int k=1; k<=m; k++){ dp[i][j]+=dp[i-1][j-k]*c[j][k]; dp[i][j]%=mod; } } } } int main() { init(10000,100); long long l=0,i,j,k,len,h,ans; while(scanf("%lld",&n)!=EOF) { l++; h=0; for(i=1;i<=n;i++){scanf("%lld",&s[i]);h+=s[i];}; DP(); ans=0; for(j=1;j<=h;j++) { ans+=dp[n][j]; ans%=mod; } printf("Case %lld: %lld\n",l,ans); //cout<<ans<<endl; } }