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 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

题意为:

有n种颜色的石头,每种颜色有num[i]块,每种颜色的石头是不可区分的,在这么多石头里面挑选石头来排成一条线,问一共同拥有多少种排法。线的长度为(1到  num[i]+num[2]+num[3]+.....num[n]。

比方有三种颜色石头,B,G,M,表示颜色。每种颜色一块石头,那么可能的序列有 B; G; M; BG; BM; GM; GB; MB; MG; BGM; BMG; GBM; GMB; MBG; MGB

用dp[i][j]代表用前i种颜色的石头去排成长度为j的序列

那么有那种情况:

①第i种颜色的石头不使用,那么 dp[i][j] = dp[i-1][j];

②第i种颜色的石头使用(序列可到达长度为原来的长度加上第i种颜色的个数),有num[i]个。原来的石头序列中去掉k个,从第i种颜色石头中取出k个。这里(k<=j),插入到原来的序列中,一共同拥有j个位置,从中挑选k个就能够了(不是单纯的插空。能够理解为,要构成长度j的序列,有j个位置,首先让第i种颜色的k个石头去挑k个位置。这就包含了相邻和不相邻的情况,因为同样颜色不可区分,有
c[j][k]种方法,然后再让剩下的石头序列依照原来的顺序依次填入每一个空位置就能够了).

有dp[i][j]+=dp[i-1][j-k]*c[j][k];

代码:

#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
typedef long long ll;
const int maxn=110;//种类数最大
const int mod=1000000007;
ll dp[maxn][maxn*maxn];//代表用前i种颜色组成长度为j的序列的方法数
ll c[maxn*maxn][maxn];//从当前序列长度+1种中选出j种。j不会超过maxn
int n;//n种颜色
int num[maxn];//每种颜色有多少石头
int sum;//最长的序列长度 void getC()
{
c[0][0]=c[1][0]=c[1][1]=1;
for(int i=2;i<maxn*maxn-1;i++)
{
c[i][0]=1;
for(int j=1;j<maxn-1;j++)
{
if(i==j)
c[i][j]=1;
else
{
c[i][j]=c[i-1][j]+c[i-1][j-1];
c[i][j]%=mod;
}
}
}
} int DP()
{
memset(dp,0,sizeof(dp));//一定的初始化为0。后面dp[i][j]=dp[i-1][j]用到了..
for(int i=1;i<=n;i++)
dp[i][0]=1;
for(int i=1;i<=num[1];i++)
dp[1][i]=1;
int tsum=num[1];
for(int i=2;i<=n;i++)//一共同拥有N种
{
tsum+=num[i];//前i种颜色的总长度,最多的
for(int j=1;j<=tsum;j++)
{
dp[i][j]=dp[i-1][j];//对于当前长度j,一种情况是不用第i种颜色的石头
for(int k=1;k<=num[i]&&k<=j;k++)//使用当前颜色石头,可是要去掉一定的长度,使用新的颜色石头来填充
{
dp[i][j]+=dp[i-1][j-k]*c[j][k];//为什么是 c[j][k]呢,能够这样理解。一共同拥有J个位置。让第i中颜色的求先去选当中k个位置。然后剩下的位置让原来颜色的顺序放下就好了
dp[i][j]%=mod;
}
}
}
ll ans=0;
for(int i=1;i<=sum;i++)
{
ans+=dp[n][i];
ans%=mod;
}
return ans;
} int main()
{
getC();
int cas=1;
while(cin>>n)
{
sum=0;
for(int i=1;i<=n;i++)
{
cin>>num[i];
sum+=num[i];
}
cout<<"Case "<<cas++<<": "<<DP()<<endl;
}
return 0;
}

[ACM] hdu 4248 A Famous Stone Collector (DP+组合)的更多相关文章

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

    A Famous Stone Collector Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  2. 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个石头) ...

  3. hdu 4248 A Famous Stone Collector

    首先发现一个很头痛的问题,下面是2个求排列组合的代码 memset(C,,sizeof(C)); ;i<;i++) { C[i][]=; ;j<=;j++) C[i][j]=(C[i-][ ...

  4. HDU 4294 A Famous Equation(DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4249 题目大意:给一个a+b=c的表达式,但是a.b.c中部分位的数字丢失,并用?代替,问有多少种方案 ...

  5. ACM: HDU 1028 Working out 解题报告-DP

    Working out time limit per test  2 seconds memory limit per test  256 megabytes input  standard inpu ...

  6. [ACM] HDU 1227 Fast Food (经典Dp)

    Fast Food Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  7. ACM HDU Bone Collector 01背包

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 这是做的第一道01背包的题目.题目的大意是有n个物品,体积为v的背包.不断的放入物品,当然物品有 ...

  8. HDOJ(HDU).2602 Bone Collector (DP 01背包)

    HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...

  9. HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences             ...

随机推荐

  1. 循环语句第3种 FOR ... in ... LOOP END LOOP;

    --------第3种--------  FOR ... in ... LOOP  END LOOP;    BEGIN    FOR i IN 1..10 LOOP      dbms_output ...

  2. codeforces7D Palindrome Degree(manacher&amp;dp或Hsh&amp;dp)

    D. Palindrome Degree time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. m_Orchestrate learning system---十五、如何快速查错

    m_Orchestrate learning system---十五.如何快速查错 一.总结 一句话总结: a.删除代码法 b.添加提示代码法 c.仔细看错误信息 1.评论板块和论坛板块的实时更新? ...

  4. 整数转罗马数字 C++实现 java实现 leetcode系列(十二)

    罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并 ...

  5. Elasticsearch之重要核心概念(cluster(集群)、shards(分配)、replicas(索引副本)、recovery(据恢复或叫数据重新分布)、gateway(es索引的持久化存储方式)、discovery.zen(es的自动发现节点机制机制)、Transport(内部节点或集群与客户端的交互方式)、settings(修改索引库默认配置)和mappings)

    Elasticsearch之重要核心概念如下: 1.cluster 代表一个集群,集群中有多个节点,其中有一个为主节点,这个主节点是可以通过选举产生的,主从节点是对于集群内部来说的.es的一个概念就是 ...

  6. Weblogic安装配置教程

    一.WebLogic的介绍    WebLogic是美国bea公司出品的一个application server,确切的说是一个基于Javaee架构的中间件,纯java开发的,最新版本WebLogic ...

  7. HD-ACM算法专攻系列(9)——大菲波数

    题目描述: 源码: 运用Java大数求解. import java.math.BigInteger; import java.util.*; public class Main { //主函数 pub ...

  8. java.lang.NoClassDefFoundError: org/springframework/dao/support/PersistenceE解决方法

    笔者是使用spring4.0时,报的错误: 原因是没有引入spring-tx-4.0.0.RELEASE.jar包,將spring-tx-4.0.0.RELEASE.jar添加到build path中 ...

  9. POJ 3320 Jessica's Reading Problem (尺取法,时间复杂度O(n logn))

    题目: 解法:定义左索引和右索引 1.先让右索引往右移,直到得到所有知识点为止: 2.然后让左索引向右移,直到刚刚能够得到所有知识点: 3.用右索引减去左索引更新答案,因为这是满足要求的子串. 4.不 ...

  10. 【参考】.class文件的JDK编译版本查看

    使用 UltraEdit  打开 .class 文件,第一行内容: 00000000h: CA FE BA BE 00 00 00 32 00 A9 07 00 02 01 00 37 ; 漱壕... ...