# 【笛卡尔树+DP】【SP3734】 PERIODNI - Periodni

## 题目

English VietnameseLuka is bored in chemistry class so he is staring at a large periodic table of chemical elements hanging from a wall above the blackboard. To kill time, Luka decided to make his own table completely different from the one in the classroom.

His table consists of N columns, each with some height, aligned at the bottom (see example below). After he draws the table he needs to fill it with elements. He first decided to enter the noble gases of which there are K. Luka must put them in the table so that no two noble gases are close to each other.

Two squares in the table are close to each other if they are in the same column or row, and all squares between them exist. In the example below, the ‘a’ squares are not close, but the ‘b’ squares are.

Write a program that, given N, K and the heights of the N columns, calculates the total number of ways for Luka to place the noble gases into the table. This number can be large, so output it modulo 1 000 000 007.

The first line contains the integers N and K separated by a space (1

The next line contains N positive integers, separated by spaces. These are heights of the columns from left to right. The heights will be at most 1 000 000.

Output the number of ways for Luka to fill his table with noble gases, modulo 1 000 000 007.

Translated by @yijan

5 2
2 3 1 2 4

43

## 思路

d p [ u ] [ i ] dp[u][i] 表示 u u 子树内放 i i 个数的方案数， d p 1 [ i ] dp1[i] 表示 当前子树 u u 内不考虑当前矩形，放 i i 个数的方案数，设 H [ i ] H[i] 为当前矩阵可行高度（即 A [ u ] − A [ f a [ u ] ] A[u]-A[fa[u]] ）。

d p [ u ] [ i ] + = ∑ j = 0 i d p 1 [ i − j ] ∗ C ( S z [ u ] − ( i − j ) , j ) ∗ C ( H [ x ] , j ) ∗ j ! dp[u][i]+=\sum_{j=0}^idp1[i-j]*C(Sz[u]-(i-j),j)*C(H[x],j)*j!

## 代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
#define N 5077
#define mod 1000000007
ll n,k,top,root;
ll ls[N],rs[N],st[N];
ll f[N][N],h[N],siz[N],fac[1000050],inv[1000050];
ll qp(ll x,ll y)
{
ll ans=1;
while(y)
{
if(y&1) ans=(ans*x)%mod;
x=(x*x)%mod;
y>>=1;
}
return ans%mod;
}
void init()
{
fac[0]=fac[1]=inv[0]=inv[1]=1;
fac[2]=2,inv[2]=qp(2,mod-2);
for(int i=3;i<=1000000;++i)
{
fac[i]=(fac[i-1]*i)%mod;
inv[i]=qp(fac[i],mod-2);
}
}
ll C(ll n,ll m)
{
if(n<m) return 0;
return fac[n]*inv[m]%mod*inv[n-m]%mod;
}
int build()
{
for(int i=1;i<=n;++i)
{
while(top&&h[st[top]]>h[i]) ls[i]=st[top--];
if(top) rs[st[top]]=i;
st[++top]=i;
}
return st[1];
}
void dfs(int x,int val)
{
f[x][0]=siz[x]=1;
ll high=h[x]-val;
if(ls[x])
{
ll y=ls[x];
dfs(y,h[x]),siz[x]+=siz[y];
for(ll i=min(siz[x],k);i>=0;--i)
for(ll j=1;j<=min(siz[y],i);++j)
f[x][i]=(f[x][i]+f[y][j]*f[x][i-j]%mod)%mod;
}
if(rs[x])
{
ll y=rs[x];
dfs(y,h[x]),siz[x]+=siz[y];
for(ll i=min(siz[x],k);i>=0;--i)
for(ll j=1;j<=min(siz[y],i);++j)
f[x][i]=(f[x][i]+f[y][j]*f[x][i-j]%mod)%mod;
}
for(ll i=min(siz[x],k);i>=0;--i)
for(ll j=1;j<=min(high,i);++j)
f[x][i]=(f[x][i]+f[x][i-j]*fac[j]%mod*C(high,j)%mod*C(siz[x]-(i-j),j)%mod)%mod;
}
int main()
{
init();
root=build();
dfs(root,0);
printf("%lld",f[root][k]);
return 0;
}

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

CE自动机

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文

09-03 4541
07-03 4853
09-20 6214
08-21 1795