状压dp通常由于空间限制需要用到二进制。
这里介绍一种二进制处理方法——biset。
#include<cstdio>
#include<iostream>
#include<bitset>
#include<cstring>
using namespace std;
bitset<128> num(10),num1(-10);
int main(){
cout<<num.to_ulong()<<endl;
for(int i=3;i>=0;i--){
cout<<num[i];
}
cout<<endl;
cout<<num<<endl;
cout<<num.flip()<<endl;
cout<<num1<<endl;
bitset<4> foo (string("1001"));
bitset<4> bar (string("0011"));
cout<<"第一个数"<<foo<<endl;
cout<<(foo[1]|1)<<endl;
cout<<"第二个数"<<bar<<endl;
cout << (foo^=bar) << '\n'; // 1010 (XOR,assign)
cout << (foo&=bar) << '\n'; // 0010 (AND,assign)
cout << (foo|=bar) << '\n'; // 0011 (OR,assign)
cout << (foo<<=2) << '\n'; // 1100 (SHL,assign)
cout << (foo>>=1) << '\n'; // 0110 (SHR,assign)
cout << (~bar) << '\n'; // 1100 (NOT)
cout << (bar<<1) << '\n'; // 0110 (SHL)
cout << (bar>>1) << '\n'; // 0001 (SHR)
cout << (foo==bar) << '\n'; // false (0110==0011)
cout << (foo!=bar) << '\n'; // true (0110!=0011)
cout << (foo&bar) << '\n'; // 0010
cout << (foo|bar) << '\n'; // 0111
cout << (foo^bar) << '\n'; // 0101
foo.size() ; //返回大小(位数)
foo.count() ; //返回1的个数
foo.any() ; //返回是否有1
foo.none() ; //返回是否没有1
cout<<foo<<endl;
foo.set() ; //全都变成1
int p=1,x=1;
cout<<foo<<endl;
foo.set(p) ; //将第p + 1位变成1
foo.flip() ; //全都取反
cout<<"前:"<<foo<<endl;
foo.set(p, x) ; //将第p + 1位变成x
cout<<"后:"<<foo<<endl;
foo.reset() ; //全都变成0
foo.reset(p) ; //将第p + 1位变成0
foo.flip(p) ; //将第p + 1位取反
cout<<foo.to_ulong()<<endl; //返回它转换为unsigned long的结果,如果超出范围则报错
// cout<<foo.to_ullong()<<endl; //返回它转换为unsigned long long的结果,如果超出范围则报错
foo.to_string() ; //返回它转换为string的结果
return 0;
}