bitset 的使用

时间:2020-03-07
本文章向大家介绍bitset 的使用,主要包括bitset 的使用使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
  • std::bitset 的语法就不搬运了, 直接做题吧*

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

题意: 给出 n 个数 \(x_i\), 每个数的取值范围为 \([a_i, b_i]\), 求 \(\sum{x_i}\) 的种类数. \(1 ≤ n ≤ 100, 1 ≤ a_i, b_i ≤ 100\).

思路: 直接状压的话需要枚举每个状态, 有了 bitset 之后就可以整体右移来转移了.


view code


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inc(i, l, r) for (int i = l; i <= r; i++)

const int maxn = 105;

int n, a[maxn], b[maxn];
bitset<1000001> dp[maxn];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n;
    inc(i, 1, n) cin >> a[i] >> b[i];
    dp[0][0] = 1;
    inc(i, 1, n) inc(j, a[i], b[i]) dp[i] |= dp[i - 1] << (j * j);
    cout << dp[n].count() << "\n";
}


hihocoder#1513 : 小Hi的烦恼

题意: 五维偏序

思路: 题意听起来很吓人, 但其实就是对暴力的优化. 复杂度 \(O(\frac{n^2}{w})\). 本题目 N 为3e4所以能过. 分块的写法应该能更快, 留着坑以后过来补.


view code


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inc(i, l, r) for (int i = l; i <= r; i++)
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back

const int maxn = 3e4 + 5;

int n;
pii p[6][maxn];
bitset<maxn> stu[6][maxn], ans;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n;
    inc(i, 1, n) {
        inc(j, 1, 5) {
            cin >> p[j][i].fi;
            p[j][i].se = i;
        }
    }
    inc(i, 1, 5) sort(p[i] + 1, p[i] + n + 1);
    inc(i, 1, 5) {
        inc(j, 2, n) {
            stu[i][p[i][j].se] = stu[i][p[i][j - 1].se];
            stu[i][p[i][j].se].set(p[i][j - 1].se);
        }
    }
    inc(i, 1, n) {
        ans.set();
        inc(j, 1, 5) ans &= stu[j][i];
        cout << ans.count() << "\n";
    }
}


Codeforces - 1097F Alex and a TV Show

题意: 给出 n 个可重集, 有四种操作: 1.把一个集合设为一个数; 2.把一个集合变成另外两个集合的并; 3.把一个集合变为从另外两个集合中各取一个数的 gcd; 4.询问集合中某一个数的个数模2. n ≤ 1e6, 操作 q ≤ 1e6, 值域 7000.

思路: 其实做法是搬运的, 我并不会做... 如果用 bitset 维护每个集合的话不好处理操作 3. 令 bitset 维护每个集合的约数, 考虑每个数出现的奇偶, 就会发现操作 2, 3 分别对应异或, 按位与. 而操作 4 要用莫比乌斯函数推导一下:
\[\begin{aligned}&\sum\limits_{i\in A}[\frac i x=1]\\=&\sum\limits_{i\in A}\sum\limits_{d|\frac i x}\mu(d)\\=&\sum\limits_{d\in A',x|d}\mu(\frac d x)\end{aligned}\]


view code


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inc(i, l, r) for (int i = l; i <= r; i++)

const int maxn = 1e5 + 5;
const int maxm = 7005;

int n, q, op, x, y, z;
bitset<7005> fac[maxm], mul[maxm], mu, a[maxn];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    mu.set();
    inc(i, 1, 7000) {
        for (int j = 2; i * j * j <= 7000; j++) {
            mu[i * j * j] = 0;
        }
    }
    inc(i, 1, 7000) {
        for (int j = 1; i * j <= 7000; j++) {
            fac[i * j][i] = 1;
            mul[i][i * j] = mu[j];
        }
    }
    cin >> n >> q;
    while (q--) {
        cin >> op;
        if (op == 1) {
            cin >> x >> y;
            a[x] = fac[y];
        } else if (op == 2) {
            cin >> x >> y >> z;
            a[x] = a[y] ^ a[z];
        } else if (op == 3) {
            cin >> x >> y >> z;
            a[x] = a[y] & a[z];
        } else {
            cin >> x >> y;
            cout << (a[x] & mul[y]).count() % 2;
        }
    }
}


原文地址:https://www.cnblogs.com/hs-zlq/p/12431843.html