hdu 4248 A Famous Stone Collector

首先发现一个很头痛的问题,下面是2个求排列组合的代码

1  memset(C,0,sizeof(C));
2     for(int i=0;i<10010;i++)
3     {
4         C[i][0]=1;
5         for(int j=1;j<=100;j++)
6             C[i][j]=(C[i-1][j]+C[i-1][j-1])%MOD;
7     }
1 C[0][0]=1;
2     for(int i=1;i<10010;i++)
3         for(int j=0;j<=100;j++)
4            if(j==0) C[i][j]=C[i-1][j];
5            else C[i][j]=(C[i-1][j]+C[i-1][j-1])%MOD;

其中第一个是刘汝佳上面的代码,不知为什么在杭电OJ上就是WA,第二个就能AC,就路过的大神指点

题目大意

给你一些不同颜色的石头,问选出一些石头排成一排总共有多少种不同排法,不同数量的石头视为不同情况,每个位置上的石头颜色都相同视为相同情况。

分析 

dp+排列组合

dp[i][j] 表示前i堆石子构成长度为j的串的方案数;

状态转移方程是:k为i堆使用的数量

dp[i][j] = (dp[i][j] + dp[i-1][j-k] * C[j][k]%MOD)%MOD;

可以这样想

 dp[ i ][ j ] = dp[ i-1 ][ j ]; //未放入第i种颜色的石头

 dp[ i ][ j ] += dp[ i-1 ][ j - k ] * C[ j ][ k ];  //放入k个第i种颜色的石 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #define MOD 1000000007
 5 using namespace std;
 6 typedef long long LL;
 7 LL C[10010][110];
 8 void get_c()
 9 {
10     memset(C,0,sizeof(C));
11     for(int i=0;i<10010;i++)
12     {
13         C[i][0]=1;
14         for(int j=1;j<=100;j++)
15             C[i][j]=(C[i-1][j]+C[i-1][j-1])%MOD;
16     }//这一步是错的,不知为啥
17
18
19     C[0][0]=1;
20     for(int i=1;i<10010;i++)
21         for(int j=0;j<=100;j++)
22            if(j==0) C[i][j]=C[i-1][j];
23            else C[i][j]=(C[i-1][j]+C[i-1][j-1])%MOD;
24 }
25 LL dp[110][10010];
26 int main()
27 {
28     int cas=0,sum,n,num;
29     LL ans;
30     get_c();
31     while(scanf("%d",&n)!=EOF)
32     {
33         memset(dp,0,sizeof(dp));
34         dp[0][0]=1;
35         sum=0;
36         for(int i=1;i<=n;i++)
37         {
38             scanf("%d",&num);
39             sum+=num;
40             for(int k=0;k<=num;k++)
41             {
42                 for(int j=k;j<=sum;j++)
43                     dp[i][j]=(dp[i][j]+(dp[i-1][j-k]*C[j][k])%MOD)%MOD;
44             }
45         }
46         ans=0;
47         for(int i=1;i<=sum;i++)
48             ans=(ans+dp[n][i])%MOD;
49         printf("Case %d: %I64d\n",++cas,ans);
50     }
51     return 0;
52 }
时间: 2024-10-01 02:51:38

hdu 4248 A Famous Stone Collector的相关文章

[ACM] hdu 4248 A Famous Stone Collector (DP+组合)

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 li

HDU 4248 A Famous Stone Collector(DP + 组合数)

题目链接:点击打开链接 思路:DP + 组合数. 用d[i][j]表示前第i种颜色的石头, 已经用了j个的方法数, 每次枚举第i种石头放多少个, 假设放k个, 那么相当于从j个位置中选k个, 预处理组合数就行了. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> #include<vector&

HDOJ 4248 A Famous Stone Collector DP

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个石头) A Famous Stone Collector Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 845    Accepted Su

hdu 4248

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

hdu 1115 Lifting the Stone (数学几何)

Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5203    Accepted Submission(s): 2155 Problem Description There are many secret openings in the floor which are covered by a big

hdu 5996 dingyeye loves stone(博弈)

题目链接:hdu 5996 dingyeye loves stone 题意: 给你一棵树,树的每一个节点有a[i]个石子,每个人可以将这个节点的石子移向它的父亲,如果没有合法操作,那么就算输,现在给你当前的局面,问你能否赢 题解: 设根节点的深度为0,将所有深度为奇数的节点的石子数目xor起来,则先手必胜当且仅当这个xor和不为0. 证明同阶梯博弈.对于偶深度的点上的石子,若对手移动它们,则可模仿操作:对于奇深度上的石子,移动一次即进入偶深度的点. 时空复杂度O(n). 1 #include<b

HDU 4256 The Famous Clock

The Famous Clock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1399    Accepted Submission(s): 940 Problem Description Mr. B, Mr. G and Mr. M are now in Warsaw, Poland, for the 2012's ACM-ICPC

hdu 4255 A Famous Grid

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4255 A Famous Grid Description Mr. B has recently discovered the grid named "spiral grid".Construct the grid like the following figure. (The grid is actually infinite. The figure is only a small par

HDU 4253 Two Famous Companies

Two Famous Companies Time Limit: 15000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 425364-bit integer IO format: %I64d      Java class name: Main In China, there are two companies offering the Internet service for the peo