You Gegeguosheng
Advanced algorithm contest Guide notes
P2114 [NOI2014] get up distress syndrome
- bitset
- Binary state compression LYD P8
#include<bits/stdc++.h>
using namespace std;
bitset<30>a,b((1<<30)-1),c;
int n,m,r;
char s[5];
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%s%d",s,&r);
if(s[0]=='A')a&=r,b&=r;
if(s[0]=='O')a|=r,b|=r;
if(s[0]=='X')a^=r,b^=r;
}
for(int i=0;i<30;++i){
if(a[i]|(b[i] && c.to_ulong()<m))
c[i]=1;
}
printf("%d",c.to_ulong());
}
About bitset
bitset for storing binary digits. Bool array of a type like the same, bitset each element has only two values 0 or 1.
But there is an element --bitset space optimization is generally only 1 bit.
bitset each element can be individually accessed, for example, the following code:
Output Format
printf("%d\n",x.to_ulong());
cout<<x<<endl;
bitset <15> a, b (string ( "101")); // define bitset, 15 means that 15
a [10] = 1; // is defined as the first 10 1
Outputting a;
Output B;
a = 101; // 赋值 a
Outputting a;
Output:
1024
000010000000000
5
000000000000101
101
000000001100101
As can be seen, a [10] = 1 + 2 ^ 10 corresponds.
Here it is the direct output bitset mysterious.
Further, a string can go directly to the bitset.
Interestingly, bitset can be assigned directly!
Of course, it is c (101) to assign bitset <15>!
then
bitset<4> a ( string("1001"));
bitset<4> b ( string("0011"));
cout << (a^=b) << endl; // 1010
cout << (a&=b) << endl; // 0010
cout << (a|=b) << endl; // 0011
cout << endl << a << endl; // 0011
cout << b << endl << endl; // 0011
cout << (a<<=2) << endl; // 1100
cout << (a>>=1) << endl; // 0110
cout << (~b) << endl; // 1100
cout << (b<<1) << endl; // 0110
cout << (b>>1) << endl; // 0001
cout << (a==b) << endl; // false (0110==0011)
cout << (a!=b) << endl; // true (0110!=0011)
cout << (a&b) << endl; // 0010
cout << (a|b) << endl; // 0111
cout << (a^b) << endl; // 0101
After reading these, it is estimated that you already have a deep understanding of the bitset, it owned porcelain-bit computing!
then
For the one called a bitset:
a.size () Returns the size (number of bits)
a.count () returns the number 1,
a.any () Returns whether there is a
a.none () Returns whether or not 1
a.set () all turned into 1
a.set (p) of the p + 1 bits become 1
a.set (p, x) the first p + 1 bits become x
a.reset () all turned into 0
a.reset (p) of the p + 1 bits become 0
a.flip () are all negated
a.flip (p) p + 1 the first bit inversion
a.to_ulong () returns its result to unsigned long, if the error is out of range
a.to_ullong () returns the unsigned long long which is converted to the results, if the error is out of range
a.to_string () which returns the result of converting the string
How Not to eight digital placeholders '0' read into it?
int ok=0,x;
rep(i,1,n*n){
rd(x);
if(x==0)ok=1;
else a[i-ok]=x;
}