-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeet in the Middle.cpp
56 lines (49 loc) · 1.24 KB
/
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
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll countSumSetSumEqualX(vector<ll> nums, ll k) {
ll n = nums.size();
vector<ll> v1(nums.begin(), nums.begin() + n / 2);
vector<ll> v2(nums.begin() + n / 2, nums.end());
vector<ll> first;
vector<ll> second;
for (ll i = 0; i < (1 << v1.size()); i++) {
ll sum = 0;
for (ll j = 0; j < v1.size(); j++) {
if (i & (1 << j)) {
sum += v1[j];
}
}
first.push_back(sum);
}
for (ll i = 0; i < (1 << v2.size()); i++) {
ll sum = 0;
for (ll j = 0; j < v2.size(); j++) {
if (i & (1 << j)) {
sum += v2[j];
}
}
second.push_back(sum);
}
ll ans = 0;
sort(first.begin(), first.end());
sort(second.begin(), second.end());
for (ll i = 0; i < first.size() && first[i] <= k; i++) {
ll ind2 = upper_bound(second.begin(), second.end(), k - first[i]) -
second.begin();
ll ind1 = lower_bound(second.begin(), second.end(), k - first[i]) -
second.begin();
ans += ind2 - ind1;
}
return ans;
}
int main() {
ll i, j, k, x, y, n;
cin >> n >> k;
vector<ll> v, v2;
for (ll i = 0; i < n; i++) {
cin >> x;
v.push_back(x);
}
cout << countSumSetSumEqualX(v, k) << endl;
}