【UVA11174】Stand in a Line——逆元+树形Dp
2016-03-27 19:53
330 查看
All the people in the byteland want to stand in a line in such a way that no person stands closer to the
front of the line than his father. You are given the information about the people of the byteland. You
have to determine the number of ways the bytelandian people can stand in a line.
Input
First line of the input contains T (T < 14) the number of test case. Then following lines contains T
Test cases.
Each test case starts with 2 integers n (1 ≤ n ≤ 40000) and m (0 ≤ m < n). n is the number
of people in the byteland and m is the number of people whose father is alive. These n people are
numbered 1 … n. Next m line contains two integers a and b denoting that b is the father of a. Each
person can have at most one father. And no person will be an ancestor of himself.
Output
For each test case the output contains a single line denoting the number of different ways the soldier
can stand in a single line. The result may be too big. So always output the remainder on dividing ther
the result by 1000000007.
Sample Input
3
3 2
2 1
3 1
3 0
3 1
2 1
Sample Output
2
6
3
题意:村子里有n个人,有多少种方式可以把他们排成一列,使没有人在他的父亲前面,(有的人的父亲不在村子里),输出方案数对1000000007的余数。
分析:村民由父子之间的关系组成一个森林,虽然有多课树,但是我们可以用一个虚拟的根节点将其转化为一棵树,那么根节点的各子树之间相互独立,Dp[i][0]表示以i为根节点的子树的排列的数目,Dp[i][1]表示以i为根节点的树的节点的个数,Dp[i][0]表示以i为根节点的子树的排列的数目,Dp[i][1]表示以i为根节点的树的节点的个数,
那么Dp[Root][0]=Dp[i1][0]×Dp[i2][0]×⋯×Dp[ie][0]×(∑k=1eDp[ik][1])!∏k=1eDp[ik][1]!Dp[Root][1]=∑k=1eDp[ik][1]那么Dp[Root][0] = Dp[i_1][0]\times Dp[i_2][0]\times \cdots\times Dp[i_e][0] \times \frac{(\sum \limits_{k=1}^eDp[i_k][1])! }{\prod \limits_{k=1}^e Dp[i_k][1]! } \quad Dp[Root][1] = \sum \limits_{k=1}^e Dp[i_k][1],就可以递推出最后的结果,对于除法,可以提前处理出逆元。
front of the line than his father. You are given the information about the people of the byteland. You
have to determine the number of ways the bytelandian people can stand in a line.
Input
First line of the input contains T (T < 14) the number of test case. Then following lines contains T
Test cases.
Each test case starts with 2 integers n (1 ≤ n ≤ 40000) and m (0 ≤ m < n). n is the number
of people in the byteland and m is the number of people whose father is alive. These n people are
numbered 1 … n. Next m line contains two integers a and b denoting that b is the father of a. Each
person can have at most one father. And no person will be an ancestor of himself.
Output
For each test case the output contains a single line denoting the number of different ways the soldier
can stand in a single line. The result may be too big. So always output the remainder on dividing ther
the result by 1000000007.
Sample Input
3
3 2
2 1
3 1
3 0
3 1
2 1
Sample Output
2
6
3
题意:村子里有n个人,有多少种方式可以把他们排成一列,使没有人在他的父亲前面,(有的人的父亲不在村子里),输出方案数对1000000007的余数。
分析:村民由父子之间的关系组成一个森林,虽然有多课树,但是我们可以用一个虚拟的根节点将其转化为一棵树,那么根节点的各子树之间相互独立,Dp[i][0]表示以i为根节点的子树的排列的数目,Dp[i][1]表示以i为根节点的树的节点的个数,Dp[i][0]表示以i为根节点的子树的排列的数目,Dp[i][1]表示以i为根节点的树的节点的个数,
那么Dp[Root][0]=Dp[i1][0]×Dp[i2][0]×⋯×Dp[ie][0]×(∑k=1eDp[ik][1])!∏k=1eDp[ik][1]!Dp[Root][1]=∑k=1eDp[ik][1]那么Dp[Root][0] = Dp[i_1][0]\times Dp[i_2][0]\times \cdots\times Dp[i_e][0] \times \frac{(\sum \limits_{k=1}^eDp[i_k][1])! }{\prod \limits_{k=1}^e Dp[i_k][1]! } \quad Dp[Root][1] = \sum \limits_{k=1}^e Dp[i_k][1],就可以递推出最后的结果,对于除法,可以提前处理出逆元。
#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <string> #include <queue> #include <stack> #include <algorithm> #include <iostream> using namespace std; typedef long long LL; const LL Mod = 1e9+7; const int Max = 41000; typedef struct node { int v,next; }edge; edge e[Max*10]; int H[Max],top; int Du[Max]; LL Dp[Max][2]; LL Inv[Max],Fac[Max]; LL Pow(LL a,LL b) //快速幂求逆元。 { LL ans = 1; while(b) { if(b%2) { ans = (ans*a) % Mod; } a = (a * a) % Mod; b >>= 1; } return ans; } void Init() //处理阶乘和逆元 { Inv[0 ]= Fac[0] = 1; for(LL i = 1;i<Max;i++) { Fac[i] = (Fac[i-1]*i)%Mod; Inv[i] = Pow(Fac[i],Mod-2); } } void AddEdge(int u,int v) { e[top].v = v; e[top].next = H[u]; H[u] = top++; } void DFS(int u) { LL num = 0; LL ans = 1; for(int i = H[u];~i;i = e[i].next) { DFS(e[i].v); num+=Dp[e[i].v][1]; ans = (((ans*Dp[e[i].v][0])%Mod)*Inv[Dp[e[i].v][1]])%Mod; } ans = (ans*Fac[num])%Mod; num++; Dp[u][0] = ans; Dp[u][1] = num; } int main() { int n,m,u,v,T; Init(); scanf("%d",&T); while(T--) { scanf("%d %d",&n,&m); memset(H,-1,sizeof(H)); memset(Du,0,sizeof(Du)); top = 0; for(int i = 1;i<=m;i++) { scanf("%d %d",&u,&v); Du[u]++; AddEdge(v,u); } for(int i = 1;i<=n;i++) { if(!Du[i]) AddEdge(0,i); } memset(Dp,0,sizeof(Dp)); DFS(0); printf("%lld\n",Dp[0][0]); } return 0; }
相关文章推荐
- android 实现一个app启动另一个app的service服务
- weblogic对jms实现的QueueConnection实现与TopicConnection实现问题
- OpenNLP:驾驭文本,分词那些事
- 二级联动的代码
- ThreadLocal源码讲解
- Python3.x学习笔记(一)
- 在CentOS里使用MySQL Connector/C++
- 用shell批量创建用户
- java循环练习:模拟注册以及登陆
- shell脚本编程简介
- Codeforces--618B--Guess the Permutation(规律)
- 系统调用system_call的处理过程
- 25.leetcode题目190: Reverse Bits(Follow up未解决)
- php+memcached配置
- JQuery相关的效果
- 性能调优可以从哪些方面着手
- 实践--使用SharedPreferences实现记住密码的功能
- 作业二
- ZMY_viewpager滑动轮播图片小圆点
- ASCII码而已