uva 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


#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <cmath>
#include <cstring>
using namespace std;

#define ll long long
const 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总为1
	ll 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值