洛谷 AC,UOJ 被 Hack TLE
查看原帖
洛谷 AC,UOJ 被 Hack TLE
482610
CEFqwq楼主2025/2/6 13:07
#include<bits/stdc++.h>
#define int long long
const int mod = 1000000007;
using namespace std;
int c, n, t, m, k, fa[30005], f[2][1037];
#define BF_SIZE 100000
    bool IOerr=0;
    inline char nc(){
        static char buf[BF_SIZE],*p1=buf+BF_SIZE,*pend=buf+BF_SIZE;
        if(p1==pend){
            p1=buf;
            pend=buf+fread(buf,1,BF_SIZE,stdin);
            if(pend==p1){IOerr=1;return -1;}
        }
        return *p1++;
    }
    inline bool bla(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}
    inline void read(int &x){
        register char ch;
        while(bla(ch=nc()));
        if(IOerr){return;}
        for(x=ch-'0';(ch=nc())>='0'&&ch<='9';x=x*10+ch-'0');
    }
    #undef BF_SIZE
inline void write(int x){
    register char F[200];
    register int tmp=x>0?x:-x;
    if(x<0)putchar('-');
    register int cnt=0;
    while(tmp>0)
    {
        F[cnt++]=tmp%10+'0';
        tmp/=10;
    }
    while(cnt>0)putchar(F[--cnt]);
}
signed main(){
//	ios::sync_with_stdio(false);
//	cin.tie(0);cout.tie(0);
	read(c);
	read(t);
	while (t--){
		read(n);
		read(m);
		read(k);
		for (int i = 2; i <= n; i++)
			read(fa[i]);
		if (!k){
			int ans = 1;
			for (int i = n; i <= (n + m - 1); i++)
				ans = ans * (i * 2 - 1) % mod;
			write(ans);
		//	cout << ans << "\n";
		}else if (!m)
			write(1);
		else if (m == 1)
			write(n * 2 - 1);
		else if (m == 2)
			write(((n * 2 - 1) * (n * 2 + 1) % mod + (n - 1)) % mod);
		else{
			memset(f, 0, sizeof f);
			f[n & 1][0] = 1;
			for (int i = n; i <= n + m - 1; i++){
				int tar = (i & 1) ^ 1;
				for (int S = 1 << k - 1; S < (1 << k); S++)
					f[tar][(S << 1) & ((1 << k) - 1)] = (f[tar][(S << 1) & ((1 << k) - 1)] + f[tar ^ 1][S]) % mod;
				for (int S = 0; S < (1 << k - 1); S++){
					for (int j = 0; j < k - 1; j++)
						if (S & (1 << j))
							f[tar][(S ^ (1 << j)) << 1] = (f[tar][(S ^ (1 << j)) << 1] + f[tar ^ 1][S]) % mod;
					f[tar][S << 1] = (f[tar][S << 1] + f[tar ^ 1][S] * ((__builtin_popcount(S) + i) * 2 - 1) % mod) % mod; 
					f[tar][S << 1 | 1] = (f[tar][S << 1 | 1] + f[tar ^ 1][S] * ((__builtin_popcount(S) + i) - 1) % mod) % mod; 
				}
				for (int S = 0; S < (1 << k); S++)
					f[tar ^ 1][S] = 0;
			}
			write(f[(n + m) & 1][0]);
		}
		putchar('\n');
	}
	return 0;
}
2025/2/6 13:07
加载中...