Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add variations #23

Merged
merged 5 commits into from
Jun 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions math/variations.11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Variations with repetitions
#include <vector>
Sprdalo marked this conversation as resolved.
Show resolved Hide resolved
#include <tuple>
using namespace std;

/*snippet-begin*/
template<class T = int>
struct variations {
struct iterator {
T l, h;
vector<T> a;

const vector<T>& operator* () const { return a; }
bool operator== (const iterator& b) const { return tie(l, h, a) == tie(b.l, b.h, b.a); }
bool operator!= (const iterator& b) const { return !(*this == b); }
iterator& operator++ () {
int i = (int)a.size() - 1;
a[i]++;
while (i && a[i] > h) {
a[i] = l;
a[--i]++;
}
return *this;
}
};

int n, k; T l;
variations(int n, int k, T l = 0) : n(n), k(k), l(l) {}

iterator begin() {
return {l, l+k-1, vector<T>(n, l)};
}

iterator end() {
vector<T> e(n, l);
e[0] = l+k;
return {l, l+k-1, move(e)};
}
};
/*snippet-end*/

int main() {
variations<> v(2,3,1);

int ind = 0;
vector<vector<int>> a = {{1,1},{1,2},{1,3},{2,1},{2,2},{2,3},{3,1},{3,2},{3,3}};
ivan100sic marked this conversation as resolved.
Show resolved Hide resolved
for (auto it = v.begin(); it != v.end(); ++it){
if (*it != a[ind]) return 1;
++ind;
}

return (ind != (int)a.size());
}
Loading