-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCount subsets whose sum is in given range - Meet In The Middle.cpp
123 lines (98 loc) · 2.38 KB
/
Count subsets whose sum is in given range - Meet In The Middle.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Zadatak: Podskupovi
#include <bits/stdc++.h>
#include <ext/rope>
using namespace std;
using namespace __gnu_cxx;
using ld = long double;
using llint = long long;
using ullint = unsigned long long;
using pii = pair <int,int>;
using pcc = pair <char,char>;
using pss = pair <string,string>;
using vi = vector <int>;
using vb = vector <bool>;
using vii = vi::iterator;
#define INF (1<<30)
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define all(c) c.begin(), c.end()
#define ms(name,val) memset(name, val, sizeof name)
#define np nullptr
int n, n1, n2;
llint rangeA, rangeB, ans;
vector <llint> v, A, B;
void calculateSubarray(vector <llint> &x, int sz, int step)
{
for (int t1 = 0; t1 < (1<<sz); ++t1)
{
llint sum = 0;
for (int t2 = 0; t2 < sz; ++t2)
if (t1&(1<<t2)) sum += v[t2+step];
x.push_back(sum);
}
}
int main()
{
ios_base::sync_with_stdio(0);
//cin.tie(0);
// counting subsets whose sum is
// in the given range ...
cin >> n >> rangeA >> rangeB;
v.resize(n);
n1 = (n>>1), n2 = n-n1;
for (int t1 = 0; t1 < n; ++t1)
cin >> v[t1];
if (rangeA > rangeB)
{
cout << 0 << '\n';
return 0;
}
calculateSubarray(A, n1, 0);
calculateSubarray(B, n2, n1);
sort(all(B));
for (int t1 = 0; t1 < A.size(); ++t1)
{
int L = 0, R = B.size()-1, tmp1 = R+1;
while (L <= R)
{
int M = (L+R)>>1;
llint tmpSum = A[t1] + B[M];
if (tmpSum < rangeA)
L = M+1;
else
{
if (tmpSum > rangeB)
R = M-1;
else
{
tmp1 = min(tmp1, M);
R = M-1;
}
}
}
if (tmp1 == B.size()) continue;
int tmp2 = -1;
L = 0, R = B.size()-1;
while (L <= R)
{
int M = (L+R)>>1;
llint tmpSum = A[t1] + B[M];
if (tmpSum < rangeA)
L = M+1;
else
{
if (tmpSum > rangeB)
R = M-1;
else
{
tmp2 = max(tmp2, M);
L = M+1;
}
}
}
if (tmp2 != -1) ans += (tmp2-tmp1+1);
}
cout << ans << '\n';
return 0;
}