HDU 5468 Puzzled Elena
Puzzled Elena
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的更多相关文章
- HDU 5468 Puzzled Elena (dfs + 莫比乌斯反演)
题意:给定一棵带权树,求每个点与其子树结点的权值互质的个数. 析:首先先要进行 dfs 遍历,len[i] 表示能够整除 i 的个数,在遍历的前和遍历后的差值就是子树的len值,有了这个值,就可以使用 ...
- HDU 5468 Puzzled Elena 莫比乌斯反演
题意: 给出一棵树,每个点上有权值.然后求每棵子树中与根节点互质( \(gcd(a, b) = 1\) )的节点个数. 分析: 对于一颗子树来说,设根节点的权值为\(u\), \(count_i\)表 ...
- 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: ...
- hdu5468 Puzzled Elena
hdu5468 Puzzled Elena 题意 求一棵子树内与它互质的点个数 解法 容斥 我们先求出与它不互质的数的个数,再用总数减去就好. #include <cstdio> #inc ...
- hdu 5468(dfs序+容斥原理)
Puzzled Elena Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- 2015上海网络赛 A Puzzled Elena
题意:给定一棵树,求这个节点的所有子树中包括他本身与它互质的节点的个数. 解题思路:题利用dfs序+容斥原理+前缀和性质解决.题目中要求每个结点,和多少个它的子结点互素.如果每次为了求一个点去跑一遍d ...
- 2015 ACM/ICPC Asia Regional Shanghai Online
1001 Puzzled Elena 1002 Antonidas 1003 Typewriter 1004 Count the Grid 1005 Code Formatting 1006 Ther ...
- dfs序题目练习
参考博文:http://blog.csdn.net/qwe2434127/article/details/49819975 http://blog.csdn.net/qq_24489717/artic ...
- HDU5468(dfs序+容斥原理)
Puzzled Elena Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
随机推荐
- Maxim Buys an Apartment CodeForces - 854B
题意:已知一条街上有n幢房子,依次的编号为1~n,其中有k幢已经卖出去了但是不知道是哪k幢.当且仅当一幢房子没有卖出去且其两旁至少有一幢房子卖出去了的时候,认为这幢房子是好的.问这n幢房子中好的房子最 ...
- net MVC 四种基本 Filter
四种基本 Filter 概述 MVC框架支持的Filter可以归为四类,每一类都可以对处理请求的不同时间点引入额外的逻辑处理.这四类Filter如下表: 使用内置的Authorization Fi ...
- hihocoder1067 最近公共祖先·二
思路: 使用tarjan算法,这是一种离线算法. 实现: #include <bits/stdc++.h> using namespace std; typedef pair<int ...
- 伟景行 citymaker 从入门到精通(3)——点击地图获取坐标,点击模型获取模型信息和属性信息
在地图上点击要素查看要素信息,也就是identify功能 script部分 // 通过枚举索引获取枚举名称 function getEnumTextByIdx(enumObj, idx) { for ...
- Win10 系统安装Sql Server2008 R2 数据库遇到的问题及解决办法总结!
1.开始安装时,提示要先安装 “.NET Framework 3.5(包括.NET 2.0和3.0)”,之前已经下载好.NET Framework 3.5 sp1,安装时还是提示要先安装 “.NET ...
- Redis学习笔记(六)有序集合进阶
1.基础操作 ZCARD(获取成员数量) ZINCRBY key_name num member(将member的分数加num) ZCOUNT key_name min max(获取分数在min与ma ...
- css3纯手写loading效果
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Mysql无法启动服务解决办法
只需要输入:mysqld --initialize 进行初始化,即可启动
- WPF知识点全攻略00- 目录
知识点目录如下: 1.WPF相对WinFrom的优缺点 2.WPF体系结构 3.XAML 4.XAML页面布局 5.XAML内容控件 6.WPF中的“树” 7.Binding 8.依赖属性 9.附加属 ...
- 【整理】 vue-cli 打包后显示favicon.ico小图标
vue-cli 打包后显示favicon.ico小图标 https://www.cnblogs.com/mmzuo-798/p/9285013.html