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 ...
随机推荐
- [原][库][c++]tinyxml使用小结
参考:http://blog.csdn.net/L_Andy/article/details/40615517 tinyxml官网: http://www.grinninglizard.com/tin ...
- js匿名函数和闭包总结
js匿名函数和闭包总结 一.总结 一句话总结:匿名函数的最主要作用是创建闭包,闭包就是将函数内部和函数外部连接起来的一座桥梁.内层的函数可以使用外层函数的所有变量,即使外层函数已经执行完毕.闭包可以用 ...
- Pollywog CodeForces - 917C (状压)
链接 大意: 一共n个格子, 初始$x$只蝌蚪在前$x$个格子, 每次最左侧的蝌蚪向前跳, 跳跃距离在范围[1,k], 并且每只蝌蚪跳跃都有一定花费, 有$q$个格子上有石头, 若有蝌蚪跳到某块石头上 ...
- 部署Spring web项目遇到的问题及解决方案
非常悲伤的一个提示: 错误源码: Caused by: java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExce ...
- 『科学计算_理论』优化算法:梯度下降法&牛顿法
梯度下降法 梯度下降法用来求解目标函数的极值.这个极值是给定模型给定数据之后在参数空间中搜索找到的.迭代过程为: 可以看出,梯度下降法更新参数的方式为目标函数在当前参数取值下的梯度值,前面再加上一个步 ...
- hdu-2897-巴什博弈
邂逅明下 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- FREETEXTBOX
本文转自http://blog.csdn.net/JOHNCOOLS/archive/2006/04/08/655553.aspx感谢作者们的付出---------------版本: FreeText ...
- 如何使用Java Enum
简单的用法:JavaEnum简单的用法一般用于代表一组常用常量,可用来代表一类相同类型的常量值.如: 性别: public enum SexEnum { male, female; } 颜色: pub ...
- forget word out a~2
1● an 不,非,无 2● amphi 两个,两种 3● ad 做,加强:
- POJ 2109 巧妙解法
Int最大是10^9.所以一般思路是二分+高精度.但是double 范围是10^(-307)-10^308所以可以用double型.k^n=p.所以有k=p^(1/n). 见代码: #include& ...