Description

用$m$种颜色的彩球装点$n$层的圣诞树。圣诞树的第$i$层恰由$l[i]$个彩球串成一行,且同一层内的相邻彩球颜色不同,同时相邻两层所使用彩球的颜色集合不同。

求有多少种装点方案,答案对$p$取模。

只要任一位置上的彩球颜色不同,就算作不同的方案。

Input

第一行三个整数$n,m,p$,表示圣诞树的层数、彩球的颜色数和取模的数。

接下来一行包含$n$个整数,表示$l[i]$。

Output

一个整数表示答案。

Sample Input

3 2 1000

3 1 2

Sample Output

8

HINT

$1\;\leq\;n,m\;\leq\;10^6,2\;\leq\;p\;\leq\;10^9,1\;\leq\;l[i]\;\leq\;5000,\sum\;l[i]\;\leq\;10^7$

Solution

先考虑单行的情况,$f[i][j]$表示前$i$个位置用了$j$种颜色的方案.

$f[i][j]=f[i-1][j-1]+f[i-1][j]\;\times\;(j-1)$

$g[i][j]$表示第$i$行有$j$种颜色的方案数.

$g[i][j]=f[l[i]][j]\;\times\;(\sum\;g[i-1][k]\;\times\;P_{m}^{j}-g[i-1][j]\;\times\;P_{j}^{j})$.

#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define M 5005
#define N 1000005
using namespace std;
typedef long long ll;
int l[N],n,m;
ll f[M][M],g[2][M],fa[M],fac[M],p,sum;
inline int read(){
int ret=0;char fa=getchar();
while(!isdigit(fa))
fa=getchar();
while(isdigit(fa)){
ret=(ret<<1)+(ret<<3)+fa-'0';
fa=getchar();
}
return ret;
}
inline void Aireen(){
n=read();m=read();p=(ll)(read());
for(int i=1;i<=n;++i)
l[i]=read();
fa[0]=fac[0]=1ll;
for(int i=1,j=m;i<M&&j;++i,--j){
fa[i]=fa[i-1]*(ll)(i)%p;
fac[i]=fac[i-1]*(ll)(j)%p;
}
//长度为i的情况
f[0][0]=1ll;
for(int i=1;i<M;++i)
for(int j=min(m,i);j;--j)
f[i][j]=(f[i-1][j-1]+f[i-1][j]*(ll)(j-1)%p)%p;
for(int i=1;i<=n;++i){
for(int j=1;j<=l[i+1];++j)
g[i&1][j]=0ll;
if(i==1){
for(int j=min(l[i],m);j;--j)
g[i][j]=fac[j]*f[l[i]][j]%p;
}
else{
sum=0ll;
for(int j=min(l[i-1],m);j;--j)
sum=(sum+g[i&1^1][j])%p;
for(int j=min(l[i],m);j;--j){
g[i&1][j]=(fac[j]*sum%p-fa[j]*g[i&1^1][j]%p+p)%p*f[l[i]][j]%p;
}
}
}
sum=0ll;
for(int j=min(l[n],m);j;--j)
sum=(sum+g[n&1][j])%p;
printf("%I64d\n",sum);
}
int main(){
freopen("christmas.in","r",stdin);
freopen("christmas.out","w",stdout);
Aireen();
fclose(stdin);
fclose(stdout);
return 0;
}

[cf140e]New Year Garland的更多相关文章

  1. CF140E New Year Garland (计数问题)

    用$m$种颜色的彩球装点$n$层的圣诞树.圣诞树的第$i$层恰由$a_{i}$个彩球串成一行,且同一层内的相邻彩球颜色不同,同时相邻两层所使用彩球的颜色集合不 同.求有多少种装点方案,答案对$p$取模 ...

  2. POJ 1759 Garland(二分+数学递归+坑精度)

    POJ 1759 Garland  这个题wa了27次,忘了用一个数来储存f[n-1],每次由于二分都会改变f[n-1]的值,得到的有的值不精确,直接输出f[n-1]肯定有问题. 这个题用c++交可以 ...

  3. poj 1759 Garland (二分搜索之其他)

    Description The New Year garland consists of N lamps attached to a common wire that hangs down on th ...

  4. poj 1759 Garland

    Garland Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2365   Accepted: 1007 Descripti ...

  5. C. Nice Garland Codeforces Round #535 (Div. 3) 思维题

    C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  6. D. Diverse Garland Codeforces Round #535 (Div. 3) 暴力枚举+贪心

    D. Diverse Garland time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  7. Diverse Garland CodeForces - 1108D (贪心+暴力枚举)

    You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...

  8. Nice Garland CodeForces - 1108C (思维+暴力)

    You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...

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

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

随机推荐

  1. 【BZOJ 1001】[BeiJing2006]狼抓兔子

    Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的,而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一个 ...

  2. Android-完全退出当前应用程序的四种方法

    Android程序有很多Activity,比如说主窗口A,调用了子窗口B,如果在B中直接finish(), 接下里显示的是A.在B中如何关闭整个Android应用程序呢?本人总结了几种比较简单的实现方 ...

  3. 利用windbg查找dictionary导致IIS占CPU100%案例分析(一)

    一.背景 先说下windbg使用场景.各位coder在工作中或多或少都会遇到下面四种情况 1.本地代码好好的,放服务器上运行一段时间后,IIS服务突然占用 w3wp.exe CPU突然100% ,不得 ...

  4. CentOS7 下安装JDK1.7 和 Tomcat7

    一.下载JDK 和 Tomcat安装包 1.JDK1.7 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downl ...

  5. 单例模式的两种实现方式对比:DCL (double check idiom)双重检查 和 lazy initialization holder class(静态内部类)

    首先这两种方式都是延迟初始化机制,就是当要用到的时候再去初始化. 但是Effective Java书中说过:除非绝对必要,否则就不要这么做. 1. DCL (double checked lockin ...

  6. Canvas之打字机游戏

    最近针对粒子化作了一点点的探究,决定结合其做个小游戏,于是这个简单的打字游戏出世了. 试玩地址:Typewriter game  仅在chrome下测试,请谨慎使用其他浏览器(特别是ff):加载速度有 ...

  7. Bootstrap系列 -- 4. 文本内容强调

    一. 文本强调基本样式 .text-muted:提示,使用浅灰色(#999) .text-primary:主要,使用蓝色(#428bca) .text-success:成功,使用浅绿色(#3c763d ...

  8. 提高Visual Studio开发性能的几款插件

    通过打开Visual Studio,单机TOOLS—Extensions and Updates-Online-Visual Studio Gallery(工具-扩展和更新-联网-Visual Stu ...

  9. [codeforces 519E]E. A and B and Lecture Rooms(树上倍增)

    题目:http://codeforces.com/problemset/problem/519/E 题意:给你一个n个点的树,有m个询问(x,y),对于每个询问回答树上有多少个点和x,y点的距离相等 ...

  10. 如何给我们的eclipse新建文件自动生成注释

    有时候,我们需要给我们的文件加载注释,但手动给每一个方法,每一个类添加注释,非常的繁琐,幸好强大的eclipse已经为我们准备好了自动添加注释的配置文件,它就是——codetemplates.xml ...