HDU 4248 A Famous Stone Collector 组合数学dp ****
A Famous Stone Collector
Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 793 Accepted Submission(s): 292
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.
available stones of each color respectively. All the input numbers will be nonnegative and no more than 100.
which is a prime number.
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.
转台转移方程:
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<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef __int64 LL;
const LL mod=; LL a[];
LL cnm[][];
LL dp[][];
void Init()
{
LL i,j;
for(i=;i<=;i++)
cnm[i][]=;
for(i=;i<=;i++)
{
for(j=;j<=i&&j<=;j++)
{
if(i==j) cnm[i][j]=;
else cnm[i][j]=( cnm[i-][j]+cnm[i-][j-] )%mod;
}
}
}
void solve(LL n)
{
LL i,j,s,Sum=,k;
memset(dp,,sizeof(dp));
for(i=;i<=;i++)
dp[i][]=;
for(i=,s=;i<=n;i++)
{
s=s+a[i];
for(j=;j<=s;j++)
{
dp[i][j]=dp[i-][j];
for(k=;k<=a[i];k++)
if(k<=j)
dp[i][j]=( dp[i][j]+(cnm[j][k]*dp[i-][j-k])%mod )%mod;
if(i==n) Sum=(Sum+dp[i][j])%mod;
}
}
printf("%I64d\n",Sum);
}
int main()
{
LL T=;
LL i,n;
Init();
while(scanf("%I64d",&n)>)
{
for(i=;i<=n;i++)
scanf("%I64d",&a[i]);
printf("Case %I64d: ",++T);
solve(n);
}
return ;
}
HDU 4248 A Famous Stone Collector 组合数学dp ****的更多相关文章
- [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 c ...
- hdu 4248 A Famous Stone Collector
首先发现一个很头痛的问题,下面是2个求排列组合的代码 memset(C,,sizeof(C)); ;i<;i++) { C[i][]=; ;j<=;j++) C[i][j]=(C[i-][ ...
- 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个石头) ...
- HDU 4249 A Famous Equation(数位DP)
题目链接:点击打开链接 思路:用d[i][a][b][c][is]表示当前到了第i位, 三个数的i位各自是a,b,c, 是否有进位 , 的方法数. 细节參见代码: #include<cstdio ...
- HDU 3016 Man Down (线段树+dp)
HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- hdu 5025 Saving Tang Monk 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...
- hdu 5996 dingyeye loves stone(博弈)
题目链接:hdu 5996 dingyeye loves stone 题意: 给你一棵树,树的每一个节点有a[i]个石子,每个人可以将这个节点的石子移向它的父亲,如果没有合法操作,那么就算输,现在给你 ...
- HDU 3341 Lost's revenge AC自动机+dp
Lost's revenge Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)T ...
- HDU 2457 DNA repair(AC自动机+DP)题解
题意:给你几个模式串,问你主串最少改几个字符能够使主串不包含模式串 思路:从昨天中午开始研究,研究到现在终于看懂了.既然是多模匹配,我们是要用到AC自动机的.我们把主串放到AC自动机上跑,并保证不出现 ...
随机推荐
- SpringMvc拦截器运行原理。
首先,先简单的说一下怎么配置SpringMvc的拦截器. 分两步,第一步先定义一个类,实现HandlerInterceptor接口. import javax.servlet.http.HttpSer ...
- 新版 iPad Pro 弯了,苹果表示这是正常现象……
简评:苹果上个月才发布的新版 iPad Pro 出问题了.有用户抱怨说,iPad 出现了机身弯曲.然而苹果公司认为这并不会影响性能,所以坚持这不是一个缺陷,emmm-- 虽然苹果公司的品控一直为人称道 ...
- C++与C的区别二
1. new,delete的局部重载: #include <iostream> using namespace std; ; class myclass { public: myclass ...
- java项目迁移
电脑重装系统以后或者从不同MyEclipse版本迁移项目时候会出现: Project facet Java 1.5 is not supported by target runtime Apache ...
- 总结day6 ---- set集合,基本类型的相互转化,编码,数据类型总结,循环时候不要动列表或者字典,深浅copy
python小数据池,代码块的最详细.深入剖析 一. id is == 二. 代码块 三. 小数据池 四. 总结 一,id,is,== 在Python中,id是什么?id是内存地址,比如你利用id ...
- Laravel5.5 引入并使用第三方类库操作
理论上,Laravel5系列都支持,各位可以一试.我这里使用5.5版本. 我这里引入了一个将汉字转化为拼音的类库测试,一起来看看吧! 首先,在laravel的app目录下自定义一个文件夹,我用的名字是 ...
- docker入门之基础操作
docker,我们可以把docker当作是简单的虚拟机.但这个虚拟机不像vm一样安装系统.所以我们又称之为容器.你可以理解成,容器就是虚拟机 docker与vm的对比 vmware:下载镜像——安装系 ...
- Go语言目录
为什么学习Go语言 第一章 环境搭建 Windows搭建Go语言环境 第二章 Go语言基础 Go语言介绍 Go语言命名 Go语言内置类型和函数 Go语言特殊函数介绍 Go语言运算符 第三章 Go语言程 ...
- Go语言指针
指针简介 (Pointer)是编程语言中的一个对象,利用地址,它的值直接指向(points to)存在电脑存储器中另一个地方的值.由于通过地址能找到所需的变量单元,可以说,地址指向该变量单元.因此,将 ...
- ajax防止表单自动提交
重写表单的checkForm方法,并用if和else解决异步判断的问题. function checkForm(){ 1 var flag = false; $.ajaxSetup({async : ...