数学: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 ...
随机推荐
- 【IBM】Merlin 给 Java 平台带来了非阻塞 I/O
Merlin 给 Java 平台带来了非阻塞 I/O 新增的功能大幅降低了线程开销 Java 技术平台早就应该提供非阻塞 I/O 机制了.幸运的是,Merlin(JDK 1.4)有一根几乎在各个场合都 ...
- Python建立socket并获取信息
import socket, sys port = 80 host = "www.baidu.com" print "Creating socket..." s ...
- 10.30 afternoon
P76竞赛时间: ????年??月??日??:??-??:?? 题目名称 他 她 它 名称 he she it 输入 he.in she.in it.in 输出 he.out she.out it.o ...
- SPFA 小优化*2
/* bzoj 2763 SPFA小优化 循环队列+SLF 顺面改掉自己之前手打qeueu的坏毛病*/ #include<iostream> #include<cstring> ...
- 全国OA系统下载地址(全)
思道OAhttp://www.anyoffice.net微软.NET平台,支持64位 金和OAhttp://www.jinher.com 红帆OAhttp://www.ioffice.cn 致远OAh ...
- Java排序8大算法实现
概述 排序有内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存. 我们这里说说八大排序就是内部排序. 当n较大, ...
- iOS 集成支付宝遇到的问题(续)
调起支付宝进行支付时提示private key is null ,碰到这个问题有两种解决方案 第一种. 将私钥转成PKCS8替换一下原私钥即可 1.生成私钥pem, 执行命令openssl genr ...
- Python:标准数据类型6种
#!/usr/bin/python3 #python的基本语法和数据类型 #python3中 一行有多个语句,用分号分割(;) print("aaa") ;print(" ...
- SGU 115.Calendar
连水3道,还能更水么... #include <stdio.h> using namespace std; ] = {, , , , , , , , , , , , }; int n, m ...
- PHPCMS(2)PHPCMS V9 环境搭建(转)
转自:http://www.cnblogs.com/Braveliu/p/5072920.html PHPCMS V9的学习总结分为以下几点: [1]PHPCMS 简介 PHP原始为Personal ...