QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#334918 | #7925. Chayas | STnofarjo# | WA | 99ms | 1969696kb | C++17 | 3.8kb | 2024-02-22 15:12:42 | 2024-02-22 15:12:42 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int maxn = 24, maxm = maxn * maxn * maxn, mod = 998244353;
int n, m;
int a[maxm], b[maxm], c[maxm];
set<vector<int>> st;
// bitset<1<<maxn> invalid[maxn][maxn]; // invalid[a][b][mask] = boleh ga dari a ke b kalo masknya mask?
// set<int> invalid[maxn][maxn];
bool invalid[1<<maxn][maxn];
int dp[1<<maxn][maxn];
int ans;
int cut(int mask, int l, int r) {
return (mask >> l) & ~(1 << (r-l+1));
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
for (int i=0; i<m; i++) {
cin >> a[i] >> b[i] >> c[i];
a[i]--, b[i]--, c[i]--;
}
for (int i=0; i<m; i++) {
vector<int> abc = {a[i], b[i], c[i]};
sort(abc.begin(), abc.end());
if (st.count(abc)) {
cout << "0\n";
return 0;
}
st.insert(abc);
}
memset(invalid, false, sizeof invalid);
for (int i=0; i<m; i++) {
for (int t=0; t<2; t++) {
vector<pair<int, int>> srt = {{a[i], 0}, {b[i], 1}, {c[i], 2}};
sort(srt.begin(), srt.end());
// vector<pair<int, int>> srt = {{a[i], 0}, {b[i], 1}};
// if (srt[0].first > srt[1].first) swap(srt[0], srt[1]);
// cout << a[i] << ' ' << b[i] << ' ' << c[i] << '\n';
// gaboleh ke c kalo a nyala dan b mati
for (int mask=0; mask<(1<<(n-3)); mask++) {
// (0..srt[0]-1) (srt[0]) (srt[1]..srt)
int true_mask = 0;
if (srt[0].first > 0) true_mask |= cut(mask, 0, srt[0].first - 1);
if (srt[0].second == 0) true_mask |= (1 << srt[0].first);
if (srt[0].first + 1 <= srt[1].first - 1) true_mask |= (cut(mask, srt[0].first, srt[1].first - 2) << (srt[0].first + 1));
if (srt[1].second == 0) true_mask |= (1 << srt[1].first);
if (srt[1].first + 1 <= srt[2].first - 1) true_mask |= (cut(mask, srt[1].first - 1, srt[2].first - 3) << (srt[1].first + 1));
if (srt[2].second == 0) true_mask |= (1 << srt[2].first);
if (srt[2].first + 1 <= n-1) true_mask |= (cut(mask, srt[2].first - 2, n - 3) << (srt[2].first + 1));
// assert((true_mask >> a[i]) & 1);
// assert(!((true_mask >> b[i]) & 1));
// assert(!((true_mask >> c[i]) & 1));
// cout << bitset<5>(true_mask) << '\n';
// invalid[true_mask][a[i]][c[i]] = true;
// invalid[a[i]][c[i]].insert(true_mask);
invalid[true_mask][c[i]] = true;
}
swap(a[i], c[i]);
}
// gaboleh ke b kalo a dan c mati
vector<pair<int, int>> srt = {{a[i], 0}, {b[i], 1}, {c[i], 2}};
sort(srt.begin(), srt.end());
for (int mask=0; mask<(1<<(n-3)); mask++) {
int true_mask = 0;
if (srt[0].first > 0) true_mask |= cut(mask, 0, srt[0].first - 1);
if (srt[0].first + 1 <= srt[1].first - 1) true_mask |= (cut(mask, srt[0].first, srt[1].first - 2) << (srt[0].first + 1));
if (srt[1].first + 1 <= srt[2].first - 1) true_mask |= (cut(mask, srt[1].first - 1, srt[2].first - 3) << (srt[1].first + 1));
if (srt[2].first + 1 <= n-1) true_mask |= (cut(mask, srt[2].first - 2, n - 3) << (srt[2].first + 1));
invalid[true_mask][b[i]] = true;
}
}
memset(dp, 0, sizeof dp);
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 {
vector<int> ons;
for (int i=0; i<n; i++) {
if ((mask >> i) & 1) ons.push_back(i);
}
for (int i : ons) {
for (int j : ons) {
if (i == j) continue;
int pre_mask = mask & ~(1 << j);
// if (!invalid[i][j][pre_mask]) dp[mask][j] = (dp[mask][j] + dp[pre_mask][i]) % mod;
// if (!invalid[i][j].count(pre_mask)) dp[mask][j] = (dp[mask][j] + dp[pre_mask][i]) % mod;
if (!invalid[pre_mask][j]) dp[mask][j] = (dp[mask][j] + dp[pre_mask][i]) % mod;;
}
}
}
}
ans = 0;
for (int i=0; i<n; i++) ans = (ans + dp[(1 << n) - 1][i]) % mod;
cout << ans << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 99ms
memory: 1969660kb
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: 3512kb
input:
4 2 3 1 4 1 4 3
output:
0
result:
ok single line: '0'
Test #3:
score: 0
Accepted
time: 72ms
memory: 1969696kb
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: -100
Wrong Answer
time: 75ms
memory: 1969648kb
input:
6 6 1 6 3 2 3 4 5 6 4 3 5 1 1 3 4 1 2 4
output:
4
result:
wrong answer 1st lines differ - expected: '6', found: '4'