HDU4670-Cube number on a tree

来源:互联网 发布:淘宝美食店铺吐血推荐 编辑:程序博客网 时间:2024/06/06 10:39

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 Pi(0 ≤ Pi ≤ 1015) 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
53 2 3 52500 200 9 270000 274 23 52 54 1
 

Sample Output
1
 

Source
2013 Multi-University Training Contest 7
 

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 longconst 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;}

原创粉丝点击