1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| #include<iostream> #include<cstdio> #include<cstring> #define lowbit(x) (x&(-x)) using namespace std; const int N=10,M=1<<N,mod=1e9+7; template<class T> inline void read(T &x) { x=0;int f=0; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();} while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); if(f)x=~x+1; } template<class T,class ...T1> inline void read(T &x,T1 &...x1) { read(x),read(x1...); } int type,T,n,m,k,tot; int f[M],g[M]; int cnt[M]; inline int adj(int x){return x>=mod?x-mod:x;} inline void upd(int &x,int y){x+=y,x=x>=mod?x-mod:x;} inline void solve() { read(n,m,k); for(int i=2,fa;i<=n;i++)read(fa); if(k==0) { int res=1; for(int i=n;i<n+m;i++)res=1ll*res*(2*i-1)%mod; printf("%d\n",res); return ; } tot=1<<k; int high=tot>>1; memset(f,0,sizeof(f)); f[0]=1; for(int i=n;i<n+m;i++) { for(int j=high;j<tot;j++)g[(j^high)<<1]=f[j]; for(int j=0;j<high;j++) { for(int t=j;t;) { int now=lowbit(t); upd(g[(j^now)<<1],f[j]); t^=now; } int si=i+cnt[j]; upd(g[j<<1|1],1ll*(si-1)*f[j]%mod); upd(g[j<<1],1ll*(2*si-1)*f[j]%mod); } memcpy(f,g,sizeof(f)); memset(g,0,sizeof(g)); } printf("%d\n",f[0]); } int main() { freopen("tree.in","r",stdin); freopen("tree.out","w",stdout); read(type,T); for(int i=0;i<(M>>1);i++)cnt[i<<1]=cnt[i],cnt[i<<1|1]=cnt[i]+1; while(T--)solve(); return 0; }
|