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:

  1. Any two lamps that follow consecutively in the same layer should have different colors.
  2. 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.

Input

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

Input
3 2 1000
3 1 2
Output
8
Input
2 3 1000
2 2
Output
24
Input
1 1 1000
5
Output
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)的更多相关文章

  1. Codeforces 140E(排列组合、dp)

    要点 主要学到的东西:一个序列染色,相邻不染同色,恰用\(j\)种颜色的1.模式数.2.方案数.3.具体染色数. 从大的思路上来讲:先dp预处理出每一层的模式数:\(f[i][j]\)表示\(i\)个 ...

  2. codeforces 140E.New Year Garland

    传送门: 解题思路: 要求相邻两行小球颜色集合不同,并且限制行内小球相邻不同. 由此可得:每行小球排列都是独立与外界的, 所以答案应该是对于所有行的颜色集合分类,在将行内的答案乘到上面. 先考虑如何分 ...

  3. codeforces 341C Iahub and Permutations(组合数dp)

    C. Iahub and Permutations time limit per test 1 second memory limit per test 256 megabytes input sta ...

  4. 【bzoj4517】[Sdoi2016]排列计数 组合数+dp

    题目描述 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列恰好有 m 个数是稳定的 满足条 ...

  5. [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)

    [Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...

  6. [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】

    [CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...

  7. [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT)

    [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT) 题面 给出一个\(n\)个点\(m\)条边的有向图(可能有环),走每条边需要支付一个价格\(c_i ...

  8. Codeforces 1108D - Diverse Garland - [简单DP]

    题目链接:http://codeforces.com/problemset/problem/1108/D time limit per test 1 secondmemory limit per te ...

  9. CodeForces 146E Lucky Subsequence(组合数+DP)

    题目描述 Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers w ...

随机推荐

  1. 简说Spring事务

    一.事务定义: 事务指逻辑上的一组操作,这组操作要么全部成功,要么全部失败. 二.事务的特性: 1. 原子性 - 指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不发生. 2. 一致性 ...

  2. windows 启用ipv6(for XX-net)补充“Ping请求找不到主机”问题

    ipv6.google.com 有点地方直接能启用,有的时候不行. 导致在xx - net 里无法启用ipv6 这样搞试试 netsh int ipv6 isatap set state enable ...

  3. Codeforces 757B - Bash's Big Day(分解因子+hashing)

    757B - Bash's Big Day 思路:筛法.将所有因子个数求出,答案就是最大的因子个数,注意全为1的特殊情况. 代码: #include<bits/stdc++.h> usin ...

  4. windows批处理命令

    前言 批处理文件(batch file)包含一系列 DOS命令,通常用于自动执行重复性任务.用户只需双击批处理文件便可执行任务,而无需重复输入相同指令.编写批处理文件非常简单,但难点在于确保一切按顺序 ...

  5. MyBatis3-基于注解的示例

    在基于注解的示例中,可以简化编写XML的过程,全部采用注解方式进行编写,并在注解上写SQL语句,语句和XML的语句保持一致,并且可以省略掉XML文件不用引入的好处.但还有一点,基于注解的方式还没有百分 ...

  6. Selenium之Action Chains类

    Action Chains类常用于模拟鼠标的行为,比如单击,双击,拖拽等行为,使用下面的方法导入Action Chains类 from selenium.webdriver.common.action ...

  7. Yii 语言设置 中文提示信息

    1.  在main.php配置文件中加入 'language'=>'zh_cn', 注: 在URL中追加参数lang=zh_cn即可实现中文 2.  在Controller方法中添加 publi ...

  8. Weird journey CodeForces - 788B (路径计数)

    大意:$n$结点$m$条边无向图, 满足 $(1)$经过$m-2$条边$2$次 $(2)$经过其余$2$条边$1$次 的路径为好路径, 求所有好路径数 相当于边加倍后再删除两条边, 求欧拉路条数 首先 ...

  9. OAF 小知识

    打开新窗口链接地址 1.link的动态使用 link组件有一个属性叫 Target Frame,设置为_blank就可以新开窗口 link组件默认是打开当前系统中的连接地址,如果要打开外部网页,可以在 ...

  10. 在EO中获取某字段基于表的列名

    //生成EO的时候自动生成的字段 public static final int BRIEFINTRO = 88; String[][] str = null; str = new String[][ ...