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

点击打开链接


用位运算降时间复杂度



#include <bitset>
#include <iostream>
#include <stdio.h>
using namespace std;

int n;
bitset<1000200> b[110];

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

	return 0;
}

猜你喜欢

转载自blog.csdn.net/mr_treeeee/article/details/81042540