QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#749726#5357. 芒果冰加了空气TernaryTree0 138ms163240kbC++142.2kb2024-11-15 09:44:062024-11-15 09:44:07

Judging History

你现在查看的是最新测评结果

  • [2024-11-15 09:44:07]
  • 评测
  • 测评结果:0
  • 用时:138ms
  • 内存:163240kb
  • [2024-11-15 09:44:06]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long
#define fs first
#define sc second
#define ls (u << 1)
#define rs (u << 1 | 1)
#define mid ((l + r) >> 1)
#define lc ls, l, mid
#define rc rs, mid + 1, r
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define per(i, r, l) for (int i = (r); i >= (l); --i)
#define gc getchar
#define pc putchar

using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;

const int maxn = 5e3 + 10;
const int mod = 1e9 + 7;
const bool multidata = 0;

template<typename T = int>
T read() {
	T x = 0, f = 1; char c = gc();
	while (c < '0' || c > '9') { if (c == '-') f = -f; c = gc(); }
	while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = gc();
	return x * f;
}

template<typename T = int>
void write(T x) {
	if (x < 0) pc('-'), x = -x;
	if (x < 10) return void (pc(x + '0'));
	write<T>(x / 10), pc(x % 10 + '0');
}

int n, ans;
vector<int> tr[maxn];
int f[maxn][maxn], g[maxn], h[maxn], siz[maxn];

int power(int a, int b, int p = mod) {
	int t = 1; a %= p;
	while (b) {
		if (b & 1) t = t * a % p;
		a = a * a % p, b >>= 1;
	}
	return t;
}

int fac[maxn], ifac[maxn], inv[maxn];

void init() {
	fac[0] = 1; rep(i, 1, maxn - 1) fac[i] = fac[i - 1] * i % mod;
	ifac[maxn - 1] = power(fac[maxn - 1], mod - 2); per(i, maxn - 2, 0) ifac[i] = ifac[i + 1] * (i + 1) % mod;
	rep(i, 1, maxn - 1) inv[i] = fac[i - 1] * ifac[i] % mod;
}

void dfs(int u, int fa) {
	f[u][0] = siz[u] = 1;
	for (int v : tr[u]) {
		if (v == fa) continue;
		dfs(v, u);
		rep(i, 0, siz[u] - 1) {
			rep(j, 0, siz[v] - 1) {
				int x = f[u][i] * f[v][j] % mod;
				(g[i] += x) %= mod, (g[i + j + 2] -= x) %= mod;
			}
			rep(j, i + 1, i + siz[v] + 1) (g[j] += g[j - 1]) %= mod;
			rep(j, i, i + siz[v]) h[j] += g[j] * fac[j] % mod * ifac[j - i] % mod, g[j] = 0;
		}
		siz[u] += siz[v];
		rep(i, 0, siz[u] - 1) f[u][i] = h[i], h[i] = 0;
	}
}

void fake_main() {
	n = read();
	rep(i, 1, n - 1) {
		int u = read(), v = read();
		tr[u].push_back(v);
		tr[v].push_back(u);
	}
	dfs(1, 0);
	rep(i, 0, n - 1) (ans += f[1][i]) %= mod;
	write(ans);
}

signed main() {
	init();
	int T = multidata ? read() : 1;
	while (T--) fake_main();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 5908kb

input:

10
1 2
2 3
3 4
5 4
6 4
7 4
8 4
9 4
10 4

output:

-976156875

result:

wrong answer 1st lines differ - expected: '310862', found: '-976156875'

Subtask #2:

score: 0
Wrong Answer

Test #27:

score: 10
Accepted
time: 138ms
memory: 163240kb

input:

5000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52
52 53
...

output:

138172849

result:

ok single line: '138172849'

Test #28:

score: 0
Wrong Answer
time: 23ms
memory: 90872kb

input:

2195
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52
52 53
...

output:

-965414360

result:

wrong answer 1st lines differ - expected: '34585647', found: '-965414360'

Subtask #3:

score: 0
Skipped

Dependency #1:

0%

Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Skipped

Dependency #4:

0%

Subtask #6:

score: 0
Skipped

Dependency #5:

0%