牛客练习赛22 C-简单瞎搞题(bitset)

bitset类似于bool类型的数组,一个元素占1bits,空间得到极大优化。

std::bitset<16> foo;

std::bitset<16> bar (0xfa2);

std::bitset<16> baz (std::string("0101111001"));

foo: 0000000000000000
bar: 0000111110100010

baz: 0000000101111001



Please, enter an 8-bit binary number: 11111111
bitset.all(): true    //判断是否每一位都为1
bitset.any(): true    //判断是否有1

bitset.none(): false    //判断是否全都不为1

bitset.count():8    //统计共有几位为1



std::bitset<4> foo (std::string("0001"));

std::cout << foo.flip(2) << '\n';     // 0101    把第二位0反转为1

std::cout << foo.flip() << '\n';      // 1010    全部反转,0变1,1变0


跟数组一样,可以foo[1]=1;             // 0010

foo[2]=foo[1];        // 0110


std::bitset<4> foo (std::string("1011"));

std::cout << foo.reset(1) << '\n';    // 1001    把第一位reset,变为0

std::cout << foo.reset() << '\n';     // 0000    全部reset,全0


std::bitset<4> foo;

std::cout << foo.set() << '\n';       // 1111    全部set为1
std::cout << foo.set(2,0) << '\n';    // 1011    第二位set为0,参数默认为1,可修改

std::cout << foo.set(2) << '\n';      // 1111    第二位set为1

bitset.size()    //bitset有多少个bits,也即是bitset的大小

bitset.test(i)    //测试第i位,为1返回true,为0返回false

bitset.to_string();

bitset.tollong();    //返回unsigned long long

bitset.to_ulong();    //返回unsigned long




/*

1 ≤ n , li , ri ≤ 100

5
1 2
2 3
3 4
4 5
5 6

26

*/

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;

bitset<N> b,a;  //S种类数不多于100*100*100,所以bitset开1e6

//bitset的第i位为1,表示可以得到该结果,最后统计1的个数,就是最终答案
//对于当前的l,r   算出有多少个结果,再加到上一次的bitset中

int main()
{
    int n,l,r;
    scanf("%d",&n);
    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;
}

猜你喜欢

转载自blog.csdn.net/Kyrie_10/article/details/81045126
今日推荐