数学:UVAoj 11174 Stand in a Line
Problem J
Stand in a Line
Input: Standard Input
Output: Standard Output
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 Output for
Sample Input
3 3 2 2 1 3 1 3 0 3 1 2 1 |
2 6 3
|
Problem setter: Abdullah-al-Mahmud
Special Thanks: Derek Kisman
这道题结论很有意思。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int mod=;
const int maxn=;
int cnt,fir[maxn],to[maxn],nxt[maxn];
int fa[maxn],sz[maxn];
long long inv[maxn]; void addedge(int a,int b){
nxt[++cnt]=fir[a];to[cnt]=b;fir[a]=cnt;
} void DFS(int x){
sz[x]=;
for(int i=fir[x];i;i=nxt[i]){
DFS(to[i]);
sz[x]+=sz[to[i]];
}
} void Solve(int n){
DFS();
long long ans=;
for(int i=;i<sz[];i++)
(ans*=i)%=mod; for(int i=;i<=n;i++)
(ans*=inv[sz[i]])%=mod; printf("%lld\n",ans);
} int main(){
#ifndef ONLINE_JUDGE
//freopen("","r",stdin);
//freopen("","w",stdout);
#endif
inv[]=;
for(int i=;i<=;i++){
inv[i]=(1ll*inv[mod%i]*(mod-mod/i))%mod;
//printf("%lld\n",inv[i]);
} int T,n,m;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
memset(fir,,sizeof(fir));
memset(fa,,sizeof(fa));cnt=;
for(int i=,a,b;i<=m;i++){
scanf("%d%d",&a,&b);
fa[a]=;addedge(b,a);
}
for(int i=;i<=n;i++)
if(fa[i]==)
addedge(,i); Solve(n);
}
return ;
}
数学:UVAoj 11174 Stand in a Line的更多相关文章
- uva 11174 Stand in a Line
// uva 11174 Stand in a Line // // 题目大意: // // 村子有n个村民,有多少种方法,使村民排成一条线 // 使得没有人站在他父亲的前面. // // 解题思路: ...
- UVA 11174 Stand in a Line 树上计数
UVA 11174 考虑每个人(t)的所有子女,在全排列中,t可以和他的任意子女交换位置构成新的排列,所以全排列n!/所有人的子女数连乘 即是答案 当然由于有MOD 要求逆. #include & ...
- UVA 11174 Stand in a Line (组合+除法的求模)
题意:村子里有n个人,给出父亲和儿子的关系,有多少种方式可以把他们排成一列,使得没人会排在他父亲的前面 思路:设f[i]表示以i为根的子树有f[i]种排法,节点i的各个子树的根节点,即它的儿子为c1, ...
- UVA 11174 Stand in a Line 树dp+算
主题链接:点击打开链接 题意:白书的P103. 加个虚根就能够了...然后就是一个多重集排列. import java.io.PrintWriter; import java.util.ArrayLi ...
- 【递推】【推导】【乘法逆元】UVA - 11174 - Stand in a Line
http://blog.csdn.net/u011915301/article/details/43883039 依旧是<训练指南>上的一道例题.书上讲的比较抽象,下面就把解法具体一下.因 ...
- UVA 11174 Stand in a Line,UVA 1436 Counting heaps —— (组合数的好题)
这两个题的模型是有n个人,有若干的关系表示谁是谁的父亲,让他们进行排队,且父亲必须排在儿子前面(不一定相邻).求排列数. 我们假设s[i]是i这个节点,他们一家子的总个数(或者换句话说,等于他的子孙数 ...
- uva 11174 Stand in a Line (排列组合)
UVa Online Judge 训练指南的题目. 题意是,给出n个人,以及一些关系,要求对这n个人构成一个排列,其中父亲必须排在儿子的前面.问一共有多少种方式. 做法是,对于每一个父节点,将它的儿子 ...
- UVa 11174 (乘法逆元) Stand in a Line
题意: 有n个人排队,要求每个人不能排在自己父亲的前面(如果有的话),求所有的排队方案数模1e9+7的值. 分析: <训练指南>上分析得挺清楚的,把公式贴一下吧: 设f(i)为以i为根节点 ...
- UVA11174 Stand in a Line
题意 PDF 分析 \[ f(i)=f(c_1)f(c_2)\dots\times(s(i)-1)!/(s(c_1)!s(c_2)! \dots s(c_k)! )\\ f(root)=(s(root ...
随机推荐
- ssh连接超时问题解决方案
方法一: 1.设置服务器向SSH客户端连接会话发送频率和时间 #vi /etc/ssh/sshd_config,添加如下两行ClientAliveInterval 60ClientAliveCount ...
- node.js中文资料导航
以下资料来自gitHUb上面:https://github.com/youyudehexie/node123 Node.js HomePage Node官网七牛镜像 Infoq深入浅出Node.js系 ...
- dev checkedlistbox动态绑定数据
最近在做项目的时候遇到个问题.用checkedlistbox控件绑定数据.在这里稍微总结一下. 其实动态绑定数据有两种方法下面说一下 1.通过数据源 DataTable dt=new DataTabl ...
- jsp页面表单的遍历要怎么写
1.传统的方式使用request.getAttribute(“list”);获取表单的值, 2.也可以用struts2提供的标签进行遍历 备注 // 传统的接受参数方法 // String sfz=t ...
- HTML5 WebAudioAPI(四)--绘制频谱图2
绘制分析器数组所有数据.本文内容,承接上文 1.800宽度绘制 var url='../content/audio/海阔天空.mp3'; if (!window.AudioContext) { ale ...
- javascript google map circle radius_changed ,angularjs google map circle radius_changed
javascript: var cityCircle = new google.maps.Circle({ strokeColor: '#FF0000', strokeOpacity: 0.8, st ...
- c# 双问号运算
model.id??0 ??运算:如果运算符左边的值为NULL侧返回右边的值,否则返回左边的值
- Fluent NHibernate之旅
Fluent NHibernate 之旅 导航篇: [原创]Fluent NHibernate之旅开篇: [原创]Fluent NHibernate之旅二--Entity Mapping: [原创]F ...
- yii2源码学习笔记(四)
继续了解组件Component.php /** * Returns a value indicating whether a property is defined for this componen ...
- 原生Ajax + Promise
有原生写的ajax + promise嫁接下 ;(function(root){ var LD = function(obj){ if( obj instanceof LD ) return obj; ...