UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#158796#31. MSTjijidawang100183ms10148kbC++112.6kb2022-08-18 20:45:572022-08-18 20:45:59

answer

#include <bits/stdc++.h>
#define file(x) {freopen(x".in", "r", stdin); freopen(x".out", "w", stdout);}
template<typename T>
inline T chkmin(T& x, const T& y){if (x > y) x = y; return x;}
template<typename T>
inline T chkmax(T& x, const T& y){if (x < y) x = y; return x;}
#define rep(x, a) memset(x, a, sizeof x);
#define cls(x) rep(x, 0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair<int, int> pii;
const int N = 45, _M_ = 12345, base = 1331, P = 1e9+7;
int n, m, a[N], siz[N], fac[N*N], ifac[N*N], Bx[N], id[N][N], ans;
inline int qpow(int a, int n)
{
	int ans = 1;
	while (n)
	{
		if (n & 1) ans = 1ll * ans * a % P;
		a = 1ll * a * a % P; n >>= 1;
	} return ans;
}
inline void initfac(int n)
{
	fac[0] = 1;
	for (int i=1; i<=n; i++) fac[i] = 1ll * i * fac[i-1] % P;
	ifac[n] = qpow(fac[n], P-2);
	for (int i=n-1; i>=0; i--) ifac[i] = 1ll * ifac[i+1] * (i+1) % P;
}
inline int A(int n, int m){return n<m ? 0 : 1ll * fac[n] * ifac[n-m] % P;}
int now, tt[N], B[N], v[N][_M_][N], e[N][_M_], dp[N][_M_];
unordered_map<ull, int> M[N];
void dfs(int b, int st, int lst)
{
	if (lst * (now - st + 1) > b) return ;
	if (st > now)
	{
		ull hash = 0;
		for (int i=1; i<st; i++) hash = hash * base + B[i];
		M[now][hash] = ++tt[now];
		for (int i=1; i<st; i++)
		{
			v[now][tt[now]][i] = B[i];
			e[now][tt[now]] += 1ll * B[i] * (B[i]-1) / 2;
		}
		return ;
	}
	if (st == now){B[st] = b; dfs(0, st+1, 0); return ;}
	for (int i=lst; i<=b; i++){B[st] = i; dfs(b-i, st+1, i);}
}
int main()
{
    scanf("%d", &n); initfac(a[1] = n*(n-1)/2);
    for (int i=n; i>1; i--) scanf("%d", a+i);
    for (int i=1; i<=n; i++){now = i; dfs(n, 1, 1);}
    dp[n][1] = 1;
	for (int i=n; i>1; i--)
		for (int s=1; s<=tt[i]; s++)
		{
			dp[i][s] = 1ll * dp[i][s] * A(e[i][s]-a[i+1], a[i]-a[i+1]-1) % P;
			memset(id, 0, sizeof id);
			for (int j=1; j<=i; j++) B[j] = v[i][s][j];
			for (int u=1; u<=i; u++)
				for (int v=u+1; v<=i; v++)
				{
					if (!id[B[u]][B[v]])
					{
						int top = 0;
						for (int p=1; p<=i; p++)
							if ((p != u) && (p != v)) Bx[++top] = B[p];
						Bx[++top] = B[u] + B[v];
						for (int p=top-1; p; p--) // a part of insertion sort
							if (Bx[p] > Bx[p+1]) swap(Bx[p], Bx[p+1]);
							else break;
						ull hash = 0;
						for (int p=1; p<=top; p++) hash = hash * base + Bx[p];
						id[B[u]][B[v]] = M[i-1][hash];
					}
					(dp[i-1][id[B[u]][B[v]]] += 1ll * dp[i][s] * B[u] % P * B[v] % P) %= P;
				}
		}
	printf("%d\n", dp[1][1] = 1ll * dp[1][1] * A(e[1][1] - a[2], a[1] - a[2] - 1) % P);
	return 0;
}

详细

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

Test #1:

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

input:

4
1 2 3

output:

576

result:

ok single line: '576'

Test #2:

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

input:

4
1 2 4

output:

144

result:

ok single line: '144'

Test #3:

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

input:

5
1 2 3 4

output:

2160000

result:

ok single line: '2160000'

Test #4:

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

input:

5
1 2 3 6

output:

276480

result:

ok single line: '276480'

Test #5:

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

input:

6
1 2 3 4 5

output:

350972052

result:

ok single line: '350972052'

Test #6:

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

input:

6
1 2 4 7 11

output:

12441600

result:

ok single line: '12441600'

Test #7:

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

input:

7
1 2 3 4 5 6

output:

373181939

result:

ok single line: '373181939'

Test #8:

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

input:

7
1 2 3 6 7 10

output:

655119719

result:

ok single line: '655119719'

Test #9:

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

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

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

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

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

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: 2000kb

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

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

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

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

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

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

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'