-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSPOJ-SUBSUMS.cpp
More file actions
64 lines (49 loc) · 1.01 KB
/
SPOJ-SUBSUMS.cpp
File metadata and controls
64 lines (49 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ll n, a, b;
vector<ll> set, soma1, soma2;
cin >> n >> a >> b;
for(ll i = 0; i < n; i++){
ll temp;
cin >> temp;
set.push_back(temp);
}
ll tamCombs = 1 << (n/2);
ll tamSub = (n/2);
for(ll i = 0;i < tamCombs; i++){
ll soma = 0;
for(ll j = 0; j < tamSub; j++){
if(i & (1 << j)){
soma += set[j];
}
}
soma1.push_back(soma);
}
if((n & 1) == 1){
tamSub = (n/2) + 1;
tamCombs = 1 << tamSub;
}else{
tamSub = (n/2);
tamCombs = 1 << tamSub;
}
for(ll i = 0; i < tamCombs; i++){
ll soma = 0;
for(ll j = 0; j < tamSub; j++){
if(i & (1 << j)){
soma += set[(n/2) + j];
}
}
soma2.push_back(soma);
}
sort(soma1.begin(), soma1.end());
vector<ll>::iterator up, down;
ll resp = 0;
for(ll i = 0; i < tamCombs; i++){
up = lower_bound(soma1.begin(),soma1.end(),a-soma2[i]);
down = upper_bound(soma1.begin(),soma1.end(),b-soma2[i]);
resp += (down - up);
}
cout << resp << endl;
}