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)!)化简后得到公式:

f(u) = (s(root) - 1)! / s(1)s(2)s(3)...s(n)

由于答案很大要模上m,但是m不是素数,不能直接求逆,只能把分子分母分别分解质因子去消,最后得到都是分子形式在去取模。

代码:

#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;

const int N = 500005;
int t, n, cnt[N], prime[N], vis[N], pn = 0, f[N], ispri[N];
long long m;

int bfs() {//用dfs必然RE
	queue<int> Q;
	for (int i = 1; i <= n; i++)
		if (!vis[i]) Q.push(i);
 	while (!Q.empty()) {
 		int now = Q.front();
 		Q.pop();
 		if (now == 0) break;
 		cnt[f[now]] += cnt[now];
 		vis[f[now]]--;
 		if (vis[f[now]] == 0)
 			Q.push(f[now]);
  	}
}

void solve(int num, int v) {
	for (int i = 0; i < pn && prime[i] <= num; i++) {
		while (num % prime[i] == 0) {
			cnt[prime[i]] += v;
			num /= prime[i];
  		}
  		if (ispri[num]) {//不加此剪枝必然TLE
  			cnt[num] += v;
     		        break;
    	        }
 	}
}

long long pow_mod(long long x, int k) {
	long long ans = 1;
	while (k) {
		if (k&1) ans = ans * x % m;
		x = x * x % m;
		k >>= 1;
 	}
	return ans;
}

int main() {
	for (int i = 2; i < N; i++) {
		if (vis[i]) continue;
		ispri[i] = 1;
		prime[pn++] = i;
		for (int j = i; j < N; j += i)
			vis[j] = 1;
 	}
	scanf("%d", &t);
	while (t--) {
		scanf("%d%lld", &n, &m);
		for (int i = 1; i <= n; i++)
			cnt[i] = 1;
		f[1] = 0;
		memset(vis, 0, sizeof(vis));
		for (int i = 2; i <= n; i++) {
			scanf("%d", &f[i]);
			vis[f[i]]++;
  		}
  		bfs();
    	memset(vis, 0, sizeof(vis));
  		for (int i = 1; i <= n; i++)
  			vis[cnt[i]]++;
		memset(cnt, 0, sizeof(cnt));
		for (int i = 2; i <= n; i++) {
			solve(i, 1);
			if (vis[i])
   				solve(i, -vis[i]);
		}
		long long ans = 1;
		for (int i = 0; i < pn; i++) {
			if (cnt[prime[i]] == 0) continue;
  			ans = (ans * pow_mod((long long)prime[i], cnt[prime[i]])) % m;
		}
  		printf("%lld\n", ans);
 	}
	return 0;
}

UVA 1436 - Counting heaps(计数问题)

时间: 2024-10-24 22:09:42

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

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

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

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

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 题目大意:给定若干个点,问有多少种梯形,不包括矩形,梯形的面积必须为正数.因为是点的集合,所以不会优重复的点. 解题思路:枚举两两点,求出该条直线,包括斜率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 <