QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#517743#8325. 重塑时光pavement25 1077ms5560kbC++173.2kb2024-08-13 13:38:242024-08-13 13:38:25

Judging History

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

  • [2024-08-13 13:38:25]
  • 评测
  • 测评结果:25
  • 用时:1077ms
  • 内存:5560kb
  • [2024-08-13 13:38:24]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define pb push_back

using ll = long long;

const int MOD = (int)1e9 + 7;
int n, m, k, ans, fac[55], inv_fac[55], bm_adj[25], bm_adj_or[(1 << 15) + 5], cnt[(1 << 15) + 5], dp[(1 << 15) + 5][25];
bool vis[25][25], have[25];
vector<int> good_bms, adj[25];

int exp_mod(int a, int b) {
	int r = 1;
	while (b) {
		if (b % 2 == 1) {
			r = (ll)r * a % MOD;
		}
		a = (ll)a * a % MOD;
		b /= 2;
	}
	return r;
}

void dfs(int u, bool cur_vis[]) {
	if (cur_vis[u]) {
		return;
	}
	cur_vis[u] = 1;
	for (auto v : adj[u]) {
		dfs(v, cur_vis);
	}
}

int nck(int n, int k) {
	if (k < 0 || k > n) {
		return 0;
	}
	return (ll)fac[n] * inv_fac[k] % MOD * inv_fac[n - k] % MOD;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> m >> k;
	for (int i = 1, u, v; i <= m; i++) {
		cin >> u >> v;
		u--;
		v--;
		adj[u].pb(v);
		bm_adj[u] |= (1 << v);
	}
	for (int bm = 1; bm < (1 << n); bm++) {
		int j = (bm & -bm);
		bm_adj_or[bm] = bm_adj_or[bm ^ j] | bm_adj[__builtin_ctz(j)];
	}
	for (int i = 0; i < n; i++) {
		dfs(i, vis[i]);
	}
	cnt[0] = 1;
	for (int bm = 1; bm < (1 << n); bm++) {
		vector<int> in_bm, not_in_bm;
		for (int i = 0; i < n; i++) {
			if (bm & (1 << i)) {
				in_bm.pb(i);
			} else {
				not_in_bm.pb(i);
			}
		}
		bool bad = 0;
		for (int i : in_bm) {
			for (int j : in_bm) {
				if (i == j) {
					continue;
				}
				for (int k : not_in_bm) {
					if (vis[i][k] && vis[k][j]) {
						bad = 1;
						break;
					}
				}
				if (bad) {
					break;
				}
			}
			if (bad) {
				break;
			}
		}
		if (!bad) {
			good_bms.pb(bm);
		}
		int is_source = bm;
		for (int j : in_bm) {
			is_source &= ~bm_adj[j];
		}
		for (int j = is_source; j; j -= (j & -j)) {
			cnt[bm] += cnt[bm ^ (j & -j)];
			if (cnt[bm] >= MOD) {
				cnt[bm] -= MOD;
			}
		}
	}
	dp[0][0] = 1;
	for (int bm = 1; bm < (1 << n); bm++) {
		for (int num = 1; num <= n; num++) {
			fill(have, have + n, 0);
			for (int j : good_bms) {
				if ((j & bm) != j) {
					continue;
				}
				int mn = __builtin_ctz(j);
				have[mn] = 1;
			}
			int target = -1;
			for (int j = 0; j < n; j++) {
				if (have[j]) {
					target = j;
					break;
				}
			}
			for (int j : good_bms) {
				if ((j & bm) != j || __builtin_ctz(j) != target) {
					continue;
				}
				int oth = bm ^ j;
				if (bm_adj_or[oth] & j) {
					continue;
				}
				//~ cout << "AT " << bm << ' ' << num << " TRY " << j << ' ' << cnt[j] << '\n';
				dp[bm][num] += (ll)dp[oth][num - 1] * cnt[j] % MOD;
				if (dp[bm][num] >= MOD) {
					dp[bm][num] -= MOD;
				}
			}
		}
	}
	fac[0] = inv_fac[0] = 1;
	for (int i = 1; i <= n + k; i++) {
		fac[i] = (ll)fac[i - 1] * i % MOD;
		inv_fac[i] = exp_mod(fac[i], MOD - 2);
	}
	for (int i = 1; i <= min(n, k + 1); i++) {
		dp[(1 << n) - 1][i] = (ll)dp[(1 << n) - 1][i] * fac[i] % MOD;
		//~ cout << "DP " << i << ' ' << dp[(1 << n) - 1][i] << '\n';
		int coeff = (ll)fac[i - 1] * nck(k, i - 1) % MOD * nck(k + 1, i) % MOD * fac[k - i + 1] % MOD;
		ans += (ll)dp[(1 << n) - 1][i] * coeff % MOD;
		if (ans >= MOD) {
			ans -= MOD;
		}
	}
	//~ cout << ans << '\n';
	cout << (ll)ans * inv_fac[n + k] % MOD << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

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

input:

3 2 0
1 2
1 3

output:

333333336

result:

ok single line: '333333336'

Test #2:

score: 0
Wrong Answer
time: 0ms
memory: 3632kb

input:

5 7 5
1 4
2 3
1 2
4 5
2 5
2 4
1 5

output:

863293657

result:

wrong answer 1st lines differ - expected: '895039689', found: '863293657'

Test #3:

score: 5
Accepted
time: 1074ms
memory: 4468kb

input:

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

output:

76923078

result:

ok single line: '76923078'

Test #4:

score: 0
Time Limit Exceeded

input:

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

output:


result:


Test #5:

score: 0
Time Limit Exceeded

input:

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

output:


result:


Test #6:

score: 0
Wrong Answer
time: 1077ms
memory: 5388kb

input:

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

output:

534320404

result:

wrong answer 1st lines differ - expected: '465070080', found: '534320404'

Test #7:

score: 5
Accepted
time: 1066ms
memory: 4560kb

input:

13 0 13

output:

1

result:

ok single line: '1'

Test #8:

score: 5
Accepted
time: 41ms
memory: 5556kb

input:

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

output:

4362623

result:

ok single line: '4362623'

Test #9:

score: 0
Wrong Answer
time: 2ms
memory: 3684kb

input:

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

output:

843763686

result:

wrong answer 1st lines differ - expected: '426526937', found: '843763686'

Test #10:

score: 0
Wrong Answer
time: 3ms
memory: 3668kb

input:

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

output:

839064645

result:

wrong answer 1st lines differ - expected: '214820829', found: '839064645'

Test #11:

score: 5
Accepted
time: 38ms
memory: 4472kb

input:

13 68 0
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
2 5
2 6
2 7
2 8
2 9
2 10
2 11
2 12
2 13
3 4
3 5
3 6
3 7
3 8
3 9
3 10
3 11
3 12
3 13
4 5
4 6
4 7
4 8
4 9
4 10
4 11
4 12
4 13
5 6
5 7
5 8
5 9
5 10
5 11
5 12
5 13
6 7
6 8
6 9
6 10
6 11
6 12
6 13
7 8
7 10
7 11
7 12
7 13
8 13
9 10
9 11
9 12
9 13...

output:

65784302

result:

ok single line: '65784302'

Test #12:

score: 0
Wrong Answer
time: 196ms
memory: 4704kb

input:

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

output:

232246972

result:

wrong answer 1st lines differ - expected: '361557272', found: '232246972'

Test #13:

score: 0
Time Limit Exceeded

input:

14 11 14
2 13
4 11
4 14
5 14
6 9
6 14
7 11
9 14
10 12
10 14
12 14

output:


result:


Test #14:

score: 0
Wrong Answer
time: 588ms
memory: 5508kb

input:

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

output:

433655277

result:

wrong answer 1st lines differ - expected: '258614192', found: '433655277'

Test #15:

score: 0
Wrong Answer
time: 151ms
memory: 5560kb

input:

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

output:

431378207

result:

wrong answer 1st lines differ - expected: '209616080', found: '431378207'

Test #16:

score: 0
Wrong Answer
time: 169ms
memory: 5292kb

input:

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

output:

330912726

result:

wrong answer 1st lines differ - expected: '701202127', found: '330912726'

Test #17:

score: 0
Time Limit Exceeded

input:

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

output:


result:


Test #18:

score: 0
Time Limit Exceeded

input:

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

output:


result:


Test #19:

score: 0
Time Limit Exceeded

input:

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

output:


result:


Test #20:

score: 0
Time Limit Exceeded

input:

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

output:


result: