UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#149604#31. MSTYuc100430ms106144kbC++112.4kb2022-07-21 18:52:012022-07-21 18:52:03

answer

#include<bits/stdc++.h>
typedef long long ll;
const int N=43,E=800,C=37357,M=1e9+7;
int n,a[N],fac[E],invf[E],cnt,siz[C],f[C][E],g[N][N],ans;
struct data{int a,c;};
std::vector<data>b[C],bb;
struct hash_table{
	int head[C],k;
	struct node{ll a;int p,nxt;}h[C];
	inline int&operator[](const std::vector<data>&b){
		ll a=0;
		for(data dt:b)for(int j=0;j<dt.c;j++)a=a<<dt.a|1;
		int aa=a%C;
		for(int i=head[aa];i;i=h[i].nxt)if(h[i].a==a)return h[i].p;
		h[++k]=(node){a,0,head[aa]},head[aa]=k;
		return h[k].p;
	}
}h;
inline int Pow(int a,int m){int s=1;for(;m;m>>=1)m&1?s=(ll)s*a%M:0,a=(ll)a*a%M;return s;}
inline int A(int n,int m){return(ll)fac[n]*invf[n-m]%M;}
void Dfs(int s,int l,int c){
	if(s==n){++cnt,h[bb]=cnt,b[cnt]=bb,siz[cnt]=n-c;return;}
	if(l&&s+l<=n){
		++bb.back().c;
		Dfs(s+l,l,c+1);
		--bb.back().c;
	}
	for(int j=l+1;s+j<=n;j++){
		bb.push_back((data){j,1});
		Dfs(s+j,j,c+1);
		bb.pop_back();
	}
}
inline void Dp(int&a,int b,int c){a=((ll)b*c+a)%M;}
int main(){
	scanf("%d",&n);
	fac[0]=1;
	for(int i=1;i<=n*(n-1)/2;i++)fac[i]=(ll)fac[i-1]*i%M;
	invf[n*(n-1)/2]=Pow(fac[n*(n-1)/2],M-2);
	for(int i=n*(n-1)/2;i;i--)invf[i-1]=(ll)invf[i]*i%M;
	for(int i=1;i<n;i++)scanf("%d",a+i);
	Dfs(0,0,0);
	for(int i=1;i<cnt;i++){
		for(int j=0;j<b[i].size();j++)
			for(int k=j+(b[i][j].c==1);k<b[i].size();k++){
				bb.clear();
				for(int l=0;l<b[i].size();l++)
					if(b[i][l].c-(j==l)-(k==l))
						bb.push_back((data){b[i][l].a,b[i][l].c-(j==l)-(k==l)});
				for(int l=0;l<bb.size();l++)
					if(bb[l].a>=b[i][j].a+b[i][k].a){
						if(bb[l].a==b[i][j].a+b[i][k].a)++bb[l].c;
						else bb.insert(bb.begin()+l,(data){b[i][j].a+b[i][k].a,1});
						goto Brk;
					}
				bb.push_back((data){b[i][j].a+b[i][k].a,1});
			  Brk:;
				g[j][k]=h[bb];
			}
		if(i==1)f[i][0]=1;
		for(int l=a[siz[i]+1]-a[siz[i]]-1;l<=n*(n-1)/2;l++){
			if(!f[i][l])continue;
			for(int j=0;j<b[i].size();j++){
				if(b[i][j].c>1)
					Dp(f[g[j][j]][l-(a[siz[i]+1]-a[siz[i]]-1)+(b[i][j].a*b[i][j].a-1)],f[i][l],b[i][j].a*b[i][j].a*b[i][j].c*(b[i][j].c-1)/2*(ll)A(l,a[siz[i]+1]-a[siz[i]]-1)%M);
				for(int k=j+1;k<b[i].size();k++)
					Dp(f[g[j][k]][l-(a[siz[i]+1]-a[siz[i]]-1)+(b[i][j].a*b[i][k].a-1)],f[i][l],b[i][j].a*b[i][k].a*b[i][j].c*b[i][k].c*(ll)A(l,a[siz[i]+1]-a[siz[i]]-1)%M);
			}
		}
	}
	ans=(ll)f[cnt][n*(n-1)/2-a[n-1]]*A(n*(n-1)/2-a[n-1],n*(n-1)/2-a[n-1])%M;
	printf("%d\n",ans);
	return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 5
Accepted
time: 0ms
memory: 2160kb

input:

4
1 2 3

output:

576

result:

ok single line: '576'

Test #2:

score: 5
Accepted
time: 0ms
memory: 2164kb

input:

4
1 2 4

output:

144

result:

ok single line: '144'

Test #3:

score: 5
Accepted
time: 0ms
memory: 2164kb

input:

5
1 2 3 4

output:

2160000

result:

ok single line: '2160000'

Test #4:

score: 5
Accepted
time: 0ms
memory: 2168kb

input:

5
1 2 3 6

output:

276480

result:

ok single line: '276480'

Test #5:

score: 5
Accepted
time: 1ms
memory: 2176kb

input:

6
1 2 3 4 5

output:

350972052

result:

ok single line: '350972052'

Test #6:

score: 5
Accepted
time: 0ms
memory: 2172kb

input:

6
1 2 4 7 11

output:

12441600

result:

ok single line: '12441600'

Test #7:

score: 5
Accepted
time: 0ms
memory: 2192kb

input:

7
1 2 3 4 5 6

output:

373181939

result:

ok single line: '373181939'

Test #8:

score: 5
Accepted
time: 0ms
memory: 2188kb

input:

7
1 2 3 6 7 10

output:

655119719

result:

ok single line: '655119719'

Test #9:

score: 5
Accepted
time: 0ms
memory: 2780kb

input:

15
1 2 3 4 5 6 7 8 9 10 11 12 13 14

output:

778980097

result:

ok single line: '778980097'

Test #10:

score: 5
Accepted
time: 1ms
memory: 2700kb

input:

15
1 2 3 7 8 10 11 13 15 18 21 25 27 30

output:

72110308

result:

ok single line: '72110308'

Test #11:

score: 5
Accepted
time: 3ms
memory: 4268kb

input:

20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

output:

521592645

result:

ok single line: '521592645'

Test #12:

score: 5
Accepted
time: 2ms
memory: 3468kb

input:

20
1 2 3 5 7 9 11 16 21 26 28 32 37 41 42 47 48 50 55

output:

185224996

result:

ok single line: '185224996'

Test #13:

score: 5
Accepted
time: 4ms
memory: 8524kb

input:

25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

output:

83240183

result:

ok single line: '83240183'

Test #14:

score: 5
Accepted
time: 3ms
memory: 7132kb

input:

25
1 2 4 7 10 13 15 16 17 18 21 25 27 31 36 40 42 43 47 52 56 60 63 64

output:

649967979

result:

ok single line: '649967979'

Test #15:

score: 5
Accepted
time: 17ms
memory: 20148kb

input:

30
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

output:

587585839

result:

ok single line: '587585839'

Test #16:

score: 5
Accepted
time: 12ms
memory: 16084kb

input:

30
1 2 3 5 10 12 16 20 24 27 28 29 31 32 36 41 44 45 49 51 54 58 62 66 68 70 74 79 84

output:

219192987

result:

ok single line: '219192987'

Test #17:

score: 5
Accepted
time: 49ms
memory: 43636kb

input:

35
1 2 3 5 8 9 12 13 14 15 18 21 25 30 33 36 38 41 43 45 49 54 57 59 64 69 70 74 78 80 81 83 87 89

output:

758370424

result:

ok single line: '758370424'

Test #18:

score: 5
Accepted
time: 44ms
memory: 37992kb

input:

35
1 2 4 7 9 13 15 19 23 28 32 36 40 41 43 47 49 50 55 56 61 64 67 69 72 75 76 78 80 85 90 93 95 98

output:

509074597

result:

ok single line: '509074597'

Test #19:

score: 5
Accepted
time: 156ms
memory: 104276kb

input:

40
1 2 4 6 8 12 15 16 19 21 26 28 31 35 36 38 41 43 47 50 55 57 58 63 65 69 74 76 78 83 85 90 91 92 ...

output:

124674859

result:

ok single line: '124674859'

Test #20:

score: 5
Accepted
time: 138ms
memory: 106144kb

input:

40
1 2 4 5 7 12 13 16 18 23 27 28 32 35 39 42 43 45 47 50 53 54 57 59 61 66 70 72 77 82 87 91 96 98 ...

output:

186165705

result:

ok single line: '186165705'