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. http协议(一)基础知识

    我自己写的随笔一般是偏学习笔记性质的,或者一点个人理解,适合新人,大牛可以忽略这个...... 参考书籍——<图解http> 当我们在浏览器的地址栏中输入网址,然后点击回车,接着,浏览器就 ...

  2. BZOJ 1101: [POI2007]Zap

    1101: [POI2007]Zap Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2262  Solved: 895[Submit][Status] ...

  3. 利用Android的UXSS漏洞完成一次XSS攻击

    黑客攻击的方式思路是先搜集信息,定位漏洞,然后针对不同的漏洞采用不同的方式来黑掉你.下面用metasploit模拟一次跨站脚本攻击(黑掉自己的手机). 1.搜集信息 msf > search a ...

  4. SQL Server读懂语句运行的统计信息 SET STATISTICS TIME IO PROFILE ON

    对于语句的运行,除了执行计划本身,还有一些其他因素要考虑,例如语句的编译时间.执行时间.做了多少次磁盘读等. 如果DBA能够把问题语句单独测试运行,可以在运行前打开下面这三个开关,收集语句运行的统计信 ...

  5. tomcat 新手上路

    前提:本机先安装好JDK,保证常规java环境已经具备 1.下载Tomcat 7.0现在官网上好象已经没有安装程序版了,只有免解压zip版本(现在最新的版本是7.0.42) 下载地址 http://t ...

  6. 2014-2015-2 《Java程序设计》课程学生博客列表

    20135101 曹钰晶 20135103 王海宁 20135104 刘 帅 20135105 王雪铖 20135109 高艺桐 20135111 李光豫 20135114 王朝宪 20135116 ...

  7. Java实现生产者和消费者

    生产者和消费者问题是操作系统的经典问题,在实际工作中也常会用到,主要的难点在于协调生产者和消费者,因为生产者的个数和消费者的个数不确定,而生产者的生成速度与消费者的消费速度也不一样,同时还要实现生产者 ...

  8. 让 HTML5 来为你定位

    Geolocation HTML5 的 geolocation 是一个令人兴奋的 API,通过这套 API,Javascript 代码就能够访问到用户的当前位置.当然,访问之前必须得到用户的明确认可, ...

  9. WampServer下如何实现多域名配置(虚拟域名配置)

    之前在学习跨域的时候,我写过一篇叫做WampServer下使用多端口访问的文章,默认的 localhost 采用的是 80 端口,能使用多端口访问的核心是得新建一个端口,也就是新建一个 http 服务 ...

  10. 转一篇关于如何在Unity里使用Protobuf

    原帖地址: http://purdyjotut.blogspot.com/2013/10/using-protobuf-in-unity3d.html 先转过来,等时间合适了,再来收拾 Using P ...