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 ...
随机推荐
- vue文件中style标签的几个标识符
.vue文件中style标签的几个标识符 在人生就要绝望的时候, 被编辑器所提示的一个scopedSlots所拯救. 卧槽, 写到最后才发现这个属性的具体卵用. 详情见最后解决办法. 问题背景 问题由 ...
- canvas绘制基础
初始接口 <body> <canvas id=“canvas”></canvas> <script> var canvas = document.get ...
- Mysql无法启动服务解决办法
只需要输入:mysqld --initialize 进行初始化,即可启动
- 洛谷 P3178 [HAOI2015]树上操作
题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个操作,分为三种:操作 1 :把某个节点 x 的点权增加 a .操作 2 :把某个节点 x 为根的子树中所有点的点权都增加 ...
- PHP CLI应用的调试原理
我们在Eclipse里选中一个PHP文件,右键选择Debug As->PHP CLI Application. 所谓CLI应用,是指这种脚本文件不需要任何Web服务器即可运行,当然, PHP运行 ...
- python基础一day3 字符串
对字符串进行的任何操作都是形成新的字符串. 切片顾头不顾尾 倒着取: 因为顾头不顾尾,所以4要取到 当步长省略时,可以同时省略最后一个冒号 写0时,取不到,什么都不写,可以取到 倒着取出全部的值,两种 ...
- PHP11 日期和时间
学习要点 UNIX时间戳 将其他格式的日期转成UNIX时间戳格式 基于UNIX时间戳的日期计算 获取并格式化输出日期 修改PHP的默认时间 微秒的使用 Unix时间戳 相关概念 Unix tim ...
- 设计模式:命令模式(Command Pattern)
问题 某个类中需要定义一个方法,该方法要实现的功能不确定的,需要等到程序执行该方法的时候才确定下来. 例如:定义一个计算数组的方法,可能需要遍历输出数组,也有可能是需要对数组中元素求和. 解决方案 按 ...
- CF1179D Fedor Runs for President [DP,斜率优化]
Codeforces 思路 考虑把连的那两个点中间的链提出来,那么就会变成一条链,链上的每个点挂着一棵子树的形式. 设那些子树的大小为\(S_1,S2,\cdots\),那么新加的简单路径个数就是 \ ...
- node如何导出数据成为excel格式
node的应用方式,导出数据 首先,你要把数据库连接上,把你要导的数据表写出来 安装模块 $ npm install sequelize $ npm install mysql $ npm insta ...