Newcoder 132 C.简单瞎搞题(01背包+)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/V5ZSQ/article/details/82917458

Description

一共有 n n 个数,第 i i 个数是 x i x_i x i x_i 可以取 [ l i , r i ] [l_i , r_i] 中任意的一个值。 设 S = x i 2 S=\sum x_i^2 ,求 S S 种类数。

Input

第一行一个数 n n
然后 n n 行,每行两个数表示 l i , r i l_i,r_i

( 1 n , l i , r i 100 ) (1\le n,l_i,r_i\le 100)

Output

输出一行一个数表示答案。

Sample Input

5
1 2
2 3
3 4
4 5
5 6

Sample Output

26

扫描二维码关注公众号,回复: 3494480 查看本文章

Solution

01 01 背包,背包体积 10 0 3 100^3 ,时间复杂度 O ( 10 0 5 ) O(100^5) ,用 b i t s e t bitset 优化一下复杂度为 O ( 10 0 5 32 ) O(\frac{100^5}{32})

Code

#include<cstdio>
#include<bitset> 
using namespace std;
bitset<1000005>b[105];
int n,l[105],r[105];
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d%d",&l[i],&r[i]);
	b[0].set(0);
	for(int i=1;i<=n;i++)
		for(int j=l[i];j<=r[i];j++)
			b[i]|=(b[i-1]<<(j*j));
	printf("%d\n",b[n].count());
	return 0;
}

猜你喜欢

转载自blog.csdn.net/V5ZSQ/article/details/82917458
132