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.
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.
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;}
阅读全文
0 0
- HDU4670 Cube number on a tree
- HDU4670-Cube number on a tree
- hdu4670 Cube number on a tree(点分治)
- HDU 4670 Cube number on a tree
- HDOJ 4670: Cube number on a tree
- hdu 4670 Cube number on a tree
- HDU 4670 Cube number on a tree
- hdu 4670 Cube number on a tree(点分治)
- 【HDU】4670 Cube number on a tree 点分治
- 【树的分治】 HDU 4670 Cube number on a tree
- HDU 4670 Cube number on a tree ( 树的点分治 )
- hdu 4670 Cube number on a tree,平衡树,启发式合并
- 2013 多校第七场 hdu 4670 Cube number on a tree(树上点的分治)
- HackerRank - number-game-on-a-tree
- hdu4670
- Draw a cube on Excel Worksheet
- spoj375Query on a tree
- Query on A Tree
- 并查集解1335 : Email Merge(hihocoder)
- C语言学习笔记(五)——复合类型
- 算法之选择排序
- ECLIPSE生成JAVADOC时出错:编码GBK的不可映射字符
- 回车事件
- HDU4670-Cube number on a tree
- hdu 2639 (Bone Collector II) 0-1背包的第k优解
- NOIP 2002 均分纸牌
- Filter过滤器
- 《大话设计模式》读书笔记之C++实现--chapter22桥接模式
- Gym 101389. B
- 关于Switch控件的使用
- Saltstack安装LAMP
- codeforces 870C. Maximum splitting