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. mysql--测试前缀索引能否用于order by 或者 group by

    创建一个测试用表 mysql> desc two; +-------+-------------+------+-----+---------+----------------+ | Field ...

  2. Linux中 groupadd 和 useradd 的命令说明

    groupadd [options] group 说明The groupadd command creates a new group account using the values specifi ...

  3. PAT 1002. 写出这个数 (20)

    读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10100. 输出格式:在一行内输出n的各位数字之和的每 ...

  4. Pycharm: keyboard reference

    Source: Official set ♥ Editing Ctrl + Space Basic code completion (the name of any class, method or ...

  5. Razor 模板自己渲染出结果 string

    using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNet ...

  6. 转:windows命令行下如何查看磁盘空间大小

    转自:http://www.cnblogs.com/hanxianlong wmic DiskDrive get Size /value ::查看C盘 wmic LogicalDisk where & ...

  7. dirtycow漏洞

    dirtycow漏洞,原理还没看懂,找了几个PoC实验了一下. dirtyc0w.c我在CentOS和Kali上都失败了 pokemon.c在CentOS上成功修改了只读文件,不过修改的不是很顺利,结 ...

  8. PRML读书会第七章 Sparse Kernel Machines(支持向量机, support vector machine ,KKT条件,RVM)

    主讲人 网神 (新浪微博: @豆角茄子麻酱凉面) 网神(66707180) 18:59:22  大家好,今天一起交流下PRML第7章.第六章核函数里提到,有一类机器学习算法,不是对参数做点估计或求其分 ...

  9. bootstrap的popover插件在focus模式时在Safari浏览器无法使用的bug解决方案

    前言 最近在使用bootstrap的popover插件,效果如下: popover插件的focus模式时表现为当点击按钮时弹出浮动层,在点击浮动层外的任何一处,都隐藏浮动层. 但是在mac下的Safa ...

  10. 理解Java接口

    作者:梅云罗链接:https://www.zhihu.com/question/20111251/answer/14760353来源:知乎著作权归作者所有,转载请联系作者获得授权. 接口只是一个规范, ...