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. NGUI 中,长技能图标显示技能Tips的核心代码

    需要将技能图标对应的位置Pos赋给Tips即可.下面是计算 Pos 的核心代码: using UnityEngine; public class LgsTest : MonoBehaviour { [ ...

  2. 15分钟入门lua

    目录:[ - ] -- 1. Variables and flow control. -- 2. Functions. -- 3. Tables. -- 3.1 Metatables and meta ...

  3. codeforces 559b//Equivalent Strings// Codeforces Round #313(Div. 1)

    题意:定义了字符串的相等,问两串是否相等. 卡了时间,空间,不能新建字符串,否则会卡. #pragma comment(linker,"/STACK:1024000000,102400000 ...

  4. 『PyTorch』第十一弹_torch.optim优化器

    一.简化前馈网络LeNet import torch as t class LeNet(t.nn.Module): def __init__(self): super(LeNet, self).__i ...

  5. 『Scrapy』爬取腾讯招聘网站

    分析爬取对象 初始网址, http://hr.tencent.com/position.php?@start=0&start=0#a (可选)由于含有多页数据,我们可以查看一下这些网址有什么相 ...

  6. from * import *(ImportError: No module named *)为什么报错没有这个目录

    先说下from * import * 的原理:比如有路径D:\fanbingbing\ai\wo.py这么一个文件,而现在你在D:\fanbingbing\buai\ni.py(别介意这么比喻..(⊙ ...

  7. ccfZ字形扫描

    问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan).给定一个n×n的矩阵,Z字形扫描的过程如下图所示: 对于下面的4×4的矩阵, 1 5 3 9 3 7 5 ...

  8. 万恶的deferred_segment_creation(延迟块分配)

    11gR2开始,空表是不分配segment的.由于没有分配 segment, EXP默认不能导出空表,user_objects有该对象但是 user_segment没有该对象

  9. Oracle解析复杂json的方法(转)

    转:Oracle解析复杂json的方法 问题背景: 当前在Oracle数据库(11G之前的版本)解析json没有可以直接使用的系统方法,网上流传的PLSQL脚本大多也只可以解析结构较单一的json串, ...

  10. length,lengthb,substr,substrb,instr小用

    --字符串的字符长度 select length('wm.dfw.士农工商.sda.人马ss.dfw.4.sdf.332.sf.qq.sd') from dual; --字符串的字节长度 select ...