ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

bitset使用说明及典型例题

2022-07-07 15:34:12  阅读:169  来源: 互联网

标签:典型 foo cout long include bitset 例题 string


转载博客:
https://blog.csdn.net/weixin_45697774/article/details/105563993?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-0-105563993-blog-106273675.pc_relevant_multi_platform_whitelistv1&spm=1001.2101.3001.4242.1&utm_relevant_index=3

使用bitset一般会使得算法复杂度变为原来的\(\frac{1}{32}\)

构造函数

bitset<4> bitset1;  //无参构造,长度为4,默认每一位为0

bitset<8> bitset2(12);  //长度为8,将12转换成二进制保存到后面,前面用0补充

string s = "100101";
bitset<10> bitset3(s);  //长度为10,前面用0补充

char s2[] = "10101";
bitset<13> bitset4(s2);  //长度为13,前面用0补充

cout << bitset1 << endl;  //0000
cout << bitset2 << endl;  //00001100
cout << bitset3 << endl;  //0000100101
cout << bitset4 << endl;  //0000000010101

可用操作符

bitset<4> foo (string("1001"));
bitset<4> bar (string("0011"));

cout << (foo^=bar) << endl;       // 1010 (foo对bar按位异或后赋值给foo)
cout << (foo&=bar) << endl;       // 0010 (按位与后赋值给foo)
cout << (foo|=bar) << endl;       // 0011 (按位或后赋值给foo)

cout << (foo<<=2) << endl;        // 1100 (左移2位,低位补0,有自身赋值)
cout << (foo>>=1) << endl;        // 0110 (右移1位,高位补0,有自身赋值)

cout << (~bar) << endl;           // 1100 (按位取反)
cout << (bar<<1) << endl;         // 0110 (左移,不赋值)
cout << (bar>>1) << endl;         // 0001 (右移,不赋值)

cout << (foo==bar) << endl;       // false (0110==0011为false)
cout << (foo!=bar) << endl;       // true  (0110!=0011为true)

cout << (foo&bar) << endl;        // 0010 (按位与,不赋值)
cout << (foo|bar) << endl;        // 0111 (按位或,不赋值)
cout << (foo^bar) << endl;        // 0101 (按位异或,不赋值)

bitset<4> foo ("1011");
cout << foo[0] << endl;  //1
cout << foo[1] << endl;  //1
cout << foo[2] << endl;  //0

可用函数

bitset<8> foo ("10011011");

cout << foo.count() << endl;  //5  (count函数用来求bitset中1的位数,foo中共有5个1
cout << foo.size() << endl;   //8  (size函数用来求bitset的大小,一共有8位

cout << foo.test(0) << endl;  //true  (test函数用来查下标处的元素是0还是1,并返回false或true,此处foo[0]为1,返回true
cout << foo.test(2) << endl;  //false  (同理,foo[2]为0,返回false

cout << foo.any() << endl;  //true  (any函数检查bitset中是否有1
cout << foo.none() << endl;  //false  (none函数检查bitset中是否没有1
cout << foo.all() << endl;  //false  (all函数检查bitset中是全部为1

bitset<8> foo ("10011011");
cout << foo.flip(2) << endl;  //10011111  (flip函数传参数时,用于将参数位取反,本行代码将foo下标2处"反转",即0变1,1变0
cout << foo.flip() << endl;   //01100000  (flip函数不指定参数时,将bitset每一位全部取反
cout << foo.set() << endl;    //11111111  (set函数不指定参数时,将bitset的每一位全部置为1
cout << foo.set(3,0) << endl;  //11110111  (set函数指定两位参数时,将第一参数位的元素置为第二参数的值,本行对foo的操作相当于foo[3]=0
cout << foo.set(3) << endl;    //11111111  (set函数只有一个参数时,将参数下标处置为1
cout << foo.reset(4) << endl;  //11101111  (reset函数传一个参数时将参数下标处置为0
cout << foo.reset() << endl;   //00000000  (reset函数不传参数时将bitset的每一位全部置为0

bitset<8> foo ("10011011");

string s = foo.to_string();  //将bitset转换成string类型
unsigned long a = foo.to_ulong();  //将bitset转换成unsigned long类型
unsigned long long b = foo.to_ullong();  //将bitset转换成unsigned long long类型

cout << s << endl;  //10011011
cout << a << endl;  //155
cout << b << endl;  //155

典型例题

  • 题意:一共有\(n\)个数,第\(i\)个数是\(x_i\),\(x_i\)可以取\([l_i, r_i]\)中任意的一个值。设\(S = \sum{{x_i}^2}\),求\(S\)种类数。
  • 数据范围:\(1 \leq n, l_i, r_i \leq 100\)
  • 思路:经典的背包问题,设\(f(i, j)\)表示只考虑前\(i\)个数,能否获得\(j\),转移方程显然。该问题可以使用bitset优化时间复杂度。我们用二进制表示能获得数的状态,设\(b_i\)表示考虑前\(i\)个数能凑出来的数的状态,若第\(i\)个数选择\(t\),则\(b_i = b_{i - 1} << t^2\)。在这里可以使用滚动数组的思想优化空间复杂度。
  • 代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <bitset>

using namespace std;

const int N = 1000010;

int n;
bitset<N> a, b;

int main()
{
    scanf("%d", &n);
    b[0] = 1;
    int l, r;
    for(int i = 1; i <= n; i ++) {
        scanf("%d%d", &l, &r);
        a.reset();
        for(int j = l; j <= r; j ++) {
            a |= b << (j * j);
        }
        b = a;
    }
    printf("%d\n", b.count());
    return 0;
}

标签:典型,foo,cout,long,include,bitset,例题,string
来源: https://www.cnblogs.com/miraclepbc/p/16454857.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有