UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#158758#31. MSTjijidawang100366ms41456kbC++113.3kb2022-08-18 19:41:572022-08-18 19:41:59

answer



#include <bits/stdc++.h>
typedef long long ll;typedef unsigned long long ull; typedef double db;typedef long double ldb;
#define fre(x) freopen(#x ".in","r",stdin),freopen(#x ".out","w",stdout)
#define Rep(i,a,b) for(int i=a;i<=b;++i) 
#define Dwn(i,a,b) for(int i=a;i>=b;--i)
#define pii pair<int,int>
#define mair make_pair
#define fir first
#define sec second
#define int ll
using namespace std;

const int maxn=40+10,M=2e3+10,Mod=1e9+7,inv2=500000004;
const ull base=131;

int pw(int x,int p){int res=1,base=x;while(p){if(p&1)res=1LL*res*base%Mod;base=1LL*base*base%Mod;p>>=1;}return res;}
int Inv(int x){return pw(x,Mod-2);}
struct Sits{
	bitset<44>bit;
	int cnt[maxn];
	void Del(int x,int d){cnt[x]-=d;if(!cnt[x])bit[x]=0;}
	void Add(int x,int d){if(!cnt[x] && d)bit[x]=1;cnt[x]+=d;}
	ull Hash(){ ull hs=0; for(int i=1;i<=40;++i)hs=hs*base+cnt[i];return hs; }
}P[50000];
int tot=1;

int a[maxn],n;
struct eg{int from,to,next;int x,y;}e[M*M];
int head[40010],len;
void lqx(int from,int to,int x,int y)
{ e[++len].from=from,e[len].to=to,e[len].next=head[from],head[from]=len;e[len].x=x,e[len].y=y; }

unordered_map<ull,int>Vis;

void BDFS(){
	queue<int>q;
	P[1].bit.set(1);P[1].cnt[1]=n;
	q.push(1);Vis[P[1].Hash()]=1;
	while(!q.empty()){
		int u=q.front();q.pop();
		int x=P[u].bit._Find_first();
		Sits A=P[u];
		while(x<42 &&  P[u].bit[x]){
			if(P[u].cnt[x]>=2){
				A.Del(x,2);A.Add(x+x,1);
				if(Vis.find(A.Hash())==Vis.end()){ P[++tot]=A; q.push(tot);Vis[A.Hash()]=tot; }
				lqx(u,Vis[A.Hash()],x,x);
				A.Add(x,2),A.Del(x+x,1);
			}
			int y=P[u].bit._Find_next(x);
			while(y<42 && P[u].bit[y]){
				A.Del(x,1),A.Del(y,1),A.Add(x+y,1);
				if(Vis.find(A.Hash())==Vis.end()){ P[++tot]=A; q.push(tot);Vis[A.Hash()]=tot; }
				lqx(u,Vis[A.Hash()],x,y);
				A.Add(x,1),A.Add(y,1),A.Del(x+y,1);
				y=P[u].bit._Find_next(y);
			}
			x=P[u].bit._Find_next(x);
		}
	}
	cerr<<tot<<"\n";
	cerr<<len<<"\n";
}

int dp[41][40010];

int fac[M],inv[M];

void Init(const int N=1600){
	fac[0]=inv[0]=1;
	Rep(i,1,N)fac[i]=1LL*fac[i-1]*i%Mod;
	inv[N]=Inv(fac[N]);
	Dwn(i,N-1,1)inv[i]=1LL*(inv[i+1])*(i+1)%Mod;
}

int Down(int n,int m){if(n<m)return 0;return 1LL*fac[n]*inv[n-m]%Mod;}

void solve(){
	cin>>n;Rep(i,1,n-1)cin>>a[i];
	sort(a+1,a+n);BDFS();Init();
	dp[0][1]=1;
	Rep(x,1,n-1){
		if(x>1){
			for(int u=1;u<=tot;++u)if(dp[x-1][u]){
				int sum=0;
				int y=P[u].bit._Find_first();
				while(y<42 && P[u].bit[y]){
					sum+=(1LL*y*(y-1)/2*P[u].cnt[y])%Mod;
					y=P[u].bit._Find_next(y);
				}
				sum-=a[x-1];
				dp[x-1][u]=1LL*dp[x-1][u]*Down(sum,a[x]-a[x-1]-1)%Mod;
			}
		}
		for(int u=1;u<=tot;++u)if(dp[x-1][u]){
			for(int i=head[u];i;i=e[i].next){
				int v=e[i].to;int a=e[i].x,b=e[i].y;
				if(a!=b)dp[x][v]=(dp[x][v]+1LL*dp[x-1][u]*P[u].cnt[a]%Mod*a%Mod*P[u].cnt[b]%Mod*b%Mod)%Mod;
				else dp[x][v]=(dp[x][v]+1LL*dp[x-1][u]*P[u].cnt[a]%Mod*(P[u].cnt[a]-1)%Mod*inv2%Mod*a%Mod*a%Mod)%Mod;
			}
		}
	}
	int u;
	Rep(i,1,tot)if(P[i].bit[n]){u=i;break;}
	cerr<<dp[n-1][u]<<"\n";
	//dp[n-1][u]=1LL*dp[n-1][u]*pw(1LL*n*(n-1)/2,1LL*n*(n-1)/2-a[n-1])%Mod;
	int lim=n*(n-1)/2-a[n-1];
	Rep(i,1,lim)dp[n-1][u]=1LL*dp[n-1][u]*i%Mod;
	cout<<dp[n-1][u]<<"\n";
}

#undef int
int main (){ ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);return solve(),0; }


Details

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

Test #1:

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

input:

4
1 2 3

output:

576

result:

ok single line: '576'

Test #2:

score: 5
Accepted
time: 10ms
memory: 22248kb

input:

4
1 2 4

output:

144

result:

ok single line: '144'

Test #3:

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

input:

5
1 2 3 4

output:

2160000

result:

ok single line: '2160000'

Test #4:

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

input:

5
1 2 3 6

output:

276480

result:

ok single line: '276480'

Test #5:

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

input:

6
1 2 3 4 5

output:

350972052

result:

ok single line: '350972052'

Test #6:

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

input:

6
1 2 4 7 11

output:

12441600

result:

ok single line: '12441600'

Test #7:

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

input:

7
1 2 3 4 5 6

output:

373181939

result:

ok single line: '373181939'

Test #8:

score: 5
Accepted
time: 7ms
memory: 22264kb

input:

7
1 2 3 6 7 10

output:

655119719

result:

ok single line: '655119719'

Test #9:

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

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: 4ms
memory: 22328kb

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: 7ms
memory: 22500kb

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: 3ms
memory: 22496kb

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: 11ms
memory: 23024kb

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: 0ms
memory: 23020kb

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: 15ms
memory: 24576kb

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: 24ms
memory: 24572kb

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: 47ms
memory: 28996kb

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: 36ms
memory: 28996kb

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: 97ms
memory: 41452kb

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: 102ms
memory: 41456kb

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'