bitset的用途挺多的,是一个比较骚的常数优化
一.很多位数的二进制数
poj 2443
http://poj.org/problem?id=2443
直接开个1万位的二进制数,求交就行了。
有关集合求并交的时候可以考虑biset优化
#include<bitset>
#include<cstdio>
#define REP(i, a, b) for(register int i = (a); i < (b); i++)
#define _for(i, a, b) for(register int i = (a); i <= (b); i++)
using namespace std;
const int MAXN = 1e3 + 10;
const int MAXM = 1e4 + 10;
bitset<MAXN> s[MAXM];
int main()
{
int n;
scanf("%d", &n);
_for(i, 1, n)
{
int m, x;
scanf("%d", &m);
_for(j, 1, m)
{
scanf("%d", &x);
s[x][i] = 1;
}
}
int q;
scanf("%d", &q);
while(q--)
{
int a, b;
scanf("%d%d", &a, &b);
if((s[a] & s[b]).any()) puts("Yes");
else puts("No");
}
return 0;
}
二.可以优化01背包中当前状态是否存在的问题,可以在原来的基础上除以32
见这篇博客
https://www.cnblogs.com/chinacwj/p/8371578.html
三.在统计类问题中用集合中的并优化常数
见这道题
https://blog.csdn.net/qq_37867156/article/details/82497948
来源:https://www.cnblogs.com/sugewud/p/9892440.html