-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1208.cpp
56 lines (51 loc) · 1.16 KB
/
1208.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 <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int arr[41];
int n, s;
bool cmp(const int& a, const int& b) {
return a > b;
}
long long solve() {
int half = n / 2;
int fsize = (1 << (n - half)), bsize = (1 << half);
vector<int> front(fsize), back(bsize);
for (int i = 0; i < fsize; i++)
for (int j = 0; j < n - half; j++)
if (i & (1 << j)) front[i] += arr[j];
for (int i = 0; i < bsize; i++)
for (int j = 0; j < half; j++)
if (i & (1 << j)) back[i] += arr[j + (n - half)];
sort(front.begin(), front.end());
sort(back.begin(), back.end(), cmp);
int fidx = 0, bidx = 0;
long long ret = 0;
while (fidx < fsize && bidx < bsize) {
int p = front[fidx] + back[bidx];
if (p == s) {
long long fcnt = 1, bcnt = 1;
fidx++, bidx++;
while (fidx < fsize && front[fidx] == front[fidx - 1]) {
fidx++;
fcnt++;
}
while (bidx < bsize && back[bidx] == back[bidx - 1]) {
bidx++;
bcnt++;
}
ret += fcnt * bcnt;
}
else if (p < s)
fidx++;
else if (p > s)
bidx++;
}
return s == 0 ? ret - 1 : ret;
}
int main() {
cin >> n >> s;
for (int i = 0; i < n; i++)
cin >> arr[i];
cout << solve() << "\n";
}