UVA - 1436 Counting heaps

Description

We are given a rooted tree of n vertices. The vertices are to be labeled with numbers
1, 2,..., n so that each label is unique and the heap condition holds, i.e. the label of any vertex is less than the label of its parent. How many such labellings exist? Since this number may be quite large, calculate only
its remainder modulo m .

Input

The input contains several tree descriptions. The first line contains the number of input trees
t(t250) . Each tree description begins with a line containing
the size of the tree n(1n500000)
and an integer m(2m109)
. n - 1 lines follow, i -th of which contains
p(i + 1) , the number of the parent of the
i + 1 -th vertex (1p(i + 1)i)
. Vertex number 1 will be the root in each tree, so its parent will not be given. Total size of the input will not exceed 50MB.

Output

For each tree output the number of its valid labellings modulo given
m .

Explanation for sample: The 8 possible labellings from the last example test case are as follows:

Sample Input

4
3 1000000
1
1
4 1000000
1
1
1
5 1000000
1
2
3
4
5 1000000
1
1
3
3

Sample Output

2
6
1
8
题意:给你一棵n个结点的有根树,要求给结点标号1~n,使得不同结点标号不同,且每个非根结点结点的标号比父结点小,求方案数。
思路:这题和UVA - 11174 Stand in a Line是一样的,不过因为m不是素数,所以所以我们要把分子分母分解质因子,然后快速幂取模。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
typedef long long ll;
using namespace std;
const int maxn = 510005;

int pre[maxn], cnt[maxn];
int isleaf[maxn];
int prime[maxn], num_prime, cprime[maxn], isprime[maxn];

queue<int> q;
ll n, mod;

void init() {
	memset(isprime, 1, sizeof(isprime));
	num_prime = 0;
	int cnt = 0;
	isprime[0] = isprime[1] = 0;
	for (int i = 2; i < maxn; i++)
		if (isprime[i]) {
			isprime[i] = num_prime;
			prime[num_prime++] = i;
			for (int j = 2; j * i < maxn; j++)
				isprime[i * j] = 0;
		}
}

void bfs() {
	for (int i = 1; i <= n; i++)
		cnt[i] = 1;
	while (!q.empty()) {
		int u = q.front();
		q.pop();
		if (u == 1)
			continue;
		cnt[pre[u]] += cnt[u];
		isleaf[pre[u]]--;
		if (!isleaf[pre[u]])
			q.push(pre[u]);
	}
}

void fac(int a, int v) {
	for (int i = 0; a != 1; i++) {
		while (a % prime[i] == 0) {
			cprime[i] += v;
			a /= prime[i];
		}
		if (isprime[a]) {
			cprime[isprime[a]] += v;
			break;
		}
	}
}

ll pow_mod(int a, int b) {
	ll x = a, tmp = 1;
	while (b) {
		if (b & 1)
			tmp = (tmp * x) % mod;
		b >>= 1;
		x = (x * x) % mod;
	}
	return tmp;
}

ll solve() {
	memset(cprime, 0, sizeof(cprime));
	for (int i = 2; i < n; i++)
		fac(i, 1);
	for (int i = 2; i <= n; i++)
		fac(cnt[i], -1);
	ll res = 1;
	for (int i = 0; i <= n && res != 0; i++)
		if (cprime[i])
			res = (res * pow_mod(prime[i], cprime[i])) % mod;
	return res;
}

int main() {
	init();
	int t;
	scanf("%d", &t);
	while (t--) {
		scanf("%lld%lld", &n, &mod);
		pre[1] = -1;
		memset(isleaf, 0, sizeof(isleaf));
		for (int i = 2; i <= n; i++) {
			scanf("%d", &pre[i]);
			isleaf[pre[i]]++;
		}
		for (int i = 1; i <= n; i++)
			if (!isleaf[i])
				q.push(i);
		bfs();
		printf("%lld\n", solve());
	}
	return 0;
}

时间: 2025-01-04 09:43:40

UVA - 1436 Counting heaps的相关文章

uva 1436 - Counting heaps(计数)

题目链接:uva 1436 - Counting heaps 题目大意:给出一个树的形状,现在为这棵树标号,保证根节点的标号值比子节点的标号值大,问有多少种标号树. 解题思路:和村名排队的思路是一只的uva11174,最后问题只和树德结构有直接关系,f(root)=(s(root)?1)!(s(1)?s(2)???s(n) 但是给定的取模数不是质数,所以不能用逆元做,只能将分子分母分别拆分成质因子,然后对质因子进制约分.因为最后的答案一定是正整数,所以对于每个质因子,分子分解出的因子个数一定大于

UVA 1436 - Counting heaps(计数问题)

UVA 1436 - Counting heaps 题目链接 题意:给定一个树的结构,放1-n数字进去,父亲结点值必须小于子节点,问情况有几种. 思路:f[u]表示以u为子树的情况,那么子树情况为f(v1), f(v2), f(v3)... f(vn).去组成子树相当于从中选s(v1), s(v2), s(v3) ... s(vn).根据组合数学,情况为f(v1) f(v2) ... f(vn) (s(u) - 1)! \ (s(v1)! s(v2)! ... * s(vn)!)化简后得到公式:

UVA - 12075 Counting Triangles

Description Triangles are polygons with three sides and strictly positive area. Lattice triangles are the triangles all whose vertexes have integer coordinates. In this problem you have to find the number of lattice triangles in anMxN grid. For examp

UVA - 10574 Counting Rectangles

Description Problem H Counting Rectangles Input: Standard Input Output:Standard Output Time Limit: 3Seconds   Given n points on the XY plane, count how many regular rectanglesare formed. A rectangle is regular if and only if its sides are all paralle

uva 12075 - Counting Triangles(容斥原理)

题目链接:uva 12075 - Counting Triangles 题目大意:一个n?m的矩阵,求说有选任意三点,可以组成多少个三角形. 解题思路:任意选三点C(3(n+1)?(m+1))但是有些组合是不可行得,即为三点共线,除了水平和竖直上的组合,就是斜线上的了,dp[i][j]即为ij情况下的斜线三点共线. #include <cstdio> #include <cstring> typedef long long ll; const int N = 1005; ll dp

UVA 12075 - Counting Triangles(容斥原理计数)

题目链接:12075 - Counting Triangles 题意:求n * m矩形内,最多能组成几个三角形 这题和UVA 1393类似,把总情况扣去三点共线情况,那么问题转化为求三点共线的情况,对于两点,求他们的gcd - 1,得到的就是他们之间有多少个点,那么情况数就可以求了,然后还是利用容斥原理去计数,然后累加出答案 代码: #include <stdio.h> #include <string.h> #include <algorithm> using nam

UVA 11123 - Counting Trapizoid(计数问题+容斥)

UVA 11123 - Counting Trapizoid 题目链接 题意:给定一些点,不重复,求出一共有几个梯形 思路:先把所有两点组成直线求出来,然后排序,斜率相同的C2n个,然后再扣除掉重叠的直线情况和长度相等情况(这样为平行四边形或矩形),由于扣除的时候会重复扣掉重叠和相等,所以在加回来,这是容斥原理. 代码: #include <stdio.h> #include <string.h> #include <math.h> #include <algor

uva 11123 - Counting Trapizoid(容斥+几何)

题目链接:uva 11123 - Counting Trapizoid 题目大意:给定若干个点,问有多少种梯形,不包括矩形,梯形的面积必须为正数.因为是点的集合,所以不会优重复的点. 解题思路:枚举两两点,求出该条直线,包括斜率k,偏移值c,以及长度l.已知梯形的性质,一对对边平行,也就是说一对平行但是不相等的边. 所以将所有线段按照斜率排序,假设对于某一斜率,有m条边,那么这m条边可以组成的含平行对边的四边形有C(2m),要求梯形还要减掉长度相同以及共线的情况,分别对应的是l相同和c相同,但是

UVA 10574 - Counting Rectangles(枚举+计数)

10574 - Counting Rectangles 题目链接 题意:给定一些点,求能够成几个矩形 思路:先把点按x排序,再按y排序,然后用O(n^2)的方法找出每条垂直x轴的边,保存这些边两点的y坐标y1, y2.之后把这些边按y1排序,再按y2排序,用O(n)的方法找出有几个连续的y1, y2都相等,那么这些边两两是能构成矩形的,为C2cnt种,然后累加起来就是答案 代码: #include <stdio.h> #include <string.h> #include <