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

题目

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

简化题意

给你 (n) 个数的取值范围 (x_i in [a_i, b_i])

(sumlimits_{i = 1}^{n} x_i) 有多少种不同的取值。

思路

背包$ + bitset$ 科技。

还不明白,我背包问题很拉胯。。。

Code

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#include <bitset>
#define MAXN 1000001

int n;
std::bitset<MAXN> ans[101];

int main() {
    scanf("%d", &n);
    ans[0] = 1;
    for (int i = 1, l, r; i <= n; ++i) {
        scanf("%d %d", &l, &r);
        for (int j = l; j <= r; ++j) {
            ans[i] |= (ans[i - 1] << (j * j));
        }
    }
    printf("%d
", ans[n].count());
    return 0;
}
原文地址:https://www.cnblogs.com/poi-bolg-poi/p/13604715.html