数学: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 ...
随机推荐
- linux下面安装和配置nginx
下载nginx-1.0.2.tar.gz wget nginx-1.0.2.tar.gz 解压 nginx-1.0.2.tar.gz tar -xzvf nginx-1.0.2.tar.gz 配置安装 ...
- 关于pv的那些事!!
遗留问题:whid=1969的日志记录是什么意思? 网站站点信息未分配的时候,会用1969去代替站点信息. PV:页面浏览量(page view),用户每次打开或刷新一次网页即被计算一次. 关于pv的 ...
- WCF 用netTcpbinding,basicHttpBinding 传输大文件
问题:WCF如何传输大文件 方案:主要有几种绑定方式netTcpbinding,basicHttpBinding,wsHttpbinding,设置相关的传输max消息选项,服务端和客户端都要设置,tr ...
- mysql 安装-编码
mysql的安装过程相对较为简单,在这里就不阐述,我想说的问题是,关于编码的安装, 在安装到达'Please select the default character set'的时候,选择'Manul ...
- Intellij Idea 13 vmoptions (Mac版本)
-ea -server -Xms1g -Xmx1g -Xss16m -XX:PermSize=256m -XX:MaxPermSize=256m -XX:+DoEscapeAnalysis -XX:+ ...
- ios手势复习值之换图片-转场动画(纯代码)
目标:实现通过手势进行图片的切换 通过左扫右扫 来实现(纯代码) 添加三个属性 1uiImageView 用来显示图片的view 2 index 用来表示图片的索引 3 ISLeft 判断是不是向 ...
- wpf 窗体中显示当前系统时间
先看一下效果: 这其实是我放置了两个TextBlock,上面显示当前的日期,下面显示时间. 接下来展示一下代码: 在XAML中: <StackPanel Width="205" ...
- 【转】WF4.0实战系列索引
转自:http://www.cnblogs.com/zhuqil/archive/2010/07/05/wf4-in-action-index.html 此系列的工作流文件案例比较多点,实用性好. W ...
- SGU 200.Cracking RSA(高斯消元)
时间限制:0.25s 空间限制:4M 题意: 给出了m(<100)个数,这m个数的质因子都是前t(<100)个质数构成的. 问有多少个这m个数的子集,使得他们的乘积是完全平方数. Solu ...
- Xcode 使用自定义字体
添加对应的字体(.ttf或.odf)到工程的resurce,使用cocos2d中的FontLabel库,FontLabel继承于UILabel,象UILabel一样使用就好了 fontName直接使用 ...