[cf140e]New Year Garland
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的更多相关文章
- CF140E New Year Garland (计数问题)
用$m$种颜色的彩球装点$n$层的圣诞树.圣诞树的第$i$层恰由$a_{i}$个彩球串成一行,且同一层内的相邻彩球颜色不同,同时相邻两层所使用彩球的颜色集合不 同.求有多少种装点方案,答案对$p$取模 ...
- POJ 1759 Garland(二分+数学递归+坑精度)
POJ 1759 Garland 这个题wa了27次,忘了用一个数来储存f[n-1],每次由于二分都会改变f[n-1]的值,得到的有的值不精确,直接输出f[n-1]肯定有问题. 这个题用c++交可以 ...
- poj 1759 Garland (二分搜索之其他)
Description The New Year garland consists of N lamps attached to a common wire that hangs down on th ...
- poj 1759 Garland
Garland Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2365 Accepted: 1007 Descripti ...
- 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 ...
- 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 ...
- Diverse Garland CodeForces - 1108D (贪心+暴力枚举)
You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...
- Nice Garland CodeForces - 1108C (思维+暴力)
You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...
- Codeforces 1108D - Diverse Garland - [简单DP]
题目链接:http://codeforces.com/problemset/problem/1108/D time limit per test 1 secondmemory limit per te ...
随机推荐
- MySQL日志管理
MySQL日志管理 2013年09月26日 ⁄ MySQL ⁄ 共 14266字 ⁄ 评论数 ⁄ 被围观 , views+ 一.日志类型: MySQL有几个不同的日志文件,可以帮助你找出mysqld内 ...
- java集合比较
几种集合的比较Hashset,hashmap无序的treeset,hashset有序的 linkedhashset 有序的,和插入数序一样的
- 升级nodejs版本
node有一个模块叫n,是专门用来管理node.js的版本的. 首先安装n模块: npm install -g n 第二步: 升级node.js到最新稳定版 n stable n后面也可以跟随版本号比 ...
- BZOJ 1854 【Scoi2010】 游戏
Description lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示.当他使用某种装备时,他只能使用该装备的某一个属性 ...
- Linux 信号详解三(sleep,raise)
sleep()函数 .sleep()函数作用:让进程睡眠 .能被信号打断,然后处理信号函数以后,就不再睡眠,直接向下执行代码 .sleep函数的返回值是剩余秒数 //sleep 函数 #include ...
- 数据库 Linux下的MySQL数据库管理
数据库就是数据的集合. 关系数据库是一种特殊的数据库,他将数据组织城标,并表示为表之间的关系. 数据库系统往往是大型项目的核心数据内容,如银行的用户账户信息,腾讯QQ的用户账户信息.股市的各种交易信息 ...
- JUnit4测试简介
相比于自己写一个测试类,在里面调用调试方法,使用JUnit4进行测试有很多的优点,极大的提高了测试的速度.本文简单介绍如何使用myEclipse10使用JUnit4,方便日后回顾总结. myEclip ...
- 【一】我眼中的FeatureLayer
1.来源 MapService 或者 FeatureService(10.0后)中的一个图层 Tabel 动态空间 2.使用 符号化 首先看下FLyr的继承关系:FeatureLayer Graph ...
- Python2.6-原理之类和oop(上)
来自<python学习手册第四版>第六部分 一.oop:宏伟蓝图(26章) 在这之前的部分中,经常会使用"对象"这个词,其实,到目前为止都是以对象为基础的,在脚本中传递 ...
- [转]Android Studio创建Xposed模块项目时BridgeApi的正确添加方式
使用Android Studio创建的空项目作为Xposed Module App,对于Api Jar包的引用方式,一开始是按照傻瓜式Jar Lib的处理方式,复制XposedBridgeApi-54 ...