日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【HDU - 5468】Puzzled Elena(容斥原理,dfs序,数学,素因子分解,有坑)

發(fā)布時間:2023/12/10 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【HDU - 5468】Puzzled Elena(容斥原理,dfs序,数学,素因子分解,有坑) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題干:

Problem Description

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≤n≤105) , after that has n?1 lines, each line has two numbers a and b (1≤a,b≤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 105 .

?

?

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

題目大意:

給出一棵樹n個點,每個點上有權(quán)值。然后求每棵子樹中與根節(jié)點互質(zhì),即gcd(a,b)=1的節(jié)點個數(shù)。

解題報告:

因為1e5以內(nèi)的每個數(shù)的素因子個數(shù)不超過10個,所以可以對于每個數(shù)的不互素個數(shù)可以在O(10*2^10)內(nèi)算出來,所以直接dfs序映射成一個序列然后按照題意求就行了。注意題目問的是gcd==1而不是互素,所以1和本身也是gcd==1的,所以val==1的時候需要ans++。

貼一個題解:

該題涉及到一個典型問題.問x與數(shù)的集合S中有多少個數(shù)不互素。解決辦法是將S中所有元素依次進行兩個步驟:①將元素進行質(zhì)因數(shù)分解。②將質(zhì)因數(shù)可能產(chǎn)生的乘積的出現(xiàn)次數(shù)加1。最后將x進行質(zhì)因數(shù)分解,利用容斥原理求解。具體方案見代碼。

容斥原理在OJ中常解決兩個典型問題:①求S中有多少個數(shù)與x不互素。②求1~m中有多少個數(shù)與n不互素。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; vector<int> vv[MAX]; int val[MAX],ans[MAX],num[MAX]; int dfn[MAX],in[MAX],out[MAX],id; vector<int> fac[MAX]; void db() {for(int tar = 1; tar<MAX; tar++) {int x = tar;for(int i = 2; i*i<=x; i++) {if(x%i == 0) {fac[tar].pb(i);while(x%i==0) x/=i;}}if(x > 1) fac[tar].pb(x);} } int cal(int n,int tar) {int res = 0;int upp = 1<<fac[n].size(),up = fac[n].size();for(int bit = 1; bit<upp; bit++) {int cnt = 0,mul = 1;for(int i = 0; i<up; i++) {if(bit & (1<<i)) {cnt++;mul *= fac[n][i];} }if(cnt & 1) res += num[mul];else res -= num[mul];num[mul] += tar;}return res; } int dfs(int cur,int rt) {int ans1 = cal(val[cur],0);int res = 0;int up = vv[cur].size();for(int i = 0; i<up; i++) {int v = vv[cur][i];if(v == rt) continue;res += dfs(v,cur);}int ans2 = cal(val[cur],1);ans[cur] = res - (ans2 - ans1);if(val[cur] == 1) ans[cur]++;return res+1; } int main() {db();int n,iCase=0;while(~scanf("%d",&n)) {//initid=0;for(int i = 1; i<MAX; i++) num[i] = 0;for(int i = 1; i<=n; i++) vv[i].clear(),ans[i]=0;for(int a,b,i = 1; i<=n-1; i++) {scanf("%d%d",&a,&b);vv[a].pb(b);vv[b].pb(a);} for(int i = 1; i<=n; i++) scanf("%d",val+i);dfs(1,-1);printf("Case #%d:",++iCase);for(int i = 1; i<=n; i++) printf(" %d",ans[i]); printf("\n"); } return 0 ; }

?

總結(jié)

以上是生活随笔為你收集整理的【HDU - 5468】Puzzled Elena(容斥原理,dfs序,数学,素因子分解,有坑)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。