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. (五)mybatis之一对一关系

    一.需求分析 需求:查询订单信息关联查询用户信息 分析:一条订单只能由一个消费者来下单,也就是说从订单的角度来说与消费者是一对一的关系. 二.建数据库表和实体对象 其中订单表中的字段user_id对应 ...

  2. ASP.NET 使用 SyndicationFeed 输出 Rss

    以前生成 RSS 都是使用拼接 Xml 的方式生成的,不仅麻烦而且还不规范. #region 输出指定分类编号的消息源内容... /// <summary> /// 输出指定分类编号的消息 ...

  3. 可视化利器 TensorBoard

    人工智能的黑盒: TensorBoard 的作用: 1.用TensorFlow保存图的信息到日志中 tfsummary.FileWriter("日志保存路径", sess.grap ...

  4. Java Web-JSTL

    Java Web-JSTL 概念 Java Server Pages Tag Library:JSP标准标签库 是由Apache组织提供的开源.免费JSP标签 用于简化和替换JSP页面上的Java代码 ...

  5. 运动的border,仿当当简易效果

    突然想到以前看到当当上有个效果,当鼠标移上去,图片边框是运动添加上的,还以为是css3或者是canvas做的呢,做完幽灵按钮后,才知道,so  easy,只不过是animate+position的杰作 ...

  6. sql 给相同属性的数据排序

    UPDATE b SET OrderIndex = a.OrderIndex FROM ( SELECT RTRIM(ROW_NUMBER() OVER ( PARTITION BY [ItemID] ...

  7. vagrant 搭建开发环境

    虚拟机盒子地址 https://app.vagrantup.com/boxes/search vagrant init hirocom/centos7.2vagrant up 修改配置 config. ...

  8. tr 命令详细介绍

    tr用来从标准输入中对字符进行操作,主要用于删除文件中指定字符.字符转换.压缩文件字符. 我们可以用:tr --help查看一下系统详细介绍 [root@bqh-118 scripts]# tr -- ...

  9. linux——运维基础,与常用命令

    1 运维概述 1 什么是运维 服务器的运行维护 2 名词 IDC(互联网数据中心) 3 监控软件 zabbix(用的最多), nagios, cactti 4 常用的linux操作系统 1 CentO ...

  10. 挖矿病毒watchbog处理过程

    1 挖矿病毒watchbog处理过程 简要说明 这段时间公司的生产服务器中了病毒watchbog,cpu动不动就是100%,查看cpu使用情况,发现很大一部分都是us,而且占100%左右的都是进程wa ...