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 ...
随机推荐
- css3 怎么实现像书籍装订线的效果
.test{ width: 150px; height: 150px; margin: 20% auto; background-color: white; background-image: rep ...
- [nowCoder] 子数组最大乘积
给定一个double类型的数组arr,其中的元素可正可负可0,返回子数组累乘的最大乘积.例如arr=[-2.5,4,0,3,0.5,8,-1],子数组[3,0.5,8]累乘可以获得最大的乘积12,所以 ...
- 堆栈中的EIP EBP ESP
EIP,EBP,ESP都是系统的寄存器,里面存的都是些地址. 为什么要说这三个指针,是因为我们系统中栈的实现上离不开他们三个. 我们DC上讲过栈的数据结构,主要有以下特点: 后进先处.(这个强调 ...
- BitNami一键安装Redmine(转)
1. 简介 对于一个新手,如果严格按照官方文档来安装redmine,我想会“疯”掉的.有没有一种简便的方法.有滴,那就是BitNami. BitNami提供redmine的一键安装程序,简单.易用.方 ...
- Unity3D脚本中文系列教程(十)
http://dong2008hong.blog.163.com/blog/static/4696882720140312627682/?suggestedreading&wumii Unit ...
- 【Unity3D】iOS 推送实现
原地址:http://www.iappfan.com/%E3%80%90unity3d%E3%80%91ios-%E6%8E%A8%E9%80%81%E5%AE%9E%E7%8E%B0/ #impor ...
- CentOS 6.4 搭建SVN服务器
SVN作为新一代代码版本管理工具,有很多优点,管理方便,逻辑明确,安全性高,代码一致性高.SVN数据存储有两种方式,BDB(事务安全表类型)和FSFS(一种不需要数据库的存储系统),为了避免在服务器连 ...
- (int)、Convert.ToInt32()与int.Parse()的区别
1.(int)是类型转换,能够使用(int)进行强类型转换的只能是数值类型,如long.short.double等,这种转换时需要考虑精度问题. 如下的代码就行不通了: string text = & ...
- What is the difference between Views and Materialized Views in Oracle?
aterialized views are disk based and update periodically base upon the query definition. Views are v ...
- POJ3764 The xor-longest path Trie树
代码写了不到30分钟,改它用了几个小时.先说题意,给你一颗树,边上有权,两点间的路径上的路径的边权抑或起来就是路径的xor值,要求的是最大的这样的路径是多少.讲到树上的两点的xor,一个常用的手段就是 ...