uva 11174 - Stand in a Line(求乘法逆元)
来源:互联网 发布:android 6.0系统源码 编辑:程序博客网 时间:2024/06/09 19:40
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
#include <iostream>#include <cstdio>#include <vector>#include <map>#include <cmath>#include <cstring>using namespace std;#define ll long longconst int maxn = 40003;const int mod = 1000000007;struct person{int father , sum;ll ans;vector<int> son;}p[maxn];int n , m;ll fac_mod[maxn] , inverse_mod[maxn];int gcd(int a,int b,int &x,int &y){ int ans; if(!b){ x=1; y=0; return a; } ans = gcd(b,a%b,x,y); int temp = x; x= y; y= temp - (a/b)*y; return ans;}ll get_inverse(int b){int x , y;//a,b的逆元//c总为1ll ans_x , ans_y;int temp = gcd(mod,b,x,y);ans_x = x <0 ? x+b : x;ans_y = y <0 ? y+mod : y;return ans_y;}void initial(){fac_mod[0] = 1;inverse_mod[0] = 1;for(int i = 1; i < maxn; i++){fac_mod[i] = (fac_mod[i-1]*i)%mod;inverse_mod[i] = (inverse_mod[i-1]*get_inverse(i))%mod;}//cout << get_inverse(11) << endl;//cout << fac_mod[40000] << endl;}void ini(){for(int i = 0; i < maxn; i++){p[i].father = 0;p[i].sum = 0;p[i].ans = 1;p[i].son.clear();}}void readcase(){scanf("%d%d" , &n , &m);int f , s;for(int i = 0; i < m; i++){scanf("%d%d" , &s , &f);p[s].father = f;p[f].son.push_back(s);}for(int i = 1; i <= n; i++){if(p[i].father == 0){p[0].son.push_back(i);}}}void dfs(int i){for(int k = 0; k < p[i].son.size(); k++){int s = p[i].son[k];dfs(s);p[i].sum += p[s].sum;p[i].ans = ((p[i].ans%mod)*(p[s].ans%mod))%mod;p[i].ans = ((((p[i].ans*fac_mod[p[i].sum])%mod*inverse_mod[p[s].sum])%mod*inverse_mod[p[i].sum-p[s].sum])%mod)%mod;//cout << i << " " << p[i].sum << " " << p[s].sum << endl;//cout << fac_mod[p[i].sum]<<"+"<<inverse_mod[p[s].sum]<<"+"<<inverse_mod[p[i].sum-p[s].sum] << endl;}p[i].sum += 1;}void computing(){dfs(0);printf("%lld\n" , p[0].ans);}int main(){initial();int T;scanf("%d" , &T);while(T--){ini();readcase();computing();}return 0;}
- uva 11174 - Stand in a Line(求乘法逆元)
- 数学,乘法逆元(Stand in a Line,uva 11174)
- uva 11174 - Stand in a Line(逆元+递推)
- Uva 11174 Stand in a Line 解题报告(递推+逆元)
- UVA11174 J.Stand in a Line (计数+逆元)
- UVA - 11174 Stand in a Line
- uva 11174 Stand in a Line
- UVa 11174 Stand in a Line
- UVA 11174 Stand in a Line
- UVA 11174 Stand in a Line 树形dp+计数
- UVA 11174 - Stand in a Line(组合递归计数)
- uvaoj 11174 - Stand in a Line
- 【UVA11174】Stand in a Line——逆元+树形Dp
- Stand in a Line UVA11174
- UVA11174 Stand in a Line
- uva11174 Stand in a Line
- UVA 11174 - Stand in a Line (数学基础+除法取模)
- ACM: uva 11174 - Stand in a Line
- 第五章 Ogre场景管理器(2)
- 事件(event)
- Using Perl6 第二章:The Basics
- vs2010 +Ogre配置
- Struts個人總結
- uva 11174 - Stand in a Line(求乘法逆元)
- Codeforces 330A Cakeminator(模拟)
- 计科《算法设计与分析》第二周作业-公钥加密 与 数字签名 之间的联系与区别(补)
- awk的使用
- 进程资源信息 struct rusage
- MaiterStore awesome results
- 小鑫の日常系列故事(七)——小纸条
- 实列1
- SQL Server中的sysobjects