UVa 11174 (乘法逆元) Stand in a Line
题意:
有n个人排队,要求每个人不能排在自己父亲的前面(如果有的话),求所有的排队方案数模1e9+7的值。
分析:
《训练指南》上分析得挺清楚的,把公式贴一下吧:
设f(i)为以i为根节点的子树的排列方法,s(i)表示以i为根的子树的节点总数。
f(i) = f(c1)f(c2)...f(ck)×(s(i)-1)!/(s(c1)!s(c2)!...s(ck)!)
按照书上最开始举的例子,其实这个式子也不难理解,就是先给这些子树确定一下位置,即有重元素的全排列。
子树的位置确定好以后,然后再确定子树中各个节点的顺序。
对了,因为求组合数会用到除法,而且1e9+7又是个素数,所以我们在做除法的时候只要乘上它对应的乘法逆元即可。
这是递归计算的代码:
#include <bits/stdc++.h> using namespace std; const int maxn = + ;
const int MOD = ; vector<int> sons[maxn];
int fa[maxn], fac[maxn], ifac[maxn]; inline int mul_mod(int a, int b, int n = MOD)
{
a %= n; b %= n;
return (int)((long long)a * b % n);
} void gcd(int a, int b, int& d, int& x, int& y)
{
if(!b) { d = a; x = ; y = ; }
else{ gcd(b, a%b, d, y, x); y -= x*(a/b); }
} int inv(int a, int n)
{
int d, x, y;
gcd(a, n, d, x, y);
return d == ? (x+n)%n : -;
} int C(int n, int m)
{ return mul_mod(mul_mod(fac[n], ifac[m]), ifac[n-m]); } void init()
{
fac[] = ifac[] = ;
for(int i = ; i < maxn; i++)
{
fac[i] = mul_mod(fac[i - ], i);
ifac[i] = inv(fac[i], MOD);
}
} int count(int u, int& size)//size是u为根的子树的节点总数
{//统计u为根的子树的排列方案
size = ;
int ans = ;
int d = sons[u].size();
vector<int> sonsize;
for(int i = ; i < d; i++)
{
int sz;
ans = mul_mod(ans, count(sons[u][i], sz));
size += sz;
sonsize.push_back(sz);
}
int sz = size - ;
for(int i = ; i < d; i++)
{
ans = mul_mod(ans, C(sz, sonsize[i]));
sz -= sonsize[i];
}
return ans;
} int main()
{
//freopen("in.txt", "r", stdin); init();
int T;
scanf("%d", &T);
while(T--)
{
int n, m;
scanf("%d%d", &n, &m);
memset(fa, , sizeof(fa));
for(int i = ; i <= n; i++) sons[i].clear();
for(int i = ; i < m; i++)
{
int a, b;
scanf("%d%d", &a, &b);
fa[a] = b;
sons[b].push_back(a);
}
for(int i = ; i <= n; i++)
if(!fa[i]) sons[].push_back(i);
int size;
printf("%d\n", count(, size));
} return ;
}
代码君
书上后面又提到如果将上式完全展开,每个非根节点u都以(s(u)-1)!出现在分子一次,又以s(u)!出现在分母一次,约分后就只剩分母一个s(u)了。
这样f(root) = (s(root)-1)!/(s(1)s(2)...s(n)),又因为s(root) = n+1(因为除了n个人还有一个虚拟的0祖宗节点),
所以f(root) = n!/(s(1)s(2)...s(n)),还是用递归算出所有的s(i)
这样预处理一下40000以内的阶乘和乘法逆元即可。
#include <bits/stdc++.h> using namespace std; const int maxn = + ;
const int MOD = ; int n, m;
vector<int> sons[maxn];
int fa[maxn], fac[maxn], ifac[maxn], inverse[maxn], sonsize[maxn]; inline int mul_mod(int a, int b, int n = MOD)
{
a %= n; b %= n;
return (int)((long long)a * b % n);
} void gcd(int a, int b, int& d, int& x, int& y)
{
if(!b) { d = a; x = ; y = ; }
else{ gcd(b, a%b, d, y, x); y -= x*(a/b); }
} int inv(int a, int n = MOD)
{
int d, x, y;
gcd(a, n, d, x, y);
return d == ? (x+n)%n : -;
} int C(int n, int m)
{ return mul_mod(mul_mod(fac[n], ifac[m]), ifac[n-m]); } void init()
{
fac[] = ifac[] = ;
for(int i = ; i < maxn; i++)
{
fac[i] = mul_mod(fac[i - ], i);
inverse[i] = inv(i);
}
} void count(int u, int& size)
{//统计以u为根的子树节点个数
size = ;
int d = sons[u].size();
for(int i = ; i < d; i++)
{
int sz;
count(sons[u][i], sz);
size += sz;
}
sonsize[u] += size;
} int main()
{
//freopen("in.txt", "r", stdin); init();
int T;
scanf("%d", &T);
while(T--)
{
memset(fa, , sizeof(fa));
memset(sonsize, , sizeof(sonsize));
for(int i = ; i <= n; i++) sons[i].clear(); scanf("%d%d", &n, &m);
for(int i = ; i < m; i++)
{
int a, b;
scanf("%d%d", &a, &b);
fa[a] = b;
sons[b].push_back(a);
}
for(int i = ; i <= n; i++)
if(!fa[i]) sons[].push_back(i);
int size;
count(, size);
//for(int i = 1; i <= n; i++) printf("%d\n", sonsize[i]);
int ans = fac[n];
for(int i = ; i <= n; i++) ans = mul_mod(ans, inverse[sonsize[i]]);
printf("%d\n", ans);
} return ;
}
代码君
UVa 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 & ...
- 数学:UVAoj 11174 Stand in a Line
Problem J Stand in a Line Input: Standard Input Output: Standard Output All the people in the bytela ...
- Hdu 1452 Happy 2004(除数和函数,快速幂乘(模),乘法逆元)
Problem Description Considera positive integer X,and let S be the sum of all positive integer diviso ...
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 F. Trig Function(切比雪夫多项式+乘法逆元)
题目链接:哈哈哈哈哈哈 _(:з」∠)_ _(:з」∠)_ _(:з」∠)_ _(:з」∠)_ _(:з」∠)_ 哈哈哈哈哈哈,从9月16日打了这个题之后就一直在补这道题,今天终于a了,哈哈哈哈哈哈. ...
- 【ZOJ 3609】Modular Inverse 最小乘法逆元
The modular modular multiplicative inverse of an integer a modulo m is an integer x such that a-1≡x ...
- A. On The Way to Lucky Plaza 概率 乘法逆元
A. On The Way to Lucky Plaza time limit per test 1.0 s memory limit per test 256 MB input standard i ...
- HDU 3923 Invoker(polya定理+乘法逆元(扩展欧几里德+费马小定理))
Invoker Time Limit : 2000/1000ms (Java/Other) Memory Limit : 122768/62768K (Java/Other) Total Subm ...
- hdu_1452_Happy 2004 (乘法逆元
Consider a positive integer X,and let S be the sum of all positive integer divisors of 2004^X. Your ...
随机推荐
- svn:Repository UUID 'XXX' doesn't match expected UUID 'YYY'
About a month ago, CodePlex have upgraded their TFS servers to to TFS 2010. While this transition wa ...
- [设计模式] 16 迭代器模式 Iterator Pattern
在GOF的<设计模式:可复用面向对象软件的基础>一书中对迭代器模式是这样说的:提供一种方法顺序访问一个聚合对象中各个元素,而又不需要暴露该对象的内部表示. 类图和实例: 迭代器模式由以下角 ...
- 汇编中Enter与Leave指令
Enter的作用相当==push ebp和mov ebp,esp 这后面两句大家很熟悉吧?函数开始一般都是这两句 Leave的作用相当==mov esp,ebp和pop ebp 而这后面这两句也很常见 ...
- hadoop浅尝 hadoop与hbase交互
在安装好hbase之后,运行一个与hadoop无关的纯hbase程序成功了. 接着写一个hadoop与hbase进行交互的小程序,这个程序的运行方法依然与前文相同, 即导出jar文件在shell下运行 ...
- lua语言入门之Sublime Text设置lua的Build System
转自: http://blog.csdn.net/wangbin_jxust/article/details/8911956 最近开始学习LUA语言,使用Sublime Text作为编辑器,不得不说, ...
- POJ 1486 Sorting Slides(寻找必须边)
题意:找出幻灯片与编号唯一对应的情况 思路: 1:求最大匹配,若小于n,则答案为none,否则转2 (不过我代码没有事先判断一开始的最大匹配数是否<n,但这样也过了,估计给的数据最大匹配数一定为 ...
- java String 空指针异常
如下代码中,第8行和第10行均会提示Exception in thread "main" java.lang.NullPointerException. 第12行的写法可行. im ...
- 李洪强iOS开发支付集成之银联支付
iOS开发支付集成之银联支付 银联官网在这里,这里能下载SDK或者是看文档.最新的版本写的简单了很多,看文档一直做下去基本上就没问题了. 首先,SDK在这里下载,里面包含需要的库文件和详细的文档. 银 ...
- Java学习笔记之:Java的变量
一.介绍 在Java语言中,所有的变量在使用前必须声明.声明变量的基本格式如下: type identifier [ = value][, identifier [= value] ...] ; 格式 ...
- Xamarin.Android 入门之:Listview和adapter
一.引言 不管开发什么软件,列表的使用是必不可少的,而本章我们将学习如何使用Xamarin去实现它,以及如何使用自定义适配器.关于xamarin中listview的基础和适配器可以查看官网https: ...