-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsumoversubsets.cpp
More file actions
77 lines (66 loc) · 1.53 KB
/
Copy pathsumoversubsets.cpp
File metadata and controls
77 lines (66 loc) · 1.53 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
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<bits/stdc++.h>
#define pb push_back
typedef long long ll;
typedef long double ld;
using namespace std;
const int N = 20;
/*
Computes the zeta transform of a, stores in f.
f[S] = sum of a[X] where X is a subset of S
Complexity: O(N * 2 ^ N)
*/
void zeta(int a[1 << N], int f[1 << N]) {
for (int i = 0; i < (1 << N); i++) {
f[i] = a[i];
}
for (int k = 0; k < N; k++) {
for (int mask = 0; mask < (1 << N); mask++) {
if (mask >> k & 1) {
f[mask] += f[mask ^ (1 << k)];
}
}
}
}
/*
Computes the mobius transform of a and stores in f.
a[S] = sum of f[X] where X is a subset of S
(Note: a is the INPUT and f is the OUTPUT)
Complexity: O(N * 2 ^ N)
Note: The mobius transform inverses the zeta transform.
*/
void mobius(int a[1 << N], int f[1 << N]) {
for (int i = 0; i < (1 << N); i++) {
f[i] = a[i];
}
for (int k = 0; k < N; k++) {
for (int mask = 0; mask < (1 << N); mask++) {
if (mask >> k & 1) {
f[mask] -= f[mask ^ (1 << k)];
}
}
}
}
// In case you forget :)
void zeta_inv(int a[1 << N], int f[1 << N]) {
mobius(a, f);
}
void mobius_inv(int a[1 << N], int f[1 << N]) {
zeta(a, f);
}
/*
Computes the subset convolution of a and b and stores in f.
f[S] = sum of a[T] * b[S\T] where T is a subset of S
Complexity:
*/
void subset_conv(int a[1 << N], int b[1 << N], int f[1 << N]) {
}
int a[1 << N];
int main(){
a[0] = 1; a[1] = 2; a[2] = 3;
zeta(a, a);
mobius(a, a);
for (int i = 0; i < 3; i++) {
cout << a[i] << endl;
}
return 0;
}