-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_Partial_Combination.cpp
More file actions
74 lines (72 loc) · 1.7 KB
/
class_Partial_Combination.cpp
File metadata and controls
74 lines (72 loc) · 1.7 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
#include "auto_util_header.hpp"
template<typename val_t>
class Partial_Combination {
private:
int n;
vector<vector<val_t>> result;
vvi combs; // iCj
void core_func(const vector<val_t> &a, int n, int r, int start) {
if (r == 0 || n < r) return;
Loop(i, combs[n - 1][r - 1]) {
result[start + i].push_back(a[Partial_Combination::n - n]);
}
if (n > 1) {
core_func(a, n - 1, r - 1, start);
core_func(a, n - 1, r, start + combs[n - 1][r - 1]);
}
}
void make_combs(int n) {
combs = vvi(n + 1, vi(n + 1));
Loop(i, n + 1) {
combs[i][0] = 1;
Loop1(j, i) {
combs[i][j] = combs[i - 1][j - 1] + combs[i - 1][j];
}
}
}
public:
vector<vector<val_t>> get_partial_combination(const vector<val_t> &a, int r) {
n = int(a.size());
if (n < r) return {};
make_combs(n);
result = vector<vector<val_t>>(combs[n][r]);
core_func(a, n, r, 0);
return result;
}
};
class Partial_Combination_Bitmask {
private:
int n;
vll result;
vvi combs; // iCj
void core_func(const ll &a, int n, int r, int start) {
if (r == 0 || n < r) return;
ll x = a & -a;
Loop(i, combs[n - 1][r - 1]) {
result[start + i] += x;
}
if (n > 1) {
core_func(a - x, n - 1, r - 1, start);
core_func(a - x, n - 1, r, start + combs[n - 1][r - 1]);
}
}
void make_combs(int n) {
combs = vvi(n + 1, vi(n + 1));
Loop(i, n + 1) {
combs[i][0] = 1;
Loop1(j, i) {
combs[i][j] = combs[i - 1][j - 1] + combs[i - 1][j];
}
}
}
public:
vll get_partial_combination(int n, int r) {
this->n = n;
if (n < r) return {};
make_combs(n);
result = vll(combs[n][r]);
ll a = (1LL << n) - 1;
core_func(a, n, r, 0);
return result;
}
};