QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#355776 | #7932. AND-OR closure | ngpin04# | WA | 1ms | 5884kb | C++14 | 4.2kb | 2024-03-17 08:06:27 | 2024-03-17 08:06:27 |
Judging History
answer
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define ALL(x) x.begin(), x.end()
#define bit(x) (1LL << (x))
#define getbit(x, i) (((x) >> (i)) & 1)
using namespace std;
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
};
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
};
int n;
int sz;
long long A[200003];
long long B[200003];
vector<long long> mem[40];
//
vector<int> vec[45];
//
vector<int> bck[45];
int cnt[45];
long long dp[45];
// is_reachable[i][j] -> j is reachable from i
bool is_reachable[45][45];
bool visited[45];
void dfs(int u, int p)
{
is_reachable[p][u] = true;
for(int v : vec[u])
{
if(is_reachable[p][v]) continue;
dfs(v, p);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> A[i];
for(int j = 0; j < 40; j++)
{
long long bit = (1LL<<j);
if(bit&A[i])
mem[j].push_back(A[i]);
}
}
set<long long> st;
for(int j = 0; j < 40; j++)
{
long long sand = (1LL<<40)-1;
if(mem[j].size() == 0) continue;
for(long long x : mem[j])
{
sand &= x;
// cout << j << " " << x << "\n";
}
st.insert(sand);
}
for(long long x : st)
{
B[++sz] = x;
}
for(int i = 1; i <= sz; i++)
{
set<int> idxes = {0};
for(int j = 1; j < i; j++)
{
// cout << i << " " << j << " " << (B[i]&B[j]) << '\n';
if((B[i]&B[j]) == B[j])
{
for(int x : idxes)
{
// cout << B[x] << " " << (B[x]&B[j]) << "\n";
if((B[x]&B[j]) == B[x])
idxes.erase(x);
idxes.insert(j);
}
}
}
for(int idx : idxes)
{
// cout << "EDJ : " << i << " " << idx << "\n";
vec[i].push_back(idx);
bck[idx].push_back(i);
cnt[idx]++;
}
}
for(int i = 0; i < 40; i++)
dfs(i, i);
auto solve = [&]() {
int n = sz + 1;
vector <long long> reach(n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (i != j && is_reachable[i][j])
reach[i] |= bit(j), reach[j] |= bit(i);
if (n == 1) {
return 2LL;
}
int mid = (n >> 1);
vector <int> dp(bit(n - mid), 0);
for (int mask = 0; mask < bit(n - mid); mask++) {
bool ok = true;
for (int i = 0; i < n - mid; i++) if (getbit(mask, i)) {
long long x = ((long long) mask << mid) & reach[i + mid];
if (x > 0)
ok = false;
}
dp[mask] = ok;
}
for (int i = 0; i < n - mid; i++)
for (int mask = 0; mask < bit(n - mid); mask++) {
if (getbit(mask, i))
dp[mask] += dp[mask ^ bit(i)];
}
long long tot = 0;
for (int mask = 0; mask < bit(mid); mask++) {
bool ok = true;
for (int i = 0; i < mid; i++) if (getbit(mask, i)) {
long long x = (long long) mask & reach[i];
if (x > 0)
ok = false;
}
if (ok) {
long long sub = 0;
for (int i = 0; i < mid; i++) if (getbit(mask, i)) {
sub |= reach[i];
}
sub >>= mid;
sub ^= bit(n - mid) - 1;
// cerr << sub << "\n";
tot += dp[sub];
}
}
return tot;
};
long long res = solve()-1;
if(bck[0].size() == 1) res--;
cout << res << "\n";
// cerr << log2(17179869184) << " " << bit(34) << "\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 5884kb
input:
4 0 1 3 5
output:
4
result:
wrong answer 1st numbers differ - expected: '5', found: '4'