-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathperm.cpp
More file actions
executable file
·36 lines (36 loc) · 900 Bytes
/
perm.cpp
File metadata and controls
executable file
·36 lines (36 loc) · 900 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
// Permutation
// https://youtu.be/-j02o6__jgs?t=7302
struct Perm : vector<int> {
#define n (int)(size())
#define p (*this)
Perm(int _n): vector<int>(_n) {
iota(begin(), end(), 0);
}
template<class...Args> Perm(Args...args): vector<int>(args...) {}
Perm(initializer_list<int> a): vector<int>(a.begin(),a.end()) {}
Perm operator+(const Perm& a) const {
Perm r(n);
for (int i = 0; i < n; ++i) r[i] = p[a[i]];
return r;
}
Perm& operator+=(const Perm& a) {
return *this = (*this)+a;
}
Perm operator-() const {
Perm r(n);
for (int i = 0; i < n; ++i) r[p[i]] = i;
return r;
}
Perm operator-(const Perm& a) const {
return *this + -a;
}
Perm& operator-=(const Perm& a) {
return *this += -a;
}
// next permutation
bool operator++() {
return next_permutation(begin(),end());
}
#undef n
#undef p
};