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] ...
随机推荐
- SAP transportation
1.CONFIGURATION TRANSPORT flow:DEV(100) --scc1--> DEV(400) --STMS after release-> QAS(510) --S ...
- [leetcode]387. First Unique Character in a String第一个不重复字母
Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...
- js第三天知识点 循环
/*for(重点) *while *do..while * *3种循环的区别: *for 主要适用于 明确循环次数的 循环 *while 适用于 不明确循环次数的循环 *do..while 适用于 ...
- 10.9h5日记
一.单位 1.px是基本的单位,像素 2.em也是一个单位,使用方式,用元素父级的字体大小乘以em前的数字,父级没有就向上一个父级找, 直到body为止,如果body没有,就用默认的字体大小16px ...
- go 语言实现一个简单的 web 服务器
学习Go语言的一些感受,不一定准确. 假如发生战争,JAVA一般都是充当航母战斗群的角色.一旦出动,就是护卫舰.巡洋舰.航母舰载机.预警机.电子战飞机.潜艇等等浩浩荡荡,杀将过去.(JVM,数十个JA ...
- PyQt5速成教程
博客地址 https://www.jianshu.com/nb/26159952
- Eclipse中logcat过滤器的使用
logcat里信息繁多,用过滤器可以方便快捷的找到我们要查找的信息. 我们可以在打开Eclipse之后,选择Window –> Show View ->Other菜单,然后在Android ...
- fastdfs 上传图片 完整版
1.jsp文件 <%@ page language="java" import="java.util.*" pageEncoding="UTF- ...
- 在IP网络中,P、PE、CE代表意思
1.ce , pe属于mpls vpn里的概念.VPN概念中,把整个网络中的路由器分为三类:用户边缘路由器(CE).运营商边缘路由器(PE)和运营商骨干路由器(P):其中,PE充当IP VPN接入路由 ...
- 自己动手开启QUIC(转载)
源:https://www.bennythink.com/quic.html#title-0 今天在推上偶然发现 Google 在自己的服务器上启用了 QUIC,QUIC 这东西嘛(发音同 quick ...