标签:des style blog http color io os ar java
DP: dp[i][j]前i堆排出j长度的序列有多少种排法,
dp[i][j]=dp[i-1][j] (不用第i堆),
dp[i][j]+=dp[i-1][j-k]*C[j][k] (用第i堆的k个石头)
3 1 1 1 2 1 2
Case 1: 15 Case 2: 8HintIn 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.
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long int LL; const LL MOD = 1000000007LL; LL C[11000][210]; void getC() { for(int i=0;i<11000;i++) C[i][0]=C[i][i]=1; for(int i=2;i<11000;i++) for(int j=1;j<i&&j<=200;j++) C[i][j]=(C[i-1][j]+C[i-1][j-1])%MOD; } LL dp[110][11000]; int n,a[110]; int main() { getC(); int cas=1; while(scanf("%d",&n)!=EOF) { for(int i=1;i<=n;i++) scanf("%d",a+i); memset(dp,0,sizeof(dp)); int len=0; dp[0][0]=1; for(int i=1;i<=n;i++) { len+=a[i]; for(int j=0;j<=len;j++) { dp[i][j]=(dp[i][j]+dp[i-1][j])%MOD; for(int k=1;k<=a[i]&&j-k>=0;k++) { dp[i][j]=(dp[i][j]+dp[i-1][j-k]*C[j][k])%MOD; } } } LL ans=0; for(int i=1;i<=len;i++) ans=(ans+dp[n][i])%MOD; printf("Case %d: %lld\n",cas++,ans%MOD); } return 0; }
HDOJ 4248 A Famous Stone Collector DP
标签:des style blog http color io os ar java
原文地址:http://blog.csdn.net/ck_boss/article/details/40209735