Puzzled Elena
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1724 Accepted Submission(s): 552
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.
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.
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
51 21 32 42 56 2 3 4 5
思路:
因为每个数的大小<=100000 这个范围内2*3*5*7*11*13 *17> 100000
最多只有 6 个素因子当我们知道这个怎么处理以后,我们可以
利用dfs序,解决这个问题我们求当前这个节点的答案时,用容斥
搞就是:以这个节点为根的树的大小 - 有1个素因子和他相同的
节点个数 + 有2个素因子和他相同的个数 - 有3个素因子和他相
同的个数 ....那么问题来了,我们如何求出有多少个 有1个
素因子和他相同的个数,有2个素因子和他相同的个数 ,,,,,
我们维护一个数fac[]数组,fac[i] 代表包含因子i的节点个数。
那么在这颗树中,进入这颗树之前求一下(有1个素因子和他相同
的节点个数,有2个素因子和他相同的节点个数.....),离开
这颗树的时候再求一下(有1个素因子和他相同的节点个数,
有2个素因子和他相同的节点个数.....),他们的差便是我们
需要的(子树中的和他有关系的信息)。
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
vector<int>G[maxn],mp[maxn];
bool vis[maxn];
int n,a[maxn];
int num[maxn],f[maxn],son[maxn];
void init()
{
for(int i=2; i<maxn; i++)
{
if(!vis[i])
{
G[i].push_back(i);
for(int j=i+i; j<maxn; j+=i)
{
vis[j]=1;
G[j].push_back(i);
}
}
}
}
void dfs(int v,int fa)
{
son[v]=1;
int f1=0,f2=0;
int k=G[a[v]].size();
for(int S=1; S<(1<<k); S++)
{
int s=1;
for(int j=0; j<k; j++)
{
if(S>>j&1)
s=s*G[a[v]][j];
}
num[s]++;
}
for(int S=1; S<(1<<k); S++)
{
int s=1,sum=0;
for(int j=0; j<k; j++)
{
if(S>>j&1)
s=s*G[a[v]][j],sum++;
}
if(sum%2)
f1+=num[s];
else
f1-=num[s];
}
for(int i=0; i<mp[v].size(); i++)
{
if(mp[v][i]==fa)continue;
dfs(mp[v][i],v),son[v]+=son[mp[v][i]];
}
for(int S=1; S<(1<<k); S++)
{
int s=1,sum=0;
for(int j=0; j<k; j++)
{
if(S>>j&1)
s=s*G[a[v]][j],sum++;
}
if(sum%2)
f2+=num[s];
else
f2-=num[s];
}
f[v]=f2-f1;
}
int main()
{
init();
int cas=0;
while(~scanf("%d",&n))
{
for(int i=0;i<maxn;i++)
mp[i].clear();
memset(num,0,sizeof(num));
memset(son,0,sizeof(son));
for(int i=1; i<n; i++)
{
int x,y;
scanf("%d%d",&x,&y);
mp[x].push_back(y);
mp[y].push_back(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++)
{
if(a[i]==1) f[i]--;
printf(" %d",son[i]-1-f[i]);
}
printf("\n");
}
return 0;
}