QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#337232 | #7932. AND-OR closure | Days_of_Future_Past# | RE | 1ms | 7740kb | C++20 | 2.7kb | 2024-02-25 09:18:21 | 2024-02-25 09:21:48 |
Judging History
answer
#include <bits/stdc++.h>
#define SZ(c) ((int)(c).size())
using namespace std;
typedef long long LL;
const int N = 2e5+10;
const int D = 40;
inline int bit(LL x, LL i) {
return ((x >> i)&1);
}
int n;
LL a[N];
vector<int> active_d;
int nn;
vector<int> g[D+2];
bool useless(int d) {
for (int i = 1; i <= n; ++i)
if (bit(a[i], d) != bit(a[1], d))
return false;
return true;
}
bool equiv(int t, int d) {
for (int i = 1; i <= n; ++i)
if (bit(a[i], t) != bit(a[i], d))
return false;
return true;
}
bool dom(int d1, int d2) {
for (int i = 1; i <= n; ++i)
if (bit(a[i], d1) == 1 && bit(a[i], d2) == 0)
return false;
return true;
}
int ord[D+2];
bool vis[D+2];
LL reachable[D+2];
void dfs(int u, vector<int>& vs) {
vis[u] = 1;
for (int v: g[u]) if (!vis[v])
dfs(v, vs);
vs.push_back(u);
}
void topo() {
vector<int> vs;
for (int i = 0; i < nn; ++i)
if (!vis[i]) dfs(i, vs);
for (int i = 0; i < nn; ++i)
ord[i] = vs[i];
for (int i = 0; i < nn; ++i) {
fill(vis, vis+nn, 0);
dfs(ord[i], vs);
for (int j = 0; j < nn; ++j)
if (vis[ord[j]]) {
assert(j <= i);
reachable[i] |= (1LL << j);
}
}
}
int nn2;
LL cnt[(1 << 20) + 10], dp[(1 << 20) + 10];
inline bool chk(LL S) {
for (int i = 0; i < 20; ++i)
if (bit(S, i) && (S & reachable[i]) != reachable[i])
return false;
return true;
}
LL ans;
void bf(int i, LL overall_reachable) {
if (i == nn2 - 1) {
LL first_half_reachable = overall_reachable & ((1LL << nn2) - 1);
ans += dp[first_half_reachable];
return;
}
bf(i - 1, overall_reachable);
if (bit(overall_reachable, i) == 0)
bf(i - 1, overall_reachable | reachable[i]);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%lld", a+i);
for (int d = 0; d < D; ++d) {
if (useless(d))
continue;
for (int t = 0; t < d; ++t)
if (equiv(t, d))
continue;
active_d.push_back(d);
}
nn = SZ(active_d);
for (int i = 0; i < nn; ++i)
for (int j = 0; j < nn; ++j) {
if (i == j)
continue;
if (dom(active_d[i], active_d[j])) {
//printf("(%d -> %d)\n", i, j);
g[i].push_back(j);
}
}
topo();
nn2 = nn/2;
//printf("nn2 = %d\n", nn2);
for (LL S = 0; S < (1LL << nn2); ++S) {
if (chk(S))
cnt[S] = 1;
dp[S] = cnt[S];
}
for (int i = 0; i < nn2; ++i) for (LL msk = (1LL << nn2) - 1; msk >= 0; --msk) {
if (bit(msk, i) == 0)
dp[msk] += dp[msk^(1LL << i)];
}
//for (LL S = 0; S < (1LL << nn2); ++S) {
//printf("dp[%lld] = %lld\n", S, dp[S]);
//}
ans = 0;
bf(nn - 1, 0);
cout << ans << endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7708kb
input:
4 0 1 3 5
output:
5
result:
ok 1 number(s): "5"
Test #2:
score: 0
Accepted
time: 1ms
memory: 7740kb
input:
5 0 1 2 3 4
output:
8
result:
ok 1 number(s): "8"
Test #3:
score: -100
Runtime Error
input:
49 1097363587067 1096810445814 275012137504 1096739142630 1096809921522 1087071335264 829364908576 949625500192 1087142638448 1096200190829 1097292808175 1095750860656 1087144145776 1097346808827 1095734082416 1096755396578 829230678048 1095663303524 1087072842592 1096216444777 949623992864 10962714...