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 ...
随机推荐
- arcgis for silverlight 地图放大到某个点或者几何对象
http://blog.csdn.net/xuan444150/article/details/7727866 分类: silverlight王国 GIS王国 2012-07-09 08:50 1 ...
- [NOI2018]你的名字(68pts)
SAM真让人头秃. 题面 https://www.luogu.org/problemnew/show/P4770 首先考虑 l=1,r=∣S∣的做法 如果对于ION2018的子串不用判重的话,对ION ...
- Android 7.0真实上手体验
Android 7.0真实上手体验 Android 7.0的首个开发者预览版发布了,支持的设备只有Nexus6.Nexus 5X.Nexus 6P.Nexus 9.Nexus Player.Pixel ...
- POJ-1753 Flip Game (BFS+状态压缩)
Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of i ...
- httpclient妙用一 httpclient作为客户端调用soap webservice(转)
前面有一篇使用HttpClient调用带参数的post接口方法,这里找到一篇使用HttpClient调用Soap协议接口的方式. 原文地址:httpclient妙用一 httpclient作为客户端调 ...
- 068——VUE中vuex的使用场景分析与state购物车实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- apache-service的使用
apache service目录设置 设置Apache HTTP Server的文件根目录(DocumentRoot) 安装Apache 时,系统会给定一个缺省的文件根目录 如果你觉得这个网页存在这个 ...
- linux processes
So that Linux can manage the processes in the system, each process is represented by a task_struct ...
- Flask初级(七)flash模板循环,判断
Project name :Flask_Plan templates:templates static:static 继续前面的代码 修改Flask_Plan.py @app.route('/') d ...
- DevExpress v17.2最新版帮助文档下载大全
DevExpress v17.2.4帮助文档下载列表大全来啦!包含.NET.VCL.HTML/JS系列所有帮助文档,提供CHM和PDF两个版本.除已停止更新的Silverlight.Windows 8 ...