-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitmask.cpp
More file actions
42 lines (34 loc) · 869 Bytes
/
Bitmask.cpp
File metadata and controls
42 lines (34 loc) · 869 Bytes
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
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
// Calculate sum of every subset, O(2^n)
vector<int64_t> sums(1 << n);
for (int mask = 1; mask < (1 << n); mask++) {
int b = __builtin_ctz(mask);
sums[mask] = sums[mask & (mask - 1)] + a[b];
}
for (int i = 0; i < (1 << n); i++) {
cout << sums[i] << " \n"[i == (1 << n) - 1];
}
/*
Iterating through all masks with their submasks, O(3^n)
for (int mask = 0; mask < (1 << n); mask++)
for (int sub_mask = mask; sub_mask; sub_mask = (sub_mask - 1) & mask)
*/
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
//cin >> t;
while (t--) {
solve();
}
return 0;
}