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 ...
随机推荐
- 浅析游戏引擎的资源管理机制——扒一扒Unity3D中隐藏在背后的资源管理
游戏中通常有大量资源,如网格.材质.纹理.动画.着色器程序和音乐等,游戏引擎作为做游戏的工具,自然要提供良好的资源管理,让游戏开发者用最简单的方式使用资源.游戏引擎的资源管理包括两大部分:离线资源管理 ...
- 2876: [Noi2012]骑行川藏 - BZOJ
Description 蛋蛋非常热衷于挑战自我,今年暑假他准备沿川藏线骑着自行车从成都前往拉萨.川藏线的沿途有着非常美丽的风景,但在这一路上也有着很多的艰难险阻,路况变化多端,而蛋蛋的体力十分有限,因 ...
- mysql Host ‘XXXXXX’ is blocked because of many connection errors
mysql Host ‘XXXXXX’ is blocked because of many connection errors ERROR 1129 (00000): Host ‘XXXXXX’ i ...
- php缓存
APC缓存退出舞台,APCU诞生,OPCACHE升级! 对于php5.5以后的新版本开发,使用apc习惯的开发者可能会发现php.5.5以后找不到了这个组件的更新,尤其是中文phper都找不到合适的资 ...
- uva 11375
思路是刘书上的 但是个高精度 java 大数 ~~ import java.util.*; import java.io.*; import java.math.BigInteger; public ...
- uva 991
卡特兰数 最后不输出空行... #include <cstdio> #include <cstdlib> #include <cmath> #include &l ...
- java调用dll文件中的类型转换
char *转String (env)->NewStringUTF("the content you want to type in"); char *转jbyteArr ...
- IOS版UC我的视频地址
UC浏览器/Library/Application Support/offlineVideos
- 【hadoop2.6.0】用C++ 编写mapreduce
hadoop通过hadoop streaming 来实现用非Java语言写的mapreduce代码. 对于一个一点Java都不会的我来说,这真是个天大的好消息. 官网上hadoop streaming ...
- SQL 中的游标实例
--声明变量 declare @IMType varchar(10),@IMResourceID varchar(10) --定义游标 declare information_cursor curso ...