QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#311953 | #7932. AND-OR closure | ucup-team139 | RE | 13ms | 35792kb | C++23 | 2.7kb | 2024-01-23 01:04:33 | 2024-01-23 01:04:34 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
void solve(int t){
int n;
cin>>n;
const int bit = 40, bound = 20;
vector<long long> v(bit,0);
bool zero=false;
for(int i=0;i<n;i++){
long long a;
cin>>a;
if(a==0)zero=true;
for(int j=0;j<bit;j++){
if((1ll<<j)&a){
if(v[j]==0)v[j]=a;
else v[j]&=a;
}
}
}
for(int i=0;i<bit;i++){
for(int j=i+1;j<bit;j++){
if(v[j]==v[i])v[j]=0;
}
}
vector grafo(bound,vector<int>());
for(int i=0;i<bound;i++){
for(int j=i+1;j<bound;j++){
if(v[i]!=0 && v[j]!=0){
if((v[j]&v[i])==v[j]){
grafo[i].push_back(j);
}
if((v[j]&v[i])==v[i]){
grafo[j].push_back(i);
}
}
}
}
vector<int> ord;
vector vis(bound,false);
auto dfs = [&](auto &dfs,int nodo){
if(vis[nodo])return;
vis[nodo]=true;
for(auto i : grafo[nodo]){
dfs(dfs,i);
}
ord.push_back(nodo);
};
for(int i=0;i<bound;i++){
if(v[i]!=0 && vis[i]==false)dfs(dfs,i);
}
reverse(ord.begin(),ord.end());
vector dp(ord.size(),vector(1<<bound,-1ll));
auto f = [&](auto &f,int pos,int mask)->long long{
if(pos==ord.size())return 1ll;
if(dp[pos][mask]!=-1)return dp[pos][mask];
long long res=0;
res+=f(f,pos+1,mask);
if((mask&(1<<ord[pos]))==0){
res+=f(f,pos+1,mask|v[ord[pos]]);
}
return dp[pos][mask]=res;
};
long long ans=0;
for(int mask = 0; mask<(1ll<<(bit-bound)); mask++){
bool ok = true;
long long rim = 0;
for(int i=0;i<(bit-bound);i++){
if(mask&(1ll<<i)){
rim|=(v[bound+i]&((1ll<<bound)-1));
if(v[bound+i]==0)ok=false;
break;
}
for(int j=0;j<i;j++){
if(mask&(1ll<<j)){
assert(v[bound+j]!=0);
if((v[bound+i]&v[bound+j])==v[bound+i] || (v[bound+i]&v[bound+j])==v[bound+j]){
ok=false;
break;
}
}
}
if(!ok)break;
}
if(ok){
ans+=f(f,0,rim);
}
}
ans-=!zero;
cout<<ans<<"\n";
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t=1;
//cin>>t;
for(int i=1;i<=t;i++)solve(i);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 11ms
memory: 35792kb
input:
4 0 1 3 5
output:
5
result:
ok 1 number(s): "5"
Test #2:
score: 0
Accepted
time: 13ms
memory: 35788kb
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...