bitset的创建:

#include<bitset>

bitset<32> ar; //默认全为0
bitset<32> ar(n); //n的二进制
bitset<32> ar(str); //01串
bitset<n> ar(str,pos,n); //从str第p位开始的n位

###基础用法:

ar.size();//返回位数
ar.count();//返回1的个数
ar.any();//返回是否有1
ar.none();//返回是否没有1
ar.test(p);//返回第p位是不是1
ar.set();//全部设为1
ar.set(p);//第p位设为1
ar.reset();//全部设为0
ar.reset(p);//第p位设为0
ar.flip();//全部反转
ar.flip(p);//第p位反转
ar.to_ulong();//返回unsigned long
ar.to_ullong();//返回unsigned long long
ar.to_string();//返回string

###例题:

515. 「LibreOJ β Round #2」贪心只能过样例

(牛客练习赛22也有这个题)

AC代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <bitset>
#define lowbit(x) (x&(-x))
using namespace std;
typedef long long LL;
const int N = 1e6 + 7;
const int INF = 0x3f3f3f3f;
int n, l, r;
int main(int argc, char const *argv[]){
while(~scanf("%d", &n)){
bitset<1000005> a, b;
b[0] = 1;
while(n--){
scanf("%d%d", &l, &r);
for(int i = l; i <= r; ++i){
a |= (b<<i*i);
}
b = a;
a.reset();
}
printf("%d\n", b.count());
}
return 0;
}

####统计二进制中1的数量:

//方法一:
int bitCount(unsigned int n){
unsigned int tmp = n - ((n >> 1) & 033333333333) - ((n >> 2) & 011111111111);
return ((tmp + (tmp >> 3)) & 030707070707) % 63;
} //方法二:
bitset<32> a(n);
a.count(); //方法三:
__builtin_popcount(n)//返回二进制位中有多少个1
__builtin_popcountll//longlong
__builtin_parity(n)//返回二进制位中1的数量的奇偶性,奇数返回1,偶数返回0
__builtin_ffs(n)//返回二进制末尾最后一个1的位置,从一开始
__builtin_ctz(n)//返回二进制末尾后面0的个数,当n为0时,和n的类型有关 #define LeftPos(x) 32 - __builtin_clz(x) - 1
#define LeftPosll(x) 64 - __builtin_clzll(x) - 1

bitset简单用法的更多相关文章

  1. CATransition(os开发之画面切换) 的简单用法

    CATransition 的简单用法 //引进CATransition 时要添加包“QuartzCore.framework”,然后引进“#import <QuartzCore/QuartzCo ...

  2. jquery.validate.js 表单验证简单用法

    引入jquery.validate.js插件以及Jquery,在最后加上这个插件的方法名来引用.$('form').validate(); <!DOCTYPE html PUBLIC " ...

  3. NSCharacterSet 简单用法

    NSCharacterSet 简单用法 NSCharacterSet其实是许多字符或者数字或者符号的组合,在网络处理的时候会用到 NSMutableCharacterSet *base = [NSMu ...

  4. [转]Valgrind简单用法

    [转]Valgrind简单用法 http://www.cnblogs.com/sunyubo/archive/2010/05/05/2282170.html Valgrind的主要作者Julian S ...

  5. Oracle的substr函数简单用法

    substr(字符串,截取开始位置,截取长度) //返回截取的字 substr('Hello World',0,1) //返回结果为 'H'  *从字符串第一个字符开始截取长度为1的字符串 subst ...

  6. Ext.Net学习笔记19:Ext.Net FormPanel 简单用法

    Ext.Net学习笔记19:Ext.Net FormPanel 简单用法 FormPanel是一个常用的控件,Ext.Net中的FormPanel控件同样具有非常丰富的功能,在接下来的笔记中我们将一起 ...

  7. TransactionScope简单用法

    记录TransactionScope简单用法,示例如下: void Test() { using (TransactionScope scope = new TransactionScope()) { ...

  8. WPF之Treeview控件简单用法

    TreeView:表示显示在树结构中分层数据具有项目可展开和折叠的控件 TreeView 的内容是可以包含丰富内容的 TreeViewItem 控件,如 Button 和 Image 控件.TreeV ...

  9. listActivity和ExpandableListActivity的简单用法

    http://www.cnblogs.com/limingblogs/archive/2011/10/09/2204866.html 今天自己简单的总结了listActivity和Expandable ...

随机推荐

  1. Vue学习笔记【13】——键盘修饰符以及自定义键盘修饰符

    1.x版本中自定义键盘修饰符[了解] Vue.directive('on').keyCodes.f2 = 113; 2.x版本中自定义键盘修饰符 通过Vue.config.keyCodes.名称 = ...

  2. 【hihocoder 1554】最短的 Nore0061

    [链接]http://hihocoder.com/problemset/problem/1554 [题意] 中文题 [题解] DP; 设f[i][j][k]表示前i个字符,第一个串已经得到了前j个字符 ...

  3. go指定分隔符格式化时间

    一.代码 package main import ( "fmt" "strings" "strconv" "time" ...

  4. opencv打开摄像头并新建窗口显示

    几个程序使用的基本函数如下: ******************************************************************* cvCreateCameraCap ...

  5. IIS身份验证和文件操作权限(二、匿名身份验证)

    一.配置匿名身份验证 二.浏览站点 -- 操作文件 ①无操作权限 点击写入 ②有操作权限(IIS_IUSRS.Authenticated Users两个任选一个) 点击写入

  6. CentOS6 图形界面(gnome)安装

    CentOS6相对于CentOS5的安装有了不少的进步,有不少默认的选项可以选择,如: Desktop :基本的桌面系统,包括常用的桌面软件,如文档查看工具. Minimal Desktop :基本的 ...

  7. 3.4 redux 异步

    在大多数的前端业务场景中,需要和后端产生异步交互,在本节中,将详细讲解 redux 中的异步方案以及一些异步第三方组件,内容有: redux 异步流 redux-thunk redux-promise ...

  8. PAT_A1112#Stucked Keyboard

    Source: PAT A1112 Stucked Keyboard (20 分) Description: On a broken keyboard, some of the keys are al ...

  9. 1060 Are They Equal (25 分)

    1060 Are They Equal (25 分)   If a machine can save only 3 significant digits, the float numbers 1230 ...

  10. 1010 Radix (25 分)

    Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...