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. List 集合 使用 remove 踩得坑

    不要在 foreach 循环里进行元素的 remove/add 操作.remove 元素请使用 Iterator方式,如果并发操作,需要对 Iterator 对象加锁.   正确例子: Iterato ...

  2. DataGridView绑定数据、删除数据

    定义学生类: using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...

  3. JoinableQueue类与线程

    生产者消费者的问题及其解决办法 问题 在之前的生产者消费者模型中,生产者和消费者只有一个, 那么生产者往队列里put几次,消费者就get几次,但是存在一个问题, 生产者不一定只有一个,消费者也不一定只 ...

  4. 4.解析配置文件 redis.conf

    将原始的redis.conf拷贝,得到一个myRedis.conf文件,修改配置文件时,就修改这个文件,不对原始的配置文件进行修改 redis配置文件中主要有以下内容: 1.units单位 a)配置大 ...

  5. Java知识导航总图

    1.系统构架 企业服务总线(ESB).微服务.面向服务的架构(SOA) 了解分布式文件存储系统,掌握集群化开发及部署 2.系统系统集成技术 Wsbservice.Socket 3.RPC远程调用的相关 ...

  6. 《浏览器工作原理与实践》<09>块级作用域:var缺陷以及为什么要引入let和const?

    在前面我们已经讲解了 JavaScript 中变量提升的相关内容,正是由于 JavaScript 存在变量提升这种特性,从而导致了很多与直觉不符的代码,这也是 JavaScript 的一个重要设计缺陷 ...

  7. 微信小程序开发(十三)安卓手机调用wx.getConnectedWifi API失败

    安卓手机调用wx.getConnectedWifi API失败,返回的错误码是12000.需要先startWifi 接口: wx.startWifi({ success(res) { console. ...

  8. java_赋值与初始化

    一.赋值(是给变量指定一个值或者是改变 一个变量的值) 变量类型  变量名=表达式 int i=10; 二.初始化 生成一个变量以后,必须通过明确的赋值语句进行初始化,然后在使用这个变量. 局部变量: ...

  9. 分析可变形字符串序列StringBuilder 以及 StringBuffer之默认大小与扩容

    默认值初始化: 1.  首先明确 StringBuffer类与 StringBuilder类均继承了抽象类 AbstractStringBuilder类 无参构造方法 2. 源码中StringBuff ...

  10. toolbox 中创建nginx服务器,使用localhost不能访问

    使用toolbox 工具使用docker创建nginx 容器,使用localhost不能访问? 使用docker run --rm -d --name dweb  -p 80:80 nginx 命令执 ...