spoj ADACOINS - Ada and Coins(树状数组+bitset优化)

ADACOINS - Ada and Coins

#dynamic-programming #datastructures

 

Ada the Ladybug has several coins in her wallet. She is wondering, how many different values [sum of some subset of coins] is she able to make (in given range).

Input

The first line contains two integer numbers 1 ≤ N ≤ 104, 1 ≤ Q ≤ 5*105, the number of coins and number of queries.

The next line contains N integer numbers 1 ≤ Ai ≤ 105, the values of coins in Ada's wallet.

Afterward, Q lines follow, each containing two integers 1 ≤ B ≤ E ≤ 105beginning and end of range, for which ada wants to know the answer

Output

For each query, output the number of distinct values Ada can generate in given range.

Example Input

10 10
1 2 2 3 5 30 31 90 100 100
1 1
1 5
3 6
2 9
30 100
1 10000
30 32
5 12
9 29
190 220

Example Output

1
5
4
8
40
231
3
8
5
25

Output Subsets

[  1,    1]: 1 
[  1,    5]: 1 2 3 4 5 
[  3,    6]: 3 4 5 6 
[  2,    9]: 2 3 4 5 6 7 8 9 
[ 30,  100]: 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 61 62 63 64 65 66 67 68 69 70 71 72 73 74 90 91 92 93 94 95 96 97 98 99 100 
[  1,10000]: 1 2 3 4 5 6 7 8 9 10 11 12 13 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 61 62 63 64 65 66 67 68 69 70 71 72 73 74 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 290 291 292 293 294 295 296 297 298 299 300 301 302 303 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 351 352 353 354 355 356 357 358 359 360 361 362 363 364 
[ 30,   32]: 30 31 32 
[  5,   12]: 5 6 7 8 9 10 11 12 
[  9,   29]: 9 10 11 12 13 
[190,  220]: 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 220

 

这里问你用钱包里的零钱进行组合,在一定范围内有多少种和,dp[i][j]为考虑前i个数,子集和为j可行性(0或1),第i个数只有加入或者不加入,

dp[i][j]=dp[i-1][j]或者dp[i][j]=dp[i-1][j-a[i]];因为只有0和1两种情况,用bitset处理,时间和空间优化为原来的1/32;

#include<bits/stdc++.h>
#define MAXN 10005
#define MAXM 100005
using namespace std;
int a[MAXN];int tree[MAXM];
int n,q;
bitset<MAXM> dp[MAXN];
inline int lowbit(int x)
{
	return x&-x;
}
void add(int x,int y)
{
	for(int i=x;i<MAXM;i+=lowbit(i))
		tree[i]+=y;
}
int getsum(int x)
{
	int sum=0;
	for(int i=x;i>0;i-=lowbit(i))
		sum+=tree[i];
	return sum;
}
int main()
{
	scanf("%d%d",&n,&q);

	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	memset(tree,0,sizeof(tree));
	for(int i=0;i<=n;i++)
		dp[i].set(0);
	for(int i=1;i<=n;i++)
	{
		dp[i]=(dp[i-1]<<a[i]|dp[i-1]);
	}
	for(int i=1;i<MAXM;i++)
		add(i,dp[n][i]);
	for(int i=0;i<q;i++)
	{
		int l,r;
		scanf("%d%d",&l,&r);
		printf("%d\n",getsum(r)-getsum(l-1));
	}
	return 0;
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值