AtCoder Beginner Contest 160 F Distributing Integers change root dp + permutation and combination

AtCoder Beginner Contest 160 The   number of contestants is 9747   fast, see all questions 3 minutes after the start of the contest

AtCoder Beginner Contest 160 F Distributing Integers change root dp + permutation and combination

See https://blog.csdn.net/mrcrack/article/details/104454762 for the general catalog

Online evaluation address https://atcoder.jp/contests/abc160/tasks/abc160_f

Change root dp If you don't understand, you can read this article [codeforces 1187E] Tree Painting Change root dp

If the factorial inverse element cannot be calculated, you can see the three fast methods of factorial inverse element in this paper.

1. The following figure uses 3 as the root node, the origin of the calculation of the number of categories 840

1.1 is as follows, the calculation needs to start from the leaf node, using the multiplication principle of combinatorial mathematics, please note that 1 this data has been occupied by the root node 3

1.2 Node 3 and node 6 are merged, a total of 6 data are available, deducting the data 1 occupied by node 3, and 5 data remaining, allocated to the nodes 6,7,8 group, which is the origin of C (5,3)

 1.3 Node 3 and Node 2 are merged, a total of 8 data are available, after deducting the data 1 occupied by Node 3, there are 7 data left, which are allocated to the nodes 1, 2 groups, which is the origin of C (7,2)

2. Take sample 4 as an example to show the process of data brute force generation. If you are not clear about the data generation process, you can see the content of the dfs1 part in the AC code .

2.1 Taking 1 as the root, the corresponding data generation process is as follows

for [u] = the [i] * to [u] * C (OT [in] -1, S [i]);

pre [u] is the number of arrangement of the subtree rooted by u, sz [u] is the number of nodes of the subtree rooted by u

sz[8]=1 pre[8]=1 sz[6]=2 pre[6]=1
sz[7]=1 pre[7]=1 sz[6]=3 pre[6]=2
sz[6]=3 pre[6]=2 sz[3]=4 pre[3]=2
sz[5]=1 pre[5]=1 sz[3]=5 pre[3]=8
sz[4]=1 pre[4]=1 sz[3]=6 pre[3]=40
sz[3]=6 pre[3]=40 sz[2]=7 pre[2]=40
sz[2]=7 pre[2]=40 sz[1]=8 pre[1]=40
sz[1]=8
sz[2]=7
sz[3]=6
sz[4]=1
sz[5]=1
sz[6]=3
sz[7]=1
sz[8]=1
pre[1]=40
pre[2]=40
pre[3]=40
pre[4]=1
pre[5]=1
pre[6]=2
pre[7]=1
pre[8]=1

2.2 Taking 2 as the root, the corresponding data generation process is as follows

 

sz[8]=1 pre[8]=1 sz[6]=2 pre[6]=1
sz[7]=1 pre[7]=1 sz[6]=3 pre[6]=2
sz[6]=3 pre[6]=2 sz[3]=4 pre[3]=2
sz[5]=1 pre[5]=1 sz[3]=5 pre[3]=8
sz[4]=1 pre[4]=1 sz[3]=6 pre[3]=40
sz[3]=6 pre[3]=40 sz[2]=7 pre[2]=40
sz[1]=1 pre[1]=1 sz[2]=8 pre[2]=280
sz[1]=1
sz[2]=8
sz[3]=6
sz[4]=1
sz[5]=1
sz[6]=3
sz[7]=1
sz[8]=1
pre[1]=1
pre[2]=280
pre[3]=40
pre[4]=1
pre[5]=1
pre[6]=2
pre[7]=1
pre[8]=1

2.3 With 3 as the root, the corresponding data generation process is as follows

sz[8]=1 pre[8]=1 sz[6]=2 pre[6]=1
sz[7]=1 pre[7]=1 sz[6]=3 pre[6]=2
sz[6]=3 pre[6]=2 sz[3]=4 pre[3]=2
sz[5]=1 pre[5]=1 sz[3]=5 pre[3]=8
sz[4]=1 pre[4]=1 sz[3]=6 pre[3]=40
sz[1]=1 pre[1]=1 sz[2]=2 pre[2]=1
sz[2]=2 pre[2]=1 sz[3]=8 pre[3]=840
sz[1]=1
sz[2]=2
sz[3]=8
sz[4]=1
sz[5]=1
sz[6]=3
sz[7]=1
sz[8]=1
pre[1]=1
pre[2]=1
pre[3]=840
pre[4]=1
pre[5]=1
pre[6]=2
pre[7]=1
pre[8]=1

2.4 Take 4 as the root, the corresponding data generation process is as follows

 

sz[8]=1 pre[8]=1 sz[6]=2 pre[6]=1
sz[7]=1 pre[7]=1 sz[6]=3 pre[6]=2
sz[6]=3 pre[6]=2 sz[3]=4 pre[3]=2
sz[5]=1 pre[5]=1 sz[3]=5 pre[3]=8
sz[1]=1 pre[1]=1 sz[2]=2 pre[2]=1
sz[2]=2 pre[2]=1 sz[3]=7 pre[3]=120
sz[3]=7 pre[3]=120 sz[4]=8 pre[4]=120
sz[1]=1
sz[2]=2
sz[3]=7
sz[4]=8
sz[5]=1
sz[6]=3
sz[7]=1
sz[8]=1
pre[1]=1
pre[2]=1
pre[3]=120
pre[4]=120
pre[5]=1
pre[6]=2
pre[7]=1
pre[8]=1

2.5 takes 5 as the root, and the corresponding data generation process is as follows

 

sz[8]=1 pre[8]=1 sz[6]=2 pre[6]=1
sz[7]=1 pre[7]=1 sz[6]=3 pre[6]=2
sz[6]=3 pre[6]=2 sz[3]=4 pre[3]=2
sz[4]=1 pre[4]=1 sz[3]=5 pre[3]=8
sz[1]=1 pre[1]=1 sz[2]=2 pre[2]=1
sz[2]=2 pre[2]=1 sz[3]=7 pre[3]=120
sz[3]=7 pre[3]=120 sz[5]=8 pre[5]=120
sz[1]=1
sz[2]=2
sz[3]=7
sz[4]=1
sz[5]=8
sz[6]=3
sz[7]=1
sz[8]=1
pre[1]=1
pre[2]=1
pre[3]=120
pre[4]=1
pre[5]=120
pre[6]=2
pre[7]=1
pre[8]=1

2.6 takes 6 as the root, and the corresponding data generation process is as follows

 

sz[8]=1 pre[8]=1 sz[6]=2 pre[6]=1
sz[7]=1 pre[7]=1 sz[6]=3 pre[6]=2
sz[5]=1 pre[5]=1 sz[3]=2 pre[3]=1
sz[4]=1 pre[4]=1 sz[3]=3 pre[3]=2
sz[1]=1 pre[1]=1 sz[2]=2 pre[2]=1
sz[2]=2 pre[2]=1 sz[3]=5 pre[3]=12
sz[3]=5 pre[3]=12 sz[6]=8 pre[6]=504
sz[1]=1
sz[2]=2
sz[3]=5
sz[4]=1
sz[5]=1
sz[6]=8
sz[7]=1
sz[8]=1
pre[1]=1
pre[2]=1
pre[3]=12
pre[4]=1
pre[5]=1
pre[6]=504
pre[7]=1
pre[8]=1

2.7 takes 7 as the root, and the corresponding data generation process is as follows

 

sz[8]=1 pre[8]=1 sz[6]=2 pre[6]=1
sz[5]=1 pre[5]=1 sz[3]=2 pre[3]=1
sz[4]=1 pre[4]=1 sz[3]=3 pre[3]=2
sz[1]=1 pre[1]=1 sz[2]=2 pre[2]=1
sz[2]=2 pre[2]=1 sz[3]=5 pre[3]=12
sz[3]=5 pre[3]=12 sz[6]=7 pre[6]=72
sz[6]=7 pre[6]=72 sz[7]=8 pre[7]=72
sz[1]=1
sz[2]=2
sz[3]=5
sz[4]=1
sz[5]=1
sz[6]=7
sz[7]=8
sz[8]=1
pre[1]=1
pre[2]=1
pre[3]=12
pre[4]=1
pre[5]=1
pre[6]=72
pre[7]=72
pre[8]=1

2.8 takes 8 as the root, and the corresponding data generation process is as follows

 

sz[7]=1 pre[7]=1 sz[6]=2 pre[6]=1
sz[5]=1 pre[5]=1 sz[3]=2 pre[3]=1
sz[4]=1 pre[4]=1 sz[3]=3 pre[3]=2
sz[1]=1 pre[1]=1 sz[2]=2 pre[2]=1
sz[2]=2 pre[2]=1 sz[3]=5 pre[3]=12
sz[3]=5 pre[3]=12 sz[6]=7 pre[6]=72
sz[6]=7 pre[6]=72 sz[8]=8 pre[8]=72
sz[1]=1
sz[2]=2
sz[3]=5
sz[4]=1
sz[5]=1
sz[6]=7
sz[7]=1
sz[8]=8
pre[1]=1
pre[2]=1
pre[3]=12
pre[4]=1
pre[5]=1
pre[6]=72
pre[7]=1
pre[8]=72

3. Take sample 4 as an example to show the process of generating data for root dp generation

3.1 With 1 as the root, the corresponding data generation process is as follows

pre [u] is the number of arrangement of the subtree rooted by u, sz [u] is the number of nodes of the subtree rooted by u

sz[8]=1 pre[8]=1 sz[6]=2 pre[6]=1
sz[7]=1 pre[7]=1 sz[6]=3 pre[6]=2
sz[6]=3 pre[6]=2 sz[3]=4 pre[3]=2
sz[5]=1 pre[5]=1 sz[3]=5 pre[3]=8
sz[4]=1 pre[4]=1 sz[3]=6 pre[3]=40
sz[3]=6 pre[3]=40 sz[2]=7 pre[2]=40
sz[2]=7 pre[2]=40 sz[1]=8 pre[1]=40
sz[1]=8
sz[2]=7
sz[3]=6
sz[4]=1
sz[5]=1
sz[6]=3
sz[7]=1
sz[8]=1
pre[1]=40
pre[2]=40
pre[3]=40
pre[4]=1
pre[5]=1
pre[6]=2
pre[7]=1
pre[8]=1

v is the root of the next search, u is the current root, and tp [v] represents the value of pre [u] when taking v as the root.

tp[v]=ans[u]/pre[v]/C(n-1,sz[v]);

ans[u]=tp[v]*pre[u]*C(n-1,n-sz[u]);

ans[1]=40 u=2 fa=1
tp[2]=ans[1]/pre[2]/C(n-1,sz[1])=40/40/C(7,7)=1
ans[2]=tp[2]*pre[2]*C(n-1,n-sz[2])=1*40*C(7,1)=280

ans[2]=280 u=3 fa=2
tp[3]=ans[2]/pre[3]/C(n-1,sz[2])=280/40/C(7,6)=1
ans[3]=tp[3]*pre[3]*C(n-1,n-sz[3])=1*40*C(7,2)=840

u=6 fa=3
u=8 fa=6
u=7 fa=6
u=5 fa=3
u=4 fa=3
40
280
840
120
120
504
72
72

The idea is the same as https://www.cnblogs.com/JohnRan/p/12591555.html

F. Give you a tree of n nodes, put 1-n in order, and place the condition that another point on this side has already placed a number. Find the number of solutions to put 1 on nodes 1-n.

  Because it requires the number of solutions to put 1 on node 1-n, the root dp is explicitly stated (maybe there are other methods, but I will not hey. First consider how to count the operands when putting 1 on node 1 .

  Let pre [v] be the number of arrangements of the subtree rooted at v. This time is a simple multiplication principle, first select and then put into the subtree arrangement. The first time dfs can process all pre []. When changing roots

  We must first take into account that when u's son v is the root, we must first get u as the pre [u] of the v subtree. Obviously (may need to write a little on the paper, the principle is to select and then sort) is ans [u] / (pre [v] * C (n-1, sz [v])), after knowing this, get ans [v] But just for u

  This subtree selects some numbers again. This is obviously C (n-1, n-sz [v]). According to the method of calculating the u answer, it can be directly applied to v, see the code for details.

AC code is as follows

#include <cstdio>
#include <algorithm>
#define LL long long
#define mod 1000000007
#define maxn 200010
using namespace std;
LL fact[maxn],inv[maxn];
int n,head[maxn],cnt,sz[maxn];//sz[u]是以u为根的子树的节点数
LL pre[maxn],ans[maxn],tp[maxn];//pre[u]是以u为根的子树的安排方式数
struct node{
	int to,next;
}e[maxn<<1];
void add_edge(int u,int v){//邻接表
	cnt++,e[cnt].to=v,e[cnt].next=head[u],head[u]=cnt;
}
LL quick_pow(LL a,LL b){//快速幂
	LL ans=1;
	while(b){
		if(b&1)ans=ans*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ans;
}
void init(){
	int i,u,v;
	scanf("%d",&n);
	for(i=1;i<n;i++){
		scanf("%d%d",&u,&v);
		add_edge(u,v),add_edge(v,u);//无向图
	}
	fact[0]=1;
	for(i=1;i<=n;i++)fact[i]=fact[i-1]*i%mod;
	inv[n]=quick_pow(fact[n],mod-2);
	for(i=n-1;i>=0;i--)inv[i]=inv[i+1]*(i+1)%mod;
}
LL C(int a,int b){//组合数计算
	return fact[a]*inv[a-b]%mod*inv[b]%mod;
}
void dfs1(int u,int fa){
	int b,v;
	sz[u]=1,pre[u]=1;
	for(b=head[u];b;b=e[b].next){
		v=e[b].to;
		if(v==fa)continue;
		dfs1(v,u);
		sz[u]+=sz[v];
		pre[u]=pre[v]*pre[u]%mod*C(sz[u]-1,sz[v])%mod;
	}
}
void dfs2(int u,int fa){//换根dp
	int b,v;
	ans[u]=tp[u]*pre[u]%mod*C(n-1,n-sz[u])%mod;
	for(b=head[u];b;b=e[b].next){
		v=e[b].to;
		if(v==fa)continue;
		tp[v]=ans[u]*quick_pow(pre[v]*C(n-1,sz[v])%mod,mod-2)%mod;//v是下一次搜索的根,u是当前的根,tp[v]代表在以v为根时,pre[u]的值。
		dfs2(v,u);
	}
}
int main(){
	init();
	dfs1(1,0);
	tp[1]=1;
	dfs2(1,0);
	for(int i=1;i<=n;i++)printf("%lld\n",ans[i]);
	return 0;
}

 

 

Published 660 original articles · praised 562 · 480,000 views

Guess you like

Origin blog.csdn.net/mrcrack/article/details/105207929