HDU 5468 Puzzled Elena (dfs + 莫比乌斯反演)
题意:给定一棵带权树,求每个点与其子树结点的权值互质的个数。
析:首先先要进行 dfs 遍历,len[i] 表示能够整除 i 的个数,在遍历的前和遍历后的差值就是子树的len值,有了这个值,就可以使用莫比斯反演了。注意如果子树的权值是1,还要加上它本身。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
//#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const int maxm = 2e4 + 10;
const LL mod = 100000007;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} bool vis[maxn];
int prime[maxn], mu[maxn];
vector<int> factor[maxn];
void Moblus(){
mu[1] = 1; int tot = 0;
for(int i = 2; i < maxn; ++i){
if(!vis[i]) prime[tot++] = i, mu[i] = -1;
for(int j = 0; j < tot; ++j){
int t = i * prime[j];
if(t >= maxn) break;
vis[t] = 1;
if(i % prime[j] == 0) break;
mu[t] = -mu[i];
}
}
for(int i = 1; i < maxn; ++i) if(mu[i])
for(int j = i; j < maxn; j += i) factor[j].pb(i);
} struct Edge{
int to, next;
};
Edge edges[maxn<<1];
int head[maxn], cnt; void addEdge(int u, int v){
edges[cnt].to = v;
edges[cnt].next = head[u];
head[u] = cnt++;
} int val[maxn], len[maxn];
int ans[maxn]; void dfs(int u, int fa){
vector<int> tmp;
for(int &i : factor[val[u]]) tmp.pb(len[i]);
for(int i = head[u]; ~i; i = edges[i].next){
int v = edges[i].to;
if(v == fa) continue;
dfs(v, u);
}
ans[u] = 0;
for(int i = 0; i < tmp.sz; ++i){
int t = factor[val[u]][i];
ans[u] += mu[t] * (len[t]-tmp[i]);
++len[t];
}
if(val[u] == 1) ++ans[u];
tmp.cl;
} int main(){
Moblus();
int kase = 0;
while(scanf("%d", &n) == 1){
ms(head, -1); cnt = 0; ms(len, 0);
for(int i = 1; i < n; ++i){
int u, v; scanf("%d %d", &u, &v);
addEdge(u, v);
addEdge(v, u);
}
for(int i = 1; i <= n; ++i) scanf("%d", val + i);
dfs(1, -1);
printf("Case #%d:", ++kase);
for(int i = 1; i <= n; ++i) printf(" %d", ans[i]);
printf("\n");
}
return 0;
}
HDU 5468 Puzzled Elena (dfs + 莫比乌斯反演)的更多相关文章
- HDU 5468 Puzzled Elena 莫比乌斯反演
题意: 给出一棵树,每个点上有权值.然后求每棵子树中与根节点互质( \(gcd(a, b) = 1\) )的节点个数. 分析: 对于一颗子树来说,设根节点的权值为\(u\), \(count_i\)表 ...
- HDU 5468 Puzzled Elena
Puzzled Elena Time Limit: 2500ms Memory Limit: 131072KB This problem will be judged on HDU. Original ...
- HDU 2841 Visible Trees(莫比乌斯反演)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2841 题意:给n*m的矩阵(从(1,1)开始编号)格子,每个格子有一棵树,人站在(0,0)的位置,求可 ...
- HDU - 4675 GCD of Sequence (莫比乌斯反演+组合数学)
题意:给出序列[a1..aN],整数M和k,求对1-M中的每个整数d,构建新的序列[b1...bN],使其满足: 1. \(1 \le bi \le M\) 2. \(gcd(b 1, b 2, -, ...
- HDU 6053:TrickGCD(莫比乌斯反演)
题目链接 题意 给出n个数,问在这n个数里面,有多少组bi(1<=bi<=ai)可以使得任意两个bi不互质. 思路 想法就是枚举2~min(ai),然后去对于每个ai都去除以这些质数,然后 ...
- 数学--数论--HDU 4675 GCD of Sequence(莫比乌斯反演+卢卡斯定理求组合数+乘法逆元+快速幂取模)
先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N i ...
- HDU 6134 Battlestation Operational(莫比乌斯反演)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6134 [题目大意] 求$\sum_{i=1}^{n}{\sum_{j=1}^{i}\lceil{\ ...
- 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: ...
- HDU 5321 Beautiful Set (莫比乌斯反演 + 逆元 + 组合数学)
题意:给定一个 n 个数的集合,然后让你求两个值, 1.是将这个集合的数进行全排列后的每个区间的gcd之和. 2.是求这个集合的所有的子集的gcd乘以子集大小的和. 析:对于先求出len,len[i] ...
随机推荐
- EXCEL中去掉撇号的操作方法
▲数字前带撇号 选定想去掉撇号的列,然后选“数据”→“分列”在弹出的 对话框中单击“下一步” ,在“文本标识符号”处选择“'” 单击 “完成”即可
- ARM板移植udev-126
下载udev-126.tar.xz 下载的网址为: https://mirrors.edge.kernel.org/pub/linux/utils/kernel/hotplug/ 解压文件并且编译 # ...
- 集成bug统计链接
http://crab.baidu.com/http://bugly.qq.com/ http://bughd.com/ http://www.umeng.com/analyticshttp://tr ...
- java_14.1 判断是否是闰年
package demo; import java.util.Calendar; import java.util.Scanner; public class Demo { public static ...
- IntelliJ+Maven+Spring+Tomcat项目搭建(MAC)
1.新建项目 打开idea,通过File->new->project,会弹出如下的信息: 接下来点击下一步,创建项目,点击“下一步”: 选择默认的Maven以及setting文件,点击“下 ...
- Bootstrap(8) 路径分页标签和徽章组件
一.路径组件路径组件也叫做面包屑导航.//面包屑导航 <ol class="breadcrumb"> <li><a href="#" ...
- linux如何搭建sftp服务器
工具/原料 centos7.2_x64 方法/步骤 创建sftp组 groupadd sftp 创建完成之后使用cat /etc/group命令组的信息 2 创建一个sftp用户mysft ...
- springboot 日志2
SpringBoot关于日志的官方文档 1.简述 SpringBoot官方文档关于日志的整体说明 本博客基于SpringBoot_1.3.6大家请先简单看下这篇英文的官方文档,文中有说 Sprin ...
- ROS launch总结
1 运行Launch文件2 新建Launch文件3 在namespace中启动nodes 4 remapping names 5 其他的launch元素 1 运行Launch文件 Launch文 ...
- Aspose.Words五 MergeField
通过MegerField来循环,将数据保存到dataset的table中,dataset通过关联datarelation字段来指定主从表关系.模板中通过标签TableStart和TableEnd来框定 ...