QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#350976 | #7932. AND-OR closure | mc020207 | WA | 1ms | 3768kb | C++17 | 1.5kb | 2024-03-11 11:14:22 | 2024-03-11 11:14:23 |
Judging History
answer
#include<bits/stdc++.h>
#define MAXN 1000010
#define INF 1000000010
#define INFLL (1LL*INF*INF)
#define MOD 1000000007
#define LL long long
#define LLL __int128_t
#define For(i,a,b) for(int i=(a);i<=(b);i++)
#define Rof(i,a,b) for(int i=(a);i>=(b);i--)
using namespace std;
const int B=39;
LL a[MAXN];
vector<LL> li;
int n;
LL solve(LL msk){
if (li.empty()) return 1;
sort(li.begin(),li.end());
li.erase(unique(li.begin(),li.end()),li.end());
// printf("li={");
// for (LL x:li) printf("%lld ",x);
// printf("}\n");
int len=li.size();
int mxp=0;
For(i,1,len-1){
if (__builtin_popcountll(li[i])>__builtin_popcountll(li[mxp])){
assert((msk&li[i])==li[i]);
mxp=i;
}
}
if (__builtin_popcountll(li[mxp])==1) return 1LL<<len;
vector<LL> li2=li;
li.clear();
LL msk2=msk-li[mxp];
for (LL x:li2){
if (msk2&x) li.push_back(msk2&x);
}
LL ans=solve(msk2);
li=li2;
LL old=li[mxp];
li.erase(li.begin()+mxp);
ans+=solve(msk);
li.push_back(old);
// printf("li={");
// for (LL x:li) printf("%lld ",x);
// printf("} ans=%lld\n",ans);
return ans;
}
void Main(){
cin>>n;
For(i,1,n) cin>>a[i];
For(i,0,B){
LL t=-1;
For(j,1,n){
if (a[j]&(1<<i)) t=t==-1?a[j]:t&a[j];
}
if (t!=-1) li.push_back(t);
}
LL andsum=a[1];
For(i,2,n) andsum&=a[i];
cout<<solve((1LL<<40)-1)-(andsum!=0)<<"\n";
}
int main(){
std::ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int T=1;/* cin>>T; */
while (T--) Main();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3768kb
input:
4 0 1 3 5
output:
5
result:
ok 1 number(s): "5"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
5 0 1 2 3 4
output:
8
result:
ok 1 number(s): "8"
Test #3:
score: -100
Wrong Answer
time: 1ms
memory: 3552kb
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:
50
result:
wrong answer 1st numbers differ - expected: '52', found: '50'