HDU4670-Cube number on a tree

Cube number on a tree

                                                                Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
                                                                                            Total Submission(s): 2019    Accepted Submission(s): 491


Problem Description
The country Tom living in is famous for traveling. Every year, many tourists from all over the world have interests in traveling there.
There are n provinces in the country. According to the experiences from the tourists came before, every province has its own preference value. A route’s preference value from one province to another is defined as the product of all the preference value of the provinces on the route. It’s guaranteed that for each two provinces in the country there is a unique route from one to another without passing any province twice or more.
Tom is a boy crazy about cube number. A cube number is a positive integer whose cube root is also an integer. He is planning to travel from a province to another in the summer vacation and he will only choose the route with the cube number preference value. Now he want to know the number of routes that satisfy his strange requirement.
 

Input
The input contains several test cases, terminated by EOF.
Each case begins with a number n ( 1 ≤ n ≤ 50000), the number of the provinces.
The second line begins with a number K (1 ≤ K ≤ 30), and K difference prime numbers follow. It’s guaranteed that all the preference number can be represented by the product of some of this K numbers(a number can appear multiple times).
The third line consists of n integer numbers, the ith number indicating the preference value P i(0 ≤ P i ≤ 10 15) of the i-th province.
Then n - 1 lines follow. Each line consists of two integers x, y, indicating there is a road connecting province x and province y.
 

Output
For each test case, print a number indicating the number of routes that satisfy the requirement.
 

Sample Input
  
  
5 3 2 3 5 2500 200 9 270000 27 4 2 3 5 2 5 4 1
 

Sample Output
  
  
1
 

Source
 

Recommend
zhuyuanchen520
 

题意:给你一棵树,树上每个点有一个权值,问有多少条路径上点的乘积是一个立方数

解题思路:树分治,因为是一个立方数且每个数能分解成的素数种类不超过30个,所以可以弄一个三进制数来保存到每一个点的情况


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cctype>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 10;

int n, m, a[30], u, v;
int s[maxn], nt[maxn], e[maxn], cnt;
int vis[maxn], mx[maxn], sum[maxn];
LL x, y, val[maxn], Pow[30];

map<LL, LL> mp;

LL f1(LL x, LL y)
{
	LL ans = 0;
	for (int i = 0; i < m; i++)
	{
		int temp = (y % 3 + x % 3) % 3;
		y /= 3, x /= 3;
		ans += 1LL * temp*Pow[i];
	}
	return ans;
}

LL f2(LL x)
{
	LL ans = 0;
	for (int i = 0; i < m; i++)
	{
		int temp = (3 - x % 3) % 3;
		x /= 3;
		ans += 1LL * temp*Pow[i];
	}
	return ans;
}

int dfs(int k, int fa, int p)
{
	int ans = 0;
	sum[k] = 1, mx[k] = 0;
	for (int i = s[k]; ~i; i = nt[i])
	{
		if (vis[e[i]] || e[i] == fa) continue;
		int temp = dfs(e[i], k, p);
		sum[k] += sum[e[i]];
		mx[k] = max(mx[k], sum[e[i]]);
		ans = mx[ans] < mx[temp] ? ans : temp;
	}
	mx[k] = max(mx[k], p - sum[k]);
	return mx[k] < mx[ans] ? k : ans;
}

LL get(int k, int fa, LL p)
{
	LL ans = mp[f2(p)];
	for (int i = s[k]; ~i; i = nt[i])
	{
		if (e[i] == fa || vis[e[i]]) continue;
		ans += get(e[i], k, f1(p, val[e[i]]));
	}
	return ans;
}

void put(int k, int fa, LL p)
{
	mp[p]++;
	for (int i = s[k]; ~i; i = nt[i])
	{
		if (e[i] == fa || vis[e[i]]) continue;
		put(e[i], k, f1(p, val[e[i]]));
	}
}

LL Find(int k)
{
	mp.clear();
	mp[0] = 1;
	LL ans = !val[k] ? 1 : 0;
	for (int i = s[k]; ~i; i = nt[i])
	{
		if (vis[e[i]]) continue;
		ans += get(e[i], k, f1(val[k], val[e[i]]));
		put(e[i], k, val[e[i]]);
	}
	return ans;
}

LL solve(int k, int p)
{
	int y = dfs(k, k, p);
	LL ans = Find(y);
	vis[y] = 1;
	for (int i = s[y]; ~i; i = nt[i])
	{
		if (vis[e[i]]) continue;
		if (sum[e[i]] < sum[y]) ans += solve(e[i], sum[e[i]]);
		else ans += solve(e[i], p - sum[y]);
	}
	return ans;
}

int main()
{
	Pow[0] = 1;
	for (int i = 1; i < 30; i++) Pow[i] = 3 * Pow[i - 1];
	while (~scanf("%d%d", &n, &m))
	{
		mx[cnt = 0] = INF;
		for (int i = 1; i <= n; i++) s[i] = -1, vis[i] = 0, val[i] = 0;
		for (int i = 0; i < m; i++) scanf("%d", &a[i]);
		for (int i = 1; i <= n; i++)
		{
			scanf("%lld", &x);
			for (int j = 0, k; j < m; j++)
			{
				for (k = 0; x%a[j] == 0; k++) x /= a[j];
				val[i] += 1LL * k % 3 * Pow[j];
			}
		}
		for (int i = 1; i < n; i++)
		{
			scanf("%d%d", &u, &v);
			nt[cnt] = s[u], s[u] = cnt, e[cnt++] = v;
			nt[cnt] = s[v], s[v] = cnt, e[cnt++] = u;
		}
		printf("%lld\n", solve(1, n));
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值