[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 ...
随机推荐
- CentOS 6.6 安装redmine
Redmine是一个开源的.基于Web的项目管理和缺陷跟踪工具.它用日历和甘特图辅助项目及进度可视化显示.同时它又支持多项目管理.Redmine是一个自由开放源码软件解决方案,它提供集成的项目管理功能 ...
- JS/JQ常见兼容辅助插件
1.Respond.js Respond.js 是一个快速.轻量的 polyfill,用于为 IE6-8 以及其它不支持 CSS3 Media Queries 的浏览器提供媒体查询的 min-widt ...
- usb驱动开发16之设备生命线
回到struct usb_hcd,继续努力的往下看. kref,usb主机控制器的引用计数.struct usb_hcd也有自己专用的引用计数函数,看hcd.c文件. static void hcd_ ...
- 微信小程序 开发 微信开发者工具 快捷键
微信小程序已经跑起来了.快捷键设置找了好久没找到,完全凭感觉.图贴出来.大家看看. 我现在用的是0.10.101100的版本,后续版本更新快捷键也应该不会有什么变化. 现在貌似不能修改.如果有同学找到 ...
- 扩展 easyui-tabs 插件 关闭标签页方法
$.extend($.fn.tabs.methods,{ allTabs:function(jq){ var tabs = $(jq).tabs('tabs'); var all = []; all ...
- springmvc 通过异常增强返回给客户端统一格式
在springmvc开发中,我们经常遇到这样的问题:逻辑正常执行时返回客户端指定格式的数据,比如json,但是遇NullPointerException空指针异常,NoSuchMethodExcept ...
- stringstream的用法【转】
[本文来自]http://www.builder.com.cn/2003/0304/83250.shtmlhttp://www.cppblog.com/alantop/archive/2007/07/ ...
- 去除项目中的SVN标记
第一步:建立一个名字叫做remove-svn-folders.reg的文本(先建立txt文件,然后粘贴内容后再修改文件名字),记得后缀要叫.reg.文本的内容为: Windows Registry E ...
- c++基础 使用智能指针
三个智能指针模板(auto_ptr.unique_ptr和shard_ptr)都定义了类似指针的对象(c++11已将auto_ptr摒弃),可以将new获得(直接或间接) 的地址赋给这种对象.当智能指 ...
- CSS实现透明边框
border: 8px solid rgba(255,255,255,0.2);