[UVA][合][逆元] 11174 - Stand in a Line@Morris' Blog|PChome Online 人新台
2013-07-12 13:58:10| 人393| 回0 | 上一篇 | 下一篇

[UVA][合][逆元] 11174 - Stand in a Line

0 收藏 0 0 站台

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/p>

3 1

2 1

 

2

6

3

 

 


Problem setter: Abdullah-al-Mahmud

Special Thanks: Derek Kisman

目其就是在求拓排序的方法有。


於一根的方法定 f(root), 其下有 s1, s2, ... , sn 的子。
而定 S(x) x 以下的所有(不包括 x)。

f(root) = f(s1)*f(s2)*f(s3)* ... *f(sn) S(root)!/(S(s1)! S(s2)! S(s3)! ... S(sn)!)

逆元算 n! 在 mod 1000000007 下的值何,用小定理算之。
先 n! mod
1000000007 再去找逆元。

逆元算 /n! 相於乘一它的逆元。


#include <stdio.h>
#include <vector>
#define MOD 1000000007
using namespace std;
long long mpow(long long x, long long y, long long mod) {
    long long ret = 1;
    while(y) {
        if(y&1) ret = ret*x, ret %= mod;
        y >>= 1;
        x = (x*x)%mod;
    }
    return ret;
}
vector<int> g[65536];
long long f[65536] = {1}, vf[65536];
long long dfs(int nd, int &soncnt) {
    sOncnt= 0;
    int ss;
    long long ret = 1;
    for(vector<int>::iterator it = g[nd].begin();
        it != g[nd].end(); it++) {
        ret = (ret*dfs(*it, ss))%MOD;
        ret = (ret*vf[ss])%MOD;
        soncnt += ss;
    }
    ret = (ret*f[soncnt])%MOD;
    soncnt++;
    return ret;
}
int main() {
    int i, j, k;
    int testcase, n, m;
    int son, parent;
    for(i = 1; i < 65536; i++) {
        f[i] = (f[i-1]*i)%MOD;
        vf[i] = mpow(f[i], MOD-2, MOD);
    }
    scanf("%d", &testcase);
    while(testcase--) {
        scanf("%d %d", &n, &m);
        int root[65536] = {};
        for(i = 1; i <= n; i++)
            g[i].clear(), root[i] = 1;
        for(i = 0; i < m; i++) {
            scanf("%d %d", &son, &parent);
            g[parent].push_back(son);
            root[son] = 0;

        }
        long long ret = 1;
        int ss, rcnt = 0;
        for(i = 1; i <= n; i++) {
            if(root[i]) {
                ret = (ret*dfs(i, ss))%MOD;
                ret = (ret*vf[ss])%MOD;
            }
        }
        ret = (ret*f[n])%MOD;
        printf("%lld\n", ret);
    }
    return 0;
}

台: Morris
人(393) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA][greedy] 11158 - Elegant Permuted Sum
此分上一篇:[UVA] 11155 - Be Efficient

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86