题意:给定一棵带权树,求每个点与其子树结点的权值互质的个数。
析:首先先要进行 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; }