codeforces814E
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
4
3 2 3 2
1
5
2 3 3 2 2
2
5
2 2 2 2 2
2
20
2 2 2 2 3 2 3 2 2 2 2 2 2 2 2 2 2 3 3 2
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的更多相关文章
- Noip前的大抱佛脚----赛前任务
赛前任务 tags:任务清单 前言 现在xzy太弱了,而且他最近越来越弱了,天天被爆踩,天天被爆踩 题单不会在作业部落发布,所以可(yi)能(ding)会不及时更新 省选前的练习莫名其妙地成为了Noi ...
随机推荐
- 生成 excel 插件 Epplus
最近做 .net core 项目 发现一个新的 生成excel 的插件 . 以前值用 aspose 或者 npio. 简介:Epplus是一个使用Open Office XML(Xlsx)文件格式,能 ...
- nodejs连接mysql数据库,报错Client does not support authentication protocol requested by server的解决方法
最近想要尝试nodejs连接本地数据库,往全栈方向做一个小小的尝试,于是下载了一个 MySQL8.0,发现Navicat连接不上,结果就下载了mysql自身的Workbench,继续使用. 然而,难受 ...
- oracle学习笔记:字符串替换 replace、regexp_replace、translate函数
1.replace 函数 语法:replace(char, search_string, replacement_string) --针对字符串替换 功能: 将char中的字符串替换. 当re ...
- tesseract图像识别验证码:安装使用和避免坑
安装使用 https://blog.csdn.net/kk185800961/article/details/78747595 避免的坑 http://www.mamicode.com/info-de ...
- Nginx中location模块的详细配置(含示例)
题记 此前在配置Nginx location模块的时候玩出了一些bug,折腾了一段时间.后来网上也查阅了相关的资料,看着也比较混乱.周末有空想着好好整理一下location模块的配置,结合自己的亲手实 ...
- centos6/7启动故障排错
centos6启动流程修复: 实验一:删除initramfs-2.6.32-754.el6.x86_64.img进行恢复 该文件很重要initramfs-2.6.32-754.el6.x86_64.i ...
- python之列表、元组
Day 2-Morning 创建列表 创建列表和创建普通变量一样,用中括号括起一堆数据即可(这里的数据可以是整型.字符串.浮点型...甚至可以包含另一个列表),数据间用逗号隔开. eg:number= ...
- 访问php界面访问不到,会下载文件
背景 某台服务器上有java跟php俩套环境,之前php默认用nginx80端口访问php项目.java项目上线后,80端口被占用,导致php项目页访问报错:404 报错 404, 原因一: ...
- 更改jupyter notebook的单元格宽度和主题颜色(theme)
一.单元格宽度 这个命令: jt -t gruvboxl -f roboto -fs 12 -cellw 100% -T -N 它将宽度设置为100% 二.主题颜色 在用jupyter noteboo ...
- java8之Metaspace
HotSpot JVM是java中最常用的java虚拟机.在java8 HotSpot JVM 中,虚拟机的内存模型做了修改调整.以前HotSpot JVM的内存模型分为新生代,老年代,永久 ...