Puzzled Elena
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2318 Accepted Submission(s): 819
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
Source
2015 ACM/ICPC Asia Regional Shanghai Online
Recommend
hujie | We have carefully selected several similar problems for you: 6667 6666 6665 6664 6663
题目大意:给出一棵有根树,每个节点有一个值,求节点i与i的子树中有多少个数是互质的。
解题思路:该题很好的利用了容斥定理和前缀和的性质。
对于一个以i为根节点的子树,我们要求a[i]与子树中有多少个值是互质的,假设我们已经掌握了
求任意一个数与一个集合中有多少个数是互质的技能。
我们是否可以在回溯的时候一个一个插入到集合中统计答案。仔细想一下的确是可以的。
但是我们要考虑不同子树间的影响,我们在左子树统计的信息在右子树也能够查询到。
由于我们统计一个数与一个集合中有多少个数是互质的时候我们是将集合中的数质因数
分解,然后看能够产生哪些因子。然后将a[i]质因数分解,进行容斥。
此时我们发现,如果我们将进入子树之前有多少个数与a[i]互质记录下来,然后在子树便利完之后
要统计当前点答案的时候将之前的答案减去就可以了。
#include<bits/stdc++.h>
#define LL long long
using namespace std;
#define pb(x) push_back(x)
#define sca(x) scanf("%d",&x)
#define mp(x,y) make_pair(x,y)
const int N = 2e5+5;
const int maxn = 1e5+100;
vector<int>v[N];
vector<pair<int,int> >_fac[N];
typedef pair<int,int>pii;
int ans[N],num[N],a[N],sz[N];
int b[1005],c[1005];
void init( )
{
for(int i=2;i<=maxn;i++)
{
int now=i;
int tot=0;
for(int j=2;j*j<=now; )
{
if(now%j==0)
{
b[tot++]=j;
while(now%j==0)now/=j;
}
if(j==2)j++;
else j+=2;
}
if(now!=1)b[tot++]=now;
for(int j=1;j<(1<<tot);j++)
{
int fac=1;
int sig=0;
for(int k=0;k<tot;k++)
{
if((1<<k)&j)
{
fac*=b[k];
sig++;
}
}
_fac[i].pb(mp(fac,sig));
}
}
}
int cal(int k,int val)
{
int tot=_fac[k].size();
int res=0;
for(int i=0;i<tot;i++)
{
pii tmp=_fac[k][i];
if(tmp.second%2) res+=num[tmp.first];
else res-=num[tmp.first];
num[tmp.first]+=val;
}
return res;
}
void dfs(int u,int fa)
{
int pre=cal(a[u],0);
sz[u]=1;
for(int i=0;i<v[u].size();i++)
{
int to=v[u][i];
if(to==fa)continue;
dfs(to,u);
sz[u]+=sz[to];
}
int now=cal(a[u],1);
ans[u]=sz[u]-1-(now-pre);
if(a[u]==1)ans[u]++;
}
int main()
{
int cas=1;
int n;init();
while(~scanf("%d",&n))
{
memset(num,0,sizeof(num));
memset(ans,0,sizeof(ans));
for(int i=1;i<=n;i++)v[i].clear();
for(int i=1;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
v[x].pb(y);
v[y].pb(x);
}
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
printf("Case #%d: ",cas++);
dfs(1,1);
for(int i=1;i<=n;i++)printf("%d%c",ans[i],i==n?'\n':' ');
}
}
/*
5
1 2
1 3
2 4
2 5
123 232 234 211 456
*/