QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#335119#7925. ChayasSTnofarjoTL 3156ms1625604kbC++202.8kb2024-02-22 18:42:402024-02-22 18:42:40

Judging History

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

  • [2024-02-22 18:42:40]
  • 评测
  • 测评结果:TL
  • 用时:3156ms
  • 内存:1625604kb
  • [2024-02-22 18:42:40]
  • 提交

answer

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

const int maxn = 24, maxm = maxn * maxn * maxn, mod = 998244353;
int n, m;
tuple<int, int, int> abc[maxm];
set<vector<int>> st;
bitset<(1<<maxn) * maxn> bst;
int dp[1<<maxn][maxn];
int ans;

int cut(int mask, int l, int r) {
	return (mask >> l) & ((1 << (r-l+1)) - 1);
}

void modadd(int &a, int b) {
	a += b;
	if (a >= mod) a -= mod;
}

void set_true(int mask, int i) {
	bst[mask * maxn + i] = true;
}

bool invalid(int mask, int i) {
	return bst[mask * maxn + i];
}

bool eq(tuple<int, int, int> &t1, tuple<int, int, int> &t2) {
	auto &[a1, b1, c1] = t1;
	auto &[a2, b2, c2] = t2;
	// if (a1 != a2 || c1 != c2) return false;
	// bool pb = (b1 < a1 && b2 < a2) || (b1 > a1 && b1 < c1 && b2 > a2 && b2 < c2) || (b1 > c1 && b2 > c2);
	// return pb;
	return (a1 == a2 && c1 == c2);
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> n >> m;
	for (int i=0; i<m; i++) {
		int a, b, c;
		cin >> a >> b >> c;
		a--, b--, c--;
		abc[i] = {a, b, c};
	}

	for (int i=0; i<m; i++) {
		auto &[a, b, c] = abc[i];
		vector<int> v = {a, b, c};
		sort(v.begin(), v.end());
		if (st.count(v)) {
			cout << "0\n";
			return 0;
		}
		st.insert(v);
	}

	sort(abc, abc + m, [](tuple<int, int, int> &t1, tuple<int, int, int> &t2) {
		auto &[a1, b1, c1] = t1;
		auto &[a2, b2, c2] = t2;
		if (a1 != a2) return a1 < a2;
		if (c1 != c2) return c1 < c2;
		return b1 < b2;
	});
	for (int l=0; l<m; ) {
		int r = l;
		while (r < m && eq(abc[l], abc[r])) r++;

		// gaboleh ke b kalo a dan c sama
		auto &[a, _, c] = abc[l];
		if (a > c) swap(a, c);
		for (int mask=0; mask<(1<<(n-2)); mask++) {
			int true_mask = 0;

			if (a > 0) true_mask |= cut(mask, 0, a - 1);
			if (a + 1 <= c - 1) true_mask |= (cut(mask, a, c - 2) << (a + 1));
			if (c + 1 <= n - 1) true_mask |= (cut(mask, c - 1, n - 3) << (c + 1));
			for (int i=l; i<r; i++) {
				auto &[aa, b, cc] = abc[i];
				set_true(true_mask & ~(1 << b), b);
			}

			true_mask |= (1 << a);
			true_mask |= (1 << c);
			for (int i=l; i<r; i++) {
				auto &[aa, b, cc] = abc[i];
				set_true(true_mask & ~(1 << b), b);
			}
		}

		l = r;
	}

	for (int mask=1; mask<(1<<n); mask++) {
		if ((mask&(mask-1)) == 0) {
			int i = 31 - __builtin_clz(mask);
			dp[mask][i] = !invalid(0, i); // cek dulu i pernah jadi b atau nggak
		} else {
			for (int m1=mask; m1>0; m1-=m1&-m1) {
				int i = __builtin_ctz(m1);
				dp[mask][i] = 0;
				int pre_mask = mask & ~(1 << i);
				for (int m2=pre_mask; m2>0; m2-=m2&-m2) {
					int j = __builtin_ctz(m2);
					if (!invalid(pre_mask, i)) modadd(dp[mask][i], dp[pre_mask][j]);
				}
			}
		}
	}

	ans = 0;
	for (int i=0; i<n; i++) modadd(ans, dp[(1<<n)-1][i]);
	cout << ans << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5696kb

input:

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

output:

4

result:

ok single line: '4'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3592kb

input:

4 2
3 1 4
1 4 3

output:

0

result:

ok single line: '0'

Test #3:

score: 0
Accepted
time: 1ms
memory: 5936kb

input:

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

output:

2

result:

ok single line: '2'

Test #4:

score: 0
Accepted
time: 1ms
memory: 5700kb

input:

6 6
1 6 3
2 3 4
5 6 4
3 5 1
1 3 4
1 2 4

output:

6

result:

ok single line: '6'

Test #5:

score: 0
Accepted
time: 1ms
memory: 5756kb

input:

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

output:

8

result:

ok single line: '8'

Test #6:

score: 0
Accepted
time: 0ms
memory: 5720kb

input:

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

output:

28

result:

ok single line: '28'

Test #7:

score: 0
Accepted
time: 1ms
memory: 6056kb

input:

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

output:

128

result:

ok single line: '128'

Test #8:

score: 0
Accepted
time: 149ms
memory: 108244kb

input:

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

output:

115058708

result:

ok single line: '115058708'

Test #9:

score: 0
Accepted
time: 301ms
memory: 210480kb

input:

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

output:

131696

result:

ok single line: '131696'

Test #10:

score: 0
Accepted
time: 653ms
memory: 413096kb

input:

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

output:

188168263

result:

ok single line: '188168263'

Test #11:

score: 0
Accepted
time: 1367ms
memory: 817308kb

input:

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

output:

131547430

result:

ok single line: '131547430'

Test #12:

score: 0
Accepted
time: 2863ms
memory: 1625604kb

input:

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

output:

128488

result:

ok single line: '128488'

Test #13:

score: 0
Accepted
time: 3156ms
memory: 1625428kb

input:

24 1
3 14 15

output:

256287771

result:

ok single line: '256287771'

Test #14:

score: 0
Accepted
time: 1ms
memory: 3736kb

input:

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

output:

0

result:

ok single line: '0'

Test #15:

score: -100
Time Limit Exceeded

input:

24 2024
1 3 4
1 3 6
1 3 10
1 3 11
1 3 13
1 3 15
1 3 22
1 3 23
1 3 24
1 4 23
1 4 24
1 5 2
1 5 12
1 6 4
1 6 13
1 6 15
1 6 23
1 6 24
1 7 2
1 7 5
1 7 12
1 8 3
1 8 4
1 8 6
1 8 10
1 8 11
1 8 13
1 8 14
1 8 15
1 8 16
1 8 18
1 8 21
1 8 22
1 8 23
1 8 24
1 9 3
1 9 4
1 9 6
1 9 8
1 9 10
1 9 11
1 9 13
1 9 14
1 9 ...

output:


result: