Byteasar has a tree TT with nn vertices conveniently labeled with 1,2,...,n1,2,...,n. Each vertex of the tree has an integer value vivi.
The value of a non-empty tree TT is equal to v1⊕v2⊕...⊕vnv1⊕v2⊕...⊕vn, where ⊕⊕ denotes bitwise-xor.
Now for every integer kk from [0,m)[0,m), please calculate the number of non-empty subtree of TT which value are equal to kk.
A subtree of TT is a subgraph of TT that is also a tree.
Input
The first line of the input contains an integer T(1≤T≤10)T(1≤T≤10), denoting the number of test cases.
In each test case, the first line of the input contains two integers n(n≤1000)n(n≤1000) and m(1≤m≤210)m(1≤m≤210), denoting the size of the tree TT and the upper-bound of vv.
The second line of the input contains nn integers v1,v2,v3,...,vn(0≤vi<m)v1,v2,v3,...,vn(0≤vi<m), denoting the value of each node.
Each of the following n−1n−1 lines contains two integers ai,biai,bi, denoting an edge between vertices aiai and bi(1≤ai,bi≤n)bi(1≤ai,bi≤n).
It is guaranteed that mm can be represent as 2k2k, where kk is a non-negative integer.
Output
For each test case, print a line with mm integers, the ii-th number denotes the number of non-empty subtree of TT which value are equal to ii.
The answer is huge, so please module 109+7109+7.
Sample Input
2 4 4 2 0 1 3 1 2 1 3 1 4 4 4 0 1 3 1 1 2 1 3 1 4
Sample Output
3 3 2 3 2 4 2 3
#include<bits/stdc++.h>
using namespace std;
const int N=1e3+100,mod=1e9+7,rev=(mod+1)>>1;
int a[N],dp[N][N],ans[N];
vector <int >mp[N];
int n,m;
int tmp[N];
void FWT(int *a,int n,int t)
{
for(int d=1; d<n; d<<=1)
for(int m=d<<1,i=0; i<n; i+=m)
for(int j=0; j<d; j++)
{
int x=a[i+j],y=a[i+j+d];
if (t==1) a[i+j]=(x+y)%mod,a[i+j+d]=(x-y+mod)%mod;
else a[i+j]=1LL*(x+y)*rev%mod,a[i+j+d]=(1LL*(x-y)*rev%mod+mod)%mod;
}
}
void solve(int *a,int *b,int n)
{
FWT(a,n,1);FWT(b,n,1);
for(int i=0; i<n; i++) a[i]=1LL*a[i]*b[i]%mod;
FWT(a,n,0);
}
void dfs(int x,int fa)
{
dp[x][a[x]]=1;
for (int i=0; i<mp[x].size(); i++)
{
int v=mp[x][i];
if(v==fa) continue;
dfs(v,x);
for (int i=0; i<m; i++) tmp[i]=dp[x][i];
solve(dp[x],dp[v],m);
for (int i=0; i<m; i++) dp[x][i]=(dp[x][i]+tmp[i])%mod;
}
for (int i=0;i<m;i++) ans[i]=(ans[i]+dp[x][i])%mod;
}
int main()
{
int T,u,v;
cin>>T;
while(T--)
{
cin>>n>>m;
memset(dp,0,sizeof dp);
memset(ans,0,sizeof ans);
for (int i=1; i<=n; i++) mp[i].clear();
for (int i=1; i<=n; i++) scanf("%d",&a[i]);
for (int i=1; i<n; i++)
{
scanf("%d%d",&u,&v);
mp[u].push_back(v);
mp[v].push_back(u);
}
dfs(1,0);
for (int i=0;i<m;i++)
if(i!=0) printf(" %d",ans[i]);
else printf("%d",ans[i]);
printf("\n");
}
return 0;
}