码迷,mamicode.com
首页 > 其他好文 > 详细

bitset骚操作

时间:2018-11-01 22:31:20      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:class   its   fine   href   blog   scan   二进制   _for   存在   

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

bitset骚操作

标签:class   its   fine   href   blog   scan   二进制   _for   存在   

原文地址:https://www.cnblogs.com/sugewud/p/9892440.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!