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. Bits Equalizer UVA - 12545

    点击打开链接 #include<cstdio> #include<cstring> /* 别看错了:0能变1,1不能变0 能完成的条件是,s与t长度相等且s中0数量和?数量之和 ...

  2. req.getParameter()无法获取参数(附前端json序列化)

    问题:前端用Ajax的post方式想servlet传递参数,servlet的getParameter()方法无法获取参数. 前端代码: $.ajax({ url: '/TestWeb/addBook' ...

  3. string类常用方法3

  4. AJPFX总结抽象类和接口的区别

    /*                 * 抽象类和接口的区别                 *                 1.成员的区别                         *   ...

  5. poj3262 Protecting the Flowers

    思路: 简单贪心,每次选择性价比最高的. 实现: #include <iostream> #include <cstdio> #include <algorithm> ...

  6. 利用JSTL重写查询的jsp页面

    利用JSTL重写Java Web MVC实例中的jsp页面 第一步:导入jstl.jar和standard.jar文件

  7. Python 中列表、元祖、字典

    1.元祖: 对象有序排列,通过索引读取读取, 对象不可变,可以是数字.字符串.列表.字典.其他元祖 2.列表: 对象有序排列,通过索引读取读取, 对象是可变的,可以是数字.字符串.元祖.其他列表.字典 ...

  8. postman设置环境变量、全局变量

    讲postman环境变量设置之前,先讲一个小插曲,环境变量.全局变量的区别在于Globals,只能用一组,而Environmen可以设置多组,所以我更喜欢设置环境变量 1.环境变量-Environme ...

  9. Linux的Network Tunnel技术

    Linux的Network Tunnel技术 概要 Linux上可以使用ip tunnel命令创建多种类型的tunnel. 在 man ip-tunnel 中可以得知以下几种类型的tunnel: MO ...

  10. net core 使用ef生成实体类(SqlServer)

    1)打开程序包管理器控制台 2)输入命令  Install-Package Microsoft.EntityFrameworkCore.SqlServer 3)输入命令  Install-Packag ...