Puzzled Elena

Time Limit: 2500ms
Memory Limit: 131072KB

This problem will be judged on HDU. Original ID: 5468
64-bit integer IO format: %I64d      Java class name: Main

Since both Stefan and Damon fell in love with Elena, and it was really difficult for her to choose. Bonnie, her best friend, suggested her to throw a question to them, and she would choose the one who can solve it.

Suppose there is a tree with n vertices and n - 1 edges, and there is a value at each vertex. The root is vertex 1. Then for each vertex, could you tell me how many vertices of its subtree can be said to be co-prime with itself?
NOTES: Two vertices are said to be co-prime if their values' GCD (greatest common divisor) equals 1.

Input
There are multiply tests (no more than 8).
For each test, the first line has a number n $(1\leq n\leq 10^5)$, after that has n−1 lines, each line has two numbers a and b$ (1\leq a,b\leq n)$, representing that vertex a is connect with vertex b. Then the next line has n numbers, the ith number indicates the value of the ith vertex. Values of vertices are not less than 1 and not more than $10^5$.

Output
For each test, at first, please output "Case #k: ", k is the number of test. Then, please output one line with n numbers (separated by spaces), representing the answer of each vertex.

Sample Input

5
1 2
1 3
2 4
2 5
6 2 3 4 5

Sample Output

Case #1: 1 1 0 0 0

Source

 
解题:莫比乌斯反演
 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
bool np[maxn] = {true,true};
int mu[maxn],p[maxn],tot;
vector<int>fac[maxn],g[maxn];
void mobius(int n) {
mu[] = ;
for(int i = ; i <= n; ++i) {
if(!np[i]) {
p[tot++] = i;
mu[i] = -;
}
for(int j = ; j < tot && p[j]*i <= n; ++j) {
np[p[j]*i] = true;
if(i%p[j] == ) {
mu[p[j]*i] = ;
break;
}
mu[p[j]*i] = -mu[i];
}
}
for(int i = ; i <= n; ++i) if(mu[i])
for(int j = i; j <= n; j += i)
fac[j].push_back(i);
}
int val[maxn],cnt[maxn],sz[maxn],ans[maxn];
void dfs(int u,int fa) {
sz[u] = ;
vector<int>pre;
for(int &c:fac[val[u]]) {
pre.push_back(cnt[c]);
++cnt[c];
}
for(auto &v:g[u]) {
if(v == fa) continue;
dfs(v,u);
sz[u] += sz[v];
}
ans[u] = sz[u];
for(int i = ; i < fac[val[u]].size(); ++i) {
int x = fac[val[u]][i];
int y = cnt[x] - pre[i];
ans[u] += mu[x]*y;
}
}
int main() {
int n,u,v,cs = ;
mobius();
while(~scanf("%d",&n)) {
for(int i = ; i <= n; ++i) g[i].clear();
for(int i = ; i < n; ++i) {
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
for(int i = ; i <= n; ++i)
scanf("%d",val + i);
memset(cnt,,sizeof cnt);
dfs(,);
printf("Case #%d:",cs++);
for(int i = ; i <= n; ++i)
printf(" %d",ans[i]);
putchar('\n');
}
return ;
}

HDU 5468 Puzzled Elena的更多相关文章

  1. HDU 5468 Puzzled Elena (dfs + 莫比乌斯反演)

    题意:给定一棵带权树,求每个点与其子树结点的权值互质的个数. 析:首先先要进行 dfs 遍历,len[i] 表示能够整除 i 的个数,在遍历的前和遍历后的差值就是子树的len值,有了这个值,就可以使用 ...

  2. HDU 5468 Puzzled Elena 莫比乌斯反演

    题意: 给出一棵树,每个点上有权值.然后求每棵子树中与根节点互质( \(gcd(a, b) = 1\) )的节点个数. 分析: 对于一颗子树来说,设根节点的权值为\(u\), \(count_i\)表 ...

  3. hdu 5468(莫比乌斯+搜索)

    hdu 5468 Puzzled Elena   /*快速通道*/ Sample Input 5 1 2 1 3 2 4 2 5 6 2 3 4 5   Sample Output Case #1: ...

  4. hdu5468 Puzzled Elena

    hdu5468 Puzzled Elena 题意 求一棵子树内与它互质的点个数 解法 容斥 我们先求出与它不互质的数的个数,再用总数减去就好. #include <cstdio> #inc ...

  5. hdu 5468(dfs序+容斥原理)

    Puzzled Elena Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  6. 2015上海网络赛 A Puzzled Elena

    题意:给定一棵树,求这个节点的所有子树中包括他本身与它互质的节点的个数. 解题思路:题利用dfs序+容斥原理+前缀和性质解决.题目中要求每个结点,和多少个它的子结点互素.如果每次为了求一个点去跑一遍d ...

  7. 2015 ACM/ICPC Asia Regional Shanghai Online

    1001 Puzzled Elena 1002 Antonidas 1003 Typewriter 1004 Count the Grid 1005 Code Formatting 1006 Ther ...

  8. dfs序题目练习

    参考博文:http://blog.csdn.net/qwe2434127/article/details/49819975 http://blog.csdn.net/qq_24489717/artic ...

  9. HDU5468(dfs序+容斥原理)

    Puzzled Elena Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

随机推荐

  1. java自带线程池

    1. newSingleThreadExecutor 创建一个单线程的线程池.这个线程池只有一个线程在工作,也就是相当于单线程串行执行所有任务.如果这个唯一的线程因为异常结束,那么会有一个新的线程来替 ...

  2. 使用statement接口实现增,删,改操作

  3. 486 Predict the Winner 预测赢家

    给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端拿取分数,然后玩家1拿,…….每次一个玩家只能拿取一个分数,分数被拿取之后不再可取.直到没有剩余分数 ...

  4. 证明碰撞集问题(Hitting Set)是NP-complete

    证明碰撞集问题(Hitting Set)是NP-complete Problem In the HITTING SET problem, we are given a family of sets { ...

  5. Struts2控制文件的上传与下载

    Struts2控制文件上传与下载的几个注意事项: (1)必须将表单的method设置为post,将enctype设置为multipart/from-data.只有这样,浏览器才会把用户选择文件的二进制 ...

  6. Elasticsearch插件清单

    1.API插件:主要对Elasticsearch添加的API特性或者功能,通常用于搜索或者映射 2. 报警插件: 当Elasticsearch的索引指标超过阀值时就会触发 3. 分词插件:ik是比较好 ...

  7. Android(java)学习笔记172:服务(service)之绑定服务调用服务里面的方法 (采用接口隐藏代码内部实现)

    1. 接口 接口可以隐藏代码内部的细节,只暴露程序员想暴露的方法 2. 利用上面的思想优化之前的案例:服务(service)之绑定服务调用服务里面的方法,如下: (1)这里MainActivity.j ...

  8. Bug的定义和分类

    什么是BUG 使用人工或自动手段,来运行或测试某个系统的过程.其目的在于检验它是否满足规定的需求或弄清预期结果与实际结果之间的差别 BUG分类 完全没有实现的功能 基本实现了用户需要的功能,但是运行时 ...

  9. H5里div多行显示省略号

    display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: ; overflow: hidden; -webkit- ...

  10. 欧拉函数 || LightOJ 1370 Bi-shoe and Phi-shoe

    给出x,求最小的y使y的欧拉函数大于等于x *解法:i).求出1e6之内的数的欧拉函数,遍历找             ii).求比x大的第一个质数——因为每个质数n的欧拉函数都是n-1 wa一次是因 ...