# SDOI2017 切树游戏

$s_u$表示节点$u$的子节点集合.
$ls_u$表示节点$u$的轻儿子集合.

$f[u][i]$表示以$u$为最高点的连通块中,异或和为$i$的方案数.

$t$表示$u$的重儿子,则有转移方程:

$f[u]=(f[t]+1)*lf[u]*w[u]=f[t]*lf[u]*w[u]+lf[u]*w[u]\\ g[u]=f[u]+g[t]+lg[u]$

$\left[ \begin{matrix} lf[u]*w[u]&0&lf[u]*w[u]\\ lf[u]*w[u]&1&lf[u]*w[u]+lg[u]\\ 0&0&1\\ \end{matrix} \right]* \left[ \begin{matrix} f[t]\\ g[t]\\ 1\\ \end{matrix} \right]= \left[ \begin{matrix} f[u]\\ g[u]\\ 1\\ \end{matrix} \right]$

$\left[ \begin{matrix} a1&0&b1\\ c1&1&d1\\ 0&0&1\\ \end{matrix} \right]* \left[ \begin{matrix} a2&0&b2\\ c2&1&d2\\ 0&0&1\\ \end{matrix} \right]= \left[ \begin{matrix} a1*a2&0&a1*b2+b1\\ c1*a2+c1&1&c1*b2+d1+d2\\ 0&0&1\\ \end{matrix} \right]$

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#define N (30010)
#define P (10007)
#define M (128)
#define inf (0x7f7f7f7f)
#define rg register int
#define Label puts("NAIVE")
#define spa print(' ')
#define ent print('\n')
#define rand() (((rand())<<(15))^(rand()))
typedef long double ld;
typedef long long LL;
typedef unsigned long long ull;
using namespace std;
namespace fastIO1{
static const int IN_LEN=1000000;
static char buf[IN_LEN],*s,*t;
}
template<class T>
static bool iosig;
static char c;
if(c=='-')iosig=true;
if(c==-1)return;
}
if(iosig)x=-x;
}
static char c;
if(c==-1)return 0;
return c;
}
const int OUT_LEN = 1000000;
char obuf[OUT_LEN],*ooh=obuf;
inline void print(char c) {
if(ooh==obuf+OUT_LEN)fwrite(obuf,1,OUT_LEN,stdout),ooh=obuf;
*ooh++=c;
}
template<class T>
inline void print(T x){
static int buf[30],cnt;
if(x==0)print('0');
else{
if(x<0)print('-'),x=-x;
for(cnt=0;x;x/=10)buf[++cnt]=x%10+48;
while(cnt)print((char)buf[cnt--]);
}
}
inline void flush(){fwrite(obuf,1,ooh-obuf,stdout);}
}
using namespace fastIO1;
int n,m,Q;
int w[N],ind,E,fi[N],ne[N<<1],b[N<<1],invm;
int fa[N],son[N],siz[N],top[N],dfn[N],rdfn[N],tmp[N],en[N],pos[N];
int ksm(int a,int p){
int res=1;
while(p){
if(p&1)res=1ll*res*a%P;
a=1ll*a*a%P,p>>=1;
}
return res;
}
struct poly{
int a[M];
void FWT(int *a,int tp){
for(int i=1;i<m;i<<=1)
for(int R=i<<1,j=0;j<m;j+=R)
for(int k=j;k<j+i;k++){
int x=a[k],y=a[k+i];
a[k]=x+y,a[k+i]=x-y;
}
for(int i=0;i<m;i++)a[i]=(a[i]+P)%P;
if(tp==-1)for(int i=0;i<m;i++)a[i]=a[i]*invm%P;
}
void trans(){FWT(a,1);}
void itrans(){FWT(a,-1);}
poly operator +(poly x){
poly ans;
for(int i=0;i<m;i++)ans.a[i]=(x.a[i]+a[i])%P;
return ans;
}
poly operator -(poly x){
poly ans;
for(int i=0;i<m;i++)ans.a[i]=(a[i]-x.a[i]+P)%P;
return ans;
}
poly operator *(poly x){
poly ans;
for(int i=0;i<m;i++)ans.a[i]=x.a[i]*a[i]%P;
return ans;
}
}g[N],lg[N],f[N],zt[M];
struct lson{
int len;
vector<poly> s;
void build(int l,int r,int x){
if(l==r){s[x]=f[tmp[l]]+zt[0];return;}
int mid=(l+r)>>1;
build(l,mid,x*2),build(mid+1,r,x*2+1);
s[x]=s[x*2]*s[x*2+1];
}
void init(int L){
s.resize((L+1)<<2),len=L;
if(!L)s[1]=zt[0];else build(1,len,1);
}
void modify(int L,int R,int k,poly t,int x){
if(L==R){s[x]=t;return;}
int mid=(L+R)>>1;
if(k<=mid)modify(L,mid,k,t,x*2);
else modify(mid+1,R,k,t,x*2+1);
s[x]=s[x*2]*s[x*2+1];
}
void modify(int k,poly t){modify(1,len,k,t,1);}
}lf[N];
struct Matrix{
poly a,b,c,d;
Matrix(){}
Matrix (poly x,poly y){a=b=c=x,d=x+y;}
Matrix operator *(Matrix x){
Matrix ans;
ans.a=a*x.a,ans.b=a*x.b+b,ans.c=c*x.a+x.c,ans.d=c*x.b+x.d+d;
return ans;
}
}t[N<<2];
ne[++E]=fi[x],fi[x]=E,b[E]=y;
}
void dfs1(int u,int pre){
siz[u]=1,f[u]=zt[w[u]];
for(int i=fi[u];i;i=ne[i]){
int v=b[i];
if(v==pre)continue;
fa[v]=u,dfs1(v,u),siz[u]+=siz[v];
f[u]=f[u]*(f[v]+zt[0]),g[u]=g[u]+g[v];
if(siz[v]>siz[son[u]])son[u]=v;
}
g[u]=g[u]+f[u];
}
void dfs2(int u){
rdfn[dfn[u]=++ind]=u;
if(son[u])top[son[u]]=top[u],dfs2(son[u]),en[u]=en[son[u]];
else en[u]=u;
for(int i=fi[u];i;i=ne[i]){
int v=b[i];
if(v==fa[u]||v==son[u])continue;
top[v]=v,dfs2(v),lg[u]=lg[u]+g[v];
}
int ls=0;
for(int i=fi[u];i;i=ne[i])
if(b[i]!=fa[u]&&b[i]!=son[u])tmp[pos[b[i]]=++ls]=b[i];
lf[u].init(ls);
}
void build(int l,int r,int x){
if(l==r){
int u=rdfn[l];
t[x]=Matrix(lf[u].s[1]*zt[w[u]],lg[u]);
return;
}
int mid=(l+r)>>1;
build(l,mid,x*2),build(mid+1,r,x*2+1);
t[x]=t[x*2]*t[x*2+1];
}
void modify(int L,int R,int k,Matrix mat,int x){
if(L==R){t[x]=mat;return;}
int mid=(L+R)>>1;
if(k<=mid)modify(L,mid,k,mat,x*2);
else modify(mid+1,R,k,mat,x*2+1);
t[x]=t[x*2]*t[x*2+1];
}
Matrix query(int L,int R,int l,int r,int x){
if(L==l&&R==r)return t[x];
int mid=(L+R)>>1;
if(r<=mid)return query(L,mid,l,r,x*2);
else if(l>mid)return query(mid+1,R,l,r,x*2+1);
else return query(L,mid,l,mid,x*2)*query(mid+1,R,mid+1,r,x*2+1);
}
int main(){
for(int i=0;i<m;i++)zt[i].a[i]=1,zt[i].trans();
for(int i=1,x,y;i<n;i++)
build(1,n,1);
while(Q--){
if(ch=='Q'){
poly ans=query(1,n,1,dfn[en[1]],1).d;
ans.itrans(),print((ans.a[x]+P)%P),ent;
}
else{
while(x){
Matrix tmp=Matrix(lf[x].s[1]*zt[w[x]],lg[x]);
modify(1,n,dfn[x],tmp,1);
x=top[x]; int pre=fa[x];
if(!pre)break;
tmp=query(1,n,dfn[x],dfn[en[x]],1);
lf[pre].modify(pos[x],tmp.b+zt[0]);
lg[pre]=lg[pre]-g[x],g[x]=tmp.d,lg[pre]=lg[pre]+g[x];
x=fa[x];
}
}
}
return flush(),0;
}

posted @ 2019-01-04 17:42  Romeolong  阅读(105)  评论(0编辑  收藏