CodeForces - 140E:New Year Garland (组合数&&DP)
As Gerald, Alexander, Sergey and Gennady are already busy with the usual New Year chores, Edward hastily decorates the New Year Tree. And any decent New Year Tree must be decorated with a good garland. Edward has lamps of m colors and he wants to make a garland from them. That garland should represent a sequence whose length equals L. Edward's tree is n layers high and Edward plans to hang the garland so as to decorate the first layer with the first l1 lamps, the second layer — with the next l2 lamps and so on. The last n-th layer should be decorated with the last ln lamps,
Edward adores all sorts of math puzzles, so he suddenly wondered: how many different ways to assemble the garland are there given that the both following two conditions are met:
- Any two lamps that follow consecutively in the same layer should have different colors.
- The sets of used colors in every two neighbouring layers must be different. We consider unordered sets (not multisets), where every color occurs no more than once. So the number of lamps of particular color does not matter.
Help Edward find the answer to this nagging problem or else he won't manage to decorate the Tree by New Year. You may consider that Edward has an unlimited number of lamps of each of m colors and it is not obligatory to use all m colors. The garlands are considered different if they differ in at least one position when represented as sequences. Calculate the answer modulo p.
The first line contains three integers n, m and p (1 ≤ n, m ≤ 106, 2 ≤ p ≤ 109) which are the number of the tree's layers, the number of the lamps' colors and module correspondingly. The next line contains n integers li (1 ≤ li ≤ 5000, ).
Output
Print the only integer — the number of garlands modulo p.
Examples
3 2 1000
3 1 2
8
2 3 1000
2 2
24
1 1 1000
5
0
Note
In the first sample the following variants are possible: 121|1|12, 121|1|21, 121|2|12, 121|2|21, 212|1|12, 212|1|21, 212|2|12, 212|2|21. In the second sample the following variants are possible: 12|13, 12|23, 12|31, 12|32 and so on.
Figure for the first sample:
题意:给定一棵树,数有N层,你有M种颜色,让你求染色方案数,结果模P;需要满足同一层的相邻颜色不同,而且相邻层的颜色集合不能相同。
思路:由于P不一定是素数,我们不能出现除法。 所以需要加减法。
我们用f[i][j]表示只考虑一层,前面i个不同的点染j种相同的颜色的方案数。
联系第二类斯特林数f[i][j]=f[i-1][j-1]+f[i-1][j]*(j-1);注意到这里的颜色是没有标号的,而且没有排序。
dp[i][j]表示第i层用j种颜色发方案数。那么dp[i][j]=Σdp[i-1][k]*A(M,k)-dp[i-1][j]*FAC(j);
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
const int maxm=;
int f[maxn][maxn],sum[maxm],A[maxn],F[maxn];
int dp[][maxn],a[maxm];
int main()
{
int N,M,P,x;
scanf("%d%d%d",&N,&M,&P);
f[][]=;
rep(i,,) rep(j,,i)
f[i][j]=(1LL*f[i-][j]*(j-)%P+f[i-][j-])%P;
//按照第二类斯特林数的理解,f[i][j]表示i个不同的下标,用j种相同的颜色染色。是没有标号的,所以最后要乘A。
sum[]=; A[]=; F[]=;
rep(i,,min(M,)) A[i]=1LL*A[i-]*(M+-i)%P;
rep(i,,min(M,)) F[i]=1LL*F[i-]*i%P;
rep(i,,N) {
scanf("%d",&a[i]);
rep(j,,min(a[i],M)){
dp[i&][j]=1LL*f[a[i]][j]*A[j]%P*sum[i-]%P; //由于f是未标号的,A
if(a[i-]>=j) (((dp[i&][j]-=1LL*F[j]*f[a[i]][j]%P*dp[(i&)^][j]%P)%=P)+=P)%=P; //dp的已经标号了的,所以颜色是固定了的
//只需要排列第i行的j种颜色
(sum[i]+=dp[i&][j])%=P;
}
}
printf("%d\n",sum[N]);
return ;
}
CodeForces - 140E:New Year Garland (组合数&&DP)的更多相关文章
- Codeforces 140E(排列组合、dp)
要点 主要学到的东西:一个序列染色,相邻不染同色,恰用\(j\)种颜色的1.模式数.2.方案数.3.具体染色数. 从大的思路上来讲:先dp预处理出每一层的模式数:\(f[i][j]\)表示\(i\)个 ...
- codeforces 140E.New Year Garland
传送门: 解题思路: 要求相邻两行小球颜色集合不同,并且限制行内小球相邻不同. 由此可得:每行小球排列都是独立与外界的, 所以答案应该是对于所有行的颜色集合分类,在将行内的答案乘到上面. 先考虑如何分 ...
- codeforces 341C Iahub and Permutations(组合数dp)
C. Iahub and Permutations time limit per test 1 second memory limit per test 256 megabytes input sta ...
- 【bzoj4517】[Sdoi2016]排列计数 组合数+dp
题目描述 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列恰好有 m 个数是稳定的 满足条 ...
- [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)
[Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...
- [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】
[CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...
- [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT)
[Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT) 题面 给出一个\(n\)个点\(m\)条边的有向图(可能有环),走每条边需要支付一个价格\(c_i ...
- Codeforces 1108D - Diverse Garland - [简单DP]
题目链接:http://codeforces.com/problemset/problem/1108/D time limit per test 1 secondmemory limit per te ...
- CodeForces 146E Lucky Subsequence(组合数+DP)
题目描述 Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers w ...
随机推荐
- Qt5.3.2openglVS2010_QSqlField_字段类型
1.本来想通过 QSqlField::typeID() 来找字段类型,但是没找到... 然而看到了 SQL_INTEGER.SQL_SMALLINT等的使用(在“static QVariant::Ty ...
- Map<K, V> 中k,v如果为null就转换
Set<String> set = map.keySet(); if(set != null && !set.isEmpty()) { for(String key : s ...
- js事件轮询机制
console.log(1) setTimeout(function(){ console.log(2) },0); console.log(3) 毫无疑问:运行结果是1 3 2 也就是说:setTi ...
- Python BeautifulSoup的使用
2017-07-24 22:39:14 Python3 中的beautifulsoup引入的包是bs4 import requests from bs4 import * r = requests.g ...
- apache安装方式
1.首先需要下载apachi,apache_2.2.22.msi 2.双击安装包进行安装,安装过程中可能出现一些选择性问题,将重点部分截图如下: 说明:这三个内容就按如上输入即可. 说明:选择自定义安 ...
- ASCII码、ISO8859-1、Unicode、GBK和UTF-8 的区别
为什么需要编码? 计算机中最小的存储单位是字节(byte),一个字节所能表示的字符数又有限,1byte=8bit,一个字节最多也只能表示255个字符,而世界上的语种又多,都有各种不同的字符,无法用一个 ...
- protected 与 internal
protected:在当前类的“内部” 和 派生子类的“内部” 可访问(注意:实例对象不可访问 或者说 访问不到):如果静态,则在当前类内部和派生子类内部 具有“全局效果” internal:在 ...
- nyoj737石子合并(一)
先得出区间为1和2时的结果.用arr[i][j]记录i,j内的和.dp[i][j]记录i,j区间全加起来的最小花费.那么区间大小为1和2时都是明显的.为3时枚举断点.其中一个区间大小为1也是可行的. ...
- Spring+Mybatis+Dubbo报错java.lang.reflect.MalformedParameterizedTypeException
原因是spring的jar文件冲突,排除spring的文件即可 <dependency> <groupId>com.alibaba</groupId> <ar ...
- asp.net GridView多行表头的实现,合并表头
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == Da ...