QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#335018 | #7925. Chayas | STnofarjo# | WA | 36ms | 1576924kb | C++17 | 2.9kb | 2024-02-22 16:20:05 | 2024-02-22 16:20:05 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#pragma gcc optimize("Ofast")
#pragma gcc target("avx,avx2,fma");
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;
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, b, c] = abc[l];
vector<int> srt = {a, b, c};
sort(srt.begin(), srt.end());
for (int mask=0; mask<(1<<(n-3)); mask++) {
int true_mask = 0;
if (srt[0] > 0) true_mask |= cut(mask, 0, srt[0] - 1);
if (srt[0] + 1 <= srt[1] - 1) true_mask |= (cut(mask, srt[0], srt[1] - 2) << (srt[0] + 1));
if (srt[1] + 1 <= srt[2] - 1) true_mask |= (cut(mask, srt[1] - 1, srt[2] - 3) << (srt[1] + 1));
if (srt[2] + 1 <= n - 1) true_mask |= (cut(mask, srt[2] - 2, n - 4) << (srt[2] + 1));
for (int i=l; i<r; i++) set_true(true_mask, get<1>(abc[i]));
true_mask |= (1 << a);
true_mask |= (1 << c);
for (int i=l; i<r; i++) set_true(true_mask, get<1>(abc[i]));
}
l = r;
}
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 {
for (int m1=mask; m1>0; m1-=m1&-m1) {
int i = __builtin_ctz(m1);
for (int m2=mask; m2>0; m2-=m2&-m2) {
int j = __builtin_ctz(m2);
if (i == j) continue;
int pre_mask = mask & ~(1 << j);
if (!invalid(pre_mask, j)) modadd(dp[mask][j], dp[pre_mask][i]);
}
}
}
}
ans = 0;
for (int i=0; i<n; i++) modadd(ans, dp[(1<<n)-1][i]);
cout << ans << '\n';
}
详细
Test #1:
score: 0
Wrong Answer
time: 36ms
memory: 1576924kb
input:
5 4 1 2 4 2 3 5 3 2 4 1 3 2
output:
5
result:
wrong answer 1st lines differ - expected: '4', found: '5'