QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#442058 | #7932. AND-OR closure | HKOI0# | WA | 179ms | 35668kb | C++14 | 2.4kb | 2024-06-15 06:17:28 | 2024-06-15 06:17:29 |
Judging History
answer
#include <bits/stdc++.h>
#define all(a) begin(a), end(a)
#define int long long
using namespace std;
const int N = 2e5 + 11;
int a[N];
bool valid[40][40];
bool dp1[1 << 20], dp2[1 << 20];
int sos[1 << 20];
bool val[20][1 << 20];
void build(bool* dp, vector<int> r, int sh){
for (int i = 0; i < 20; i++) {
val[i][0] = true;
for (int j = 1; j < (1 << 20); j++) {
int lb = j & -j;
val[i][j] = val[i][j ^ lb] && valid[sh + i][sh + __lg(lb)];
}
}
for (int msk = 0; msk < (1 << 20); msk++) {
bool ok = true;
if (msk) {
int lb = msk & -msk;
if (!val[__lg(lb)][msk ^ lb]) {
ok = false;
}
}
for (int j = 0; j < 20; j++) {
if (msk & (1 << j)) {
if (r[j] == 0) ok = false;
}
}
dp[msk] = ok;
}
}
void solve(){
int n; cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int and_all = (1LL << 40) - 1;
int or_all = 0;
for (int i = 0; i < n; i++) {
and_all &= a[i];
or_all |= a[i];
}
or_all ^= and_all;
for (int i = 0; i < n; i++) {
a[i] ^= and_all;
}
int msk = (1LL << 41) - 1;
set<int> s;
for (int i = 0; i < 40; i++) {
int x = msk;
for (int j = 0; j < n; j++) {
if ((a[j] >> i) & 1) {
x &= a[j];
}
}
if (x == msk) continue;
s.insert(x);
}
vector<int> r(all(s));
r.resize(40);
for (int i = 0; i < 40; i++) {
for (int j = 0; j < 40; j++) {
valid[i][j] = r[i] == 0 || r[j] == 0 ? false : !((r[i] & r[j]) == r[i] || (r[i] & r[j]) == r[j]);
}
}
build(dp1, {r.begin(), r.begin() + 20}, 0);
build(dp2, {r.begin() + 20, r.end()}, 20);
for (int i = 0; i < (1 << 20); i++) {
sos[i] = dp2[i];
}
for (int j = 0; j < 20; j++) {
for (int i = 0; i < (1 << 20); i += 1 << (j + 1)) {
for (int k = 0; k < (1 << j); k++) {
sos[i + k + (1 << j)] += sos[i + k];
}
}
}
int ans = 0;
for (int i = 0; i < (1 << 20); i++) {
if (!dp1[i]) continue;
int msk = 0;
for (int j = 0; j < 20; j++) {
bool ok = true;
for (int k = 0; k < 20; k++) {
if ((i >> k) & 1) {
if (!valid[k][20 + j]) {
ok = false;
}
}
}
if (ok) msk += (1 << j);
}
ans += sos[msk];
}
cout << ans << '\n';
// for (auto x : s) {
// cout << x << ' ';
// }
// cout << endl;
}
int32_t main(){
#ifndef LOCAL
cin.tie(0)->sync_with_stdio(false);
#endif
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 179ms
memory: 35232kb
input:
4 0 1 3 5
output:
5
result:
ok 1 number(s): "5"
Test #2:
score: 0
Accepted
time: 172ms
memory: 35668kb
input:
5 0 1 2 3 4
output:
8
result:
ok 1 number(s): "8"
Test #3:
score: -100
Wrong Answer
time: 168ms
memory: 35316kb
input:
49 1097363587067 1096810445814 275012137504 1096739142630 1096809921522 1087071335264 829364908576 949625500192 1087142638448 1096200190829 1097292808175 1095750860656 1087144145776 1097346808827 1095734082416 1096755396578 829230678048 1095663303524 1087072842592 1096216444777 949623992864 10962714...
output:
121
result:
wrong answer 1st numbers differ - expected: '52', found: '121'