https://lunch.blog.luogu.org/cf814e-an-unavoidable-detour-for-homedp-ji-shuo-post

https://blog.csdn.net/qq_31759205/article/details/77715991

An unavoidable detour for home

CodeForces - 814E

Those unwilling to return home from a long journey, will be affected by the oddity of the snail and lose their way. Mayoi, the oddity's carrier, wouldn't like this to happen, but there's nothing to do with this before a cure is figured out. For now, she would only like to know the enormous number of possibilities to be faced with if someone gets lost.

There are n towns in the region, numbered from 1 to n. The town numbered 1 is called the capital. The traffic network is formed by bidirectional roads connecting pairs of towns. No two roads connect the same pair of towns, and no road connects a town with itself. The time needed to travel through each of the roads is the same. Lost travelers will not be able to find out how the towns are connected, but the residents can help them by providing the following facts:

  • Starting from each town other than the capital, the shortest path (i.e. the path passing through the minimum number of roads) to the capital exists, and is unique;
  • Let li be the number of roads on the shortest path from town i to the capital, then li ≥ li - 1 holds for all 2 ≤ i ≤ n;
  • For town i, the number of roads connected to it is denoted by di, which equals either 2 or 3.

You are to count the number of different ways in which the towns are connected, and give the answer modulo 109 + 7. Two ways of connecting towns are considered different if a pair (u, v) (1 ≤ u, v ≤ n) exists such there is a road between towns u and v in one of them but not in the other.

Input

The first line of input contains a positive integer n (3 ≤ n ≤ 50) — the number of towns.

The second line contains n space-separated integers d1, d2, ..., dn (2 ≤ di ≤ 3) — the number of roads connected to towns 1, 2, ..., n, respectively. It is guaranteed that the sum of di over all i is even.

Output

Output one integer — the total number of different possible ways in which the towns are connected, modulo 109 + 7.

Examples

Input
4
3 2 3 2
Output
1
Input
5
2 3 3 2 2
Output
2
Input
5
2 2 2 2 2
Output
2
Input
20
2 2 2 2 3 2 3 2 2 2 2 2 2 2 2 2 2 3 3 2
Output
82944

Note

In the first example, the following structure is the only one to satisfy the constraints, the distances from towns 2, 3, 4 to the capital are all 1.

In the second example, the following two structures satisfy the constraints.

/*
g的状态转移:
①当i=j=k=0时,g[i][j][k]=1
②当 j=0,k>0时,g[i][j][k]=∑g[i][j][k-1-l]*c[k-1][l]*(l!)/2 l∈[2,k-1]
表示从前k-1个度数能变成3的点中选l个点出来与最后一个点构成环,这l+1度数都变成3
③当i=0,j>0或k>0时,g[i][j][k]=g[i][j-2][k]*(j-1)+g[i][j][k-1]*k
前一部分:表示从j-1个度数能变成2的点中选一个出来与最后一个点相连,这2个点的度数都变成2
后一部分:表示从k个度数能变成2的点中选一个出来与最后一个点相连,这个点变成度数3,最后一个点变成度数2
④当i>0时,g[i][j][k]=g[i-1][j-1][k]*j+g[i-1][j+1][k-1]*k
前一部分:表示从j个度数能变成2的点中选一个出来与最后一个点相连,这个点变成度数2
后一部分:表示从k个度数能变成3的点钟选一个出来与最后一个点相连,这个点变成度数3
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
ll s=; bool f=; char ch=' ';
while(!isdigit(ch)) {f|=(ch=='-'); ch=getchar();}
while(isdigit(ch)) {s=(s<<)+(s<<)+(ch^); ch=getchar();}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<) {putchar('-'); x=-x;}
if(x<) {putchar(x+''); return;}
write(x/); putchar((x%)+'');
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
const ll Mod=;
int n,deg[N];
ll fac[N],C[N][N];
ll ans=,dp[N][N],g[N][N][N];
/*
dp[i][j]表示前i个节点分为若干层,最后一层有j个节点的方案数
g[i][j][k]表示当前这层有i个节点,上一层有j个度数为2,k个度数为3的
*/
inline void Ad(ll &x,ll y)
{
x+=y; x-=(x>=Mod)?Mod:;
}
int main()
{
// freopen("data.in","r",stdin);
// freopen("my.out","w",stdout);
int i,j,k,l,c2,c3;
R(n);
for(i=;i<=n;i++) R(deg[i]);
fac[]=fac[]=; fac[]=; for(i=;i<=n;i++) fac[i]=fac[i-]*i%Mod; //fac[i]=(i!)/2;
C[][]=;
for(i=;i<=n;i++)
{
C[i][]=; for(j=;j<=n;j++) C[i][j]=(C[i-][j-]+C[i-][j])%Mod;
}
g[][][]=;//i=0就是最下面的时候,要把最后一层连好的方案数
for(j=;j<=n;j++) for(k=;k<=n-j;k++)
{
if(j==&&k>)
{
for(l=;l<=k-;l++) Ad(g[][j][k],g[][j][k-l-]*C[k-][l]%Mod*fac[l]%Mod);//新加的点凑个环
}
else
{
if(j>=) Ad(g[][j][k],g[][j-][k]*(j-)%Mod);//度数为2的必须成对加
if(k>=) Ad(g[][j][k],g[][j][k-]*k%Mod);
}
}
for(i=;i<=n;i++)
{
for(j=;j<=n-i;j++) for(k=;k<=n-i-j;k++)
{
if(j>=) Ad(g[i][j][k],g[i-][j-][k]*j%Mod);
if(k>=) Ad(g[i][j][k],g[i-][j+][k-]*k%Mod);
}
}
// for(i=0;i<=n;i++) for(j=0;j<=n;j++) for(k=0;k<=n;k++)
// {
// cout<<i<<' '<<j<<' '<<k<<' '<<g[i][j][k]<<endl;
// }
dp[deg[]+][deg[]]=;
for(i=deg[]+;i<=n;i++)
{
for(j=;j<=i-deg[]-;j++) //从第三层开始
{
c2=; c3=;
for(k=;k<=i-j;k++)//枚举上一层有k个点
{
if(deg[i-j-k+]==) c2++; else c3++;
Ad(dp[i][j],dp[i-j][k]*g[j][c2][c3]%Mod);
}
}
}
c2=; c3=;
for(i=;i<n;i++)
{
if(deg[n-i+]==) c2++; else c3++;
Ad(ans,dp[n][i]*g[][c2][c3]%Mod);
}
Wl(ans);
return ;
}

codeforces814E的更多相关文章

  1. Noip前的大抱佛脚----赛前任务

    赛前任务 tags:任务清单 前言 现在xzy太弱了,而且他最近越来越弱了,天天被爆踩,天天被爆踩 题单不会在作业部落发布,所以可(yi)能(ding)会不及时更新 省选前的练习莫名其妙地成为了Noi ...

随机推荐

  1. poj 2406 求最短重复字串

    题解: KMP中next数组的巧妙运用.在这里我们假设这个字符串的长度是len,那么如果len可以被len-next[len]整除的话,我们就可以说len-next[len]就是那个最短子串的长度为什 ...

  2. 微软升级 WSL Window Subsystem for Linux Win10系统自带Linux

    在设计之初,微软就允许类似于Win32这种子系统运行于windows NT内核之上,它可以为上层应用提供编程接口,同时避免应用去实现内核里的一些调用细节.NT内核的设计在最开始就可以支持POSIX,O ...

  3. 关于Vue中,checkBox等组件在赋值后,点击切换页面未及时更新问题

    我们经常碰到这样的问题,在v-for循环中,给某些组件(此处以checkBox为例)赋值后,组件并不能正常切换, 这是因为数据层太多,render函数没有自动更新,需手动强制刷新. 解决方法:在切换c ...

  4. HttpResponse与JasonResponse

    两者的含义 我们都知道后台给前台返回的数据都是字符串类型,那么怎么返回成为一个问题 HttpResponse与JasonResponse都是django中后台给前台返回数据的方法, 并且他们最后走的都 ...

  5. 移动端适配flexible.js

    npm install lib-flexible --save npm install px2rem-loader --save-dev

  6. 用jq动态给导航菜单添加active

    点击后页面跳转到了新的链接,找到所有的li下的a标签,对其链接地址进行判断,如果和当前浏览器的地址一致,就认为是当前应该激活的菜单,添加active类,否则就取消. <ul class=&quo ...

  7. SmartBinding实现DataSet与ListView的绑定及同步显示

    kbmMW 5.10.10发布了,这个版本解决了我提出的问题,当对DataSet增删记录时,ListView能够同步显示.下面看看具体的实现代码. 为了解决上面的问题,作者为IkbmMWBinding ...

  8. CSS3 filter滤镜

    其默认值是none,他不具备继承性,其中filter-function一个具有以下值可选: grayscale灰度 sepia褐色(求专业指点翻译) saturate饱和度 hue-rotate色相旋 ...

  9. 一个基于Scrapy框架的pixiv爬虫

    源码 https://github.com/vicety/Pixiv-Crawler,功能什么的都在这里介绍了 说几个重要的部分吧 登录部分 困扰我最久的部分,网上找的其他pixiv爬虫的登录方式大多 ...

  10. gitlab安装教程 正在修炼的西瓜君

    查看内存配置  我们先不急着来安装gitlab,先来看一下自己电脑的内存情况,我把这一步提到最前面,是因为这是我安装过程中遇到的最大的坑.  下面是gitlab的cpu和内存需求(https://do ...