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] ...
随机推荐
- Django的Rbac介绍2
上一篇博客我们记录了一下Django中使用Rbac,但是上一篇博客中的方法有一点不好,就是,因为我要在html文件中控制:如果用户有某个权限,则显示这个权限所代表的按钮,但是我现在只有1张表的增删改查 ...
- Django的具体操作(一)
一,首先说下什么是Django,Django其实就是一个框架,用python写的,能开发网站之类的. 二,django的组成 1,urls:网址入口关联到对应的views视图函数,(采用mvc开发模式 ...
- RabbitMQ 参数们的Power “续”
参数中的 arguments 之前讲参数的一些作用的时候,忽略了最后一个字典类型的参数,因为这个参数是大有文章的,值得单独进出来说道说道. 这时,就不得不打开我们的 Web UI管理系统了,可以看 ...
- 再遇ibatisNet
11年在Mr刘的带领下第一次接触ibatisnet ,当时Mr刘很详细的很讲了xml里的写法还有配置文件之类的,但是随着时间越来越久远.很多东西都开始淡忘了. 如今,再次和它相遇,依然觉得很亲切,虽然 ...
- Word打开时显示*模板*,删除模板
XP系统, 找到目录 C:\Documents and Settings\Administrator\Application Data\Microsoft\Templates 删除里面的模板文件即可
- tomcat 启动日志乱码
打开cd到tomcat/conf/目录下 修改logging.properties 找到 java.util.logging.ConsoleHandler.encoding = utf-8这行 更改为 ...
- Mysql自定义函数functions时报错
delimiter && [函数创建语句.....] [Err] 1418 - This function has none of DETERMINISTIC, NO SQL, or ...
- widdow.locationg.可跟的参数,获取url不同的地址
- 解决JFinal多文件上传时只获取到第一个文件名
我的思路: 用户生成时随即生成一串随机字符作为该用户的文件上传目录,并保存该字符串到用户的某一字段.需要显示上传的附件时,遍历这个文件夹.上传时可把文件名设置为上传时间. 1.生成上传路径 可写在注册 ...
- JS设置CSS样式的集中方式
1. 直接设置style的属性 某些情况用这个设置 !important值无效 如果属性有'-'号,就写成驼峰的形式(如textAlign) 如果想保留 - 号,就中括号的形式 element. ...