先要%mmh学长。
nlogn求解位运算卷积:
一、模板
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#define ll long long
#define llu unsigned ll
using namespace std;
//len 为2的正整次幂
//以下取模与不取模的写法,仅在细节上有所调整
//两种细节写法均可
ll inv2;
void fwt_or(ll *x,int len,int opt)
{
for(int i=2;i<=len;i<<=1)
{
for(int p=i>>1,j=0;j<len;j+=i)
{
for(int k=j;k<j+p;k++)
x[k+p]+=x[k]*opt;
}
}
}
void fwt_and(ll *x,int len,int opt)
{
for(int i=2;i<=len;i<<=1)
{
for(int p=i>>1,j=0;j<len;j+=i)
{
for(int k=j;k<j+p;k++)
x[k]+=x[k+p]*opt;
}
}
}
void fwt_xor(ll *x,int len,int opt)
{
for(int i=2;i<=len;i<<=1)
{
for(int p=i>>1,j=0;j<len;j+=i)
{
for(int k=j;k<j+p;k++)
{
int xx=x[k],yy=x[k+p];
x[k]=xx+yy;
x[k+p]=xx-yy;
if(opt==-1) x[k]/=2,x[k+p]/=2;
}
}
}
}
void fwt_or_mod(int *x,int len,int opt,int mod)
{
for(int i=1;i<len;i<<=1)
{
for(int p=i<<1,j=0;j<len;j+=p)
{
for(int k=0;k<i;k++)
{
if(opt==1) x[i+j+k]=(x[j+k]+x[i+j+k])%mod;
else x[i+j+k]=(x[i+j+k]+mod-x[j+k])%mod;
}
}
}
}
void fwt_and_mod(int *x,int len,int opt,int mod)
{
for(int i=1;i<len;i<<=1)
{
for(int p=i<<1,j=0;j<len;j+=p)
{
for(int k=0;k<i;k++)
{
if(opt==1) x[j+k]=(x[j+k]+x[i+j+k])%mod;
else x[j+k]=(x[j+k]+mod-x[i+j+k])%mod;
}
}
}
}
void fwt_xor_mod(int *x,int len,int opt,int mod)
{
for(int i=1;i<len;i<<=1)
{
for(int p=i<<1,j=0;j<len;j+=p)
{
for(int k=0;k<i;k++)
{
int xx=x[j+k],yy=x[i+j+k];
x[j+k]=(xx+yy)%mod;
x[i+j+k]=(xx+mod-yy)%mod;
if(opt==-1)
x[j+k]=1ll*x[j+k]*inv2%mod,x[i+j+k]=1ll*x[i+j+k]*inv2%mod;
}
}
}
}
int main(void)
{
return 0;
}
二、例题:【HDU5909】Tree Cutting:
Tree Cutting
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)
Total Submission(s): 2158 Accepted Submission(s): 813
Problem Description
Byteasar has a tree T with n vertices conveniently labeled with 1,2,…,n. Each vertex of the tree has an integer value vi.
The value of a non-empty tree T is equal to v1⊕v2⊕…⊕vn, where ⊕ denotes bitwise-xor.
Now for every integer k from [0,m), please calculate the number of non-empty subtree of T which value are equal to k.
A subtree of T is a subgraph of T that is also a tree.
Input
The first line of the input contains an integer 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) and m(1≤m≤210), denoting the size of the tree T and the upper-bound of v.
The second line of the input contains n integers v1,v2,v3,…,vn(0≤vi<m), denoting the value of each node.
Each of the following n−1 lines contains two integers ai,bi, denoting an edge between vertices ai and bi(1≤ai,bi≤n).
It is guaranteed that m can be represent as 2k, where k is a non-negative integer.
Output
For each test case, print a line with m integers, the i-th number denotes the number of non-empty subtree of T which value are equal to i.
The answer is huge, so please module 1e9+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
题意:现在对于每个[0,m)的整数k,请统计有多少非空连通子树的价值等于k。
设f [ i ] [ j ] 表示以i为根的子树,i一定取,剩余节点必须联通,异或和为j的方案数
初始化f [ i ] [ val [ i ] ] = 1
枚举儿子v转移
f [ i ] [ j ] = f [ i ] [ j ] + sum of ( f [ i ] [ x ] ⋅ f [ v ] [ y ] ) if(x ^ y == j )
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#define ll long long
#define llu unsigned ll
using namespace std;
const int mod=1e9+7;
const int maxn=1010;
const ll inv2=500000004;
int head[maxn],ver[maxn<<1],nt[maxn<<1];
int vi[maxn],x1[1<<10],x2[1<<10],dp[maxn][1<<10];
int ans[1<<10],tot=0,n,m;
void add(int x,int y)
{
ver[++tot]=y,nt[tot]=head[x],head[x]=tot;
}
void fwt_xor_mod(int *x,int opt)
{
for(int i=1;i<m;i<<=1)
{
for(int p=i<<1,j=0;j<m;j+=p)
{
for(int k=0;k<i;k++)
{
int xx=x[j+k],yy=x[i+j+k];
x[j+k]=(xx+yy)%mod;
x[i+j+k]=(xx+mod-yy)%mod;
if(opt==-1)
x[j+k]=1ll*x[j+k]*inv2%mod,x[i+j+k]=1ll*x[i+j+k]*inv2%mod;
}
}
}
}
void fi(int *a,int *b)
{
for(int i=0;i<m;i++)
x1[i]=a[i],x2[i]=b[i];
fwt_xor_mod(x1,1);
fwt_xor_mod(x2,1);
for(int i=0;i<m;i++)
x1[i]=1ll*x1[i]*x2[i]%mod;
fwt_xor_mod(x1,-1);
for(int i=0;i<m;i++)
a[i]=(a[i]+x1[i])%mod;
}
void dfs(int x,int fa)
{
for(int i=0;i<m;i++)
dp[x][i]=0;
dp[x][vi[x]]=1;
for(int i=head[x];i;i=nt[i])
{
int y=ver[i];
if(y==fa) continue;
dfs(y,x);
fi(dp[x],dp[y]);
}
for(int i=0;i<m;i++)
ans[i]=(ans[i]+dp[x][i])%mod;
}
int main(void)
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
memset(head,0,sizeof(head));
memset(ans,0,sizeof(ans));
tot=0;
int x,y;
for(int i=1;i<=n;i++)
scanf("%d",&vi[i]);
for(int i=1;i<n;i++)
{
scanf("%d%d",&x,&y);
add(x,y),add(y,x);
}
dfs(1,0);
for(int i=0;i<m;i++)
{
printf("%d%c",ans[i],i==m-1?'\n':' ');
}
}
return 0;
}