From 6711f74656ae1f0ea34dde451d5c99f455da44b1 Mon Sep 17 00:00:00 2001 From: Marko Date: Tue, 11 Jun 2024 16:14:43 +0200 Subject: [PATCH 1/5] Add variations --- math/variations.11.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 math/variations.11.cpp diff --git a/math/variations.11.cpp b/math/variations.11.cpp new file mode 100644 index 0000000..e85586b --- /dev/null +++ b/math/variations.11.cpp @@ -0,0 +1,56 @@ +// Binomials +#include +using namespace std; +using mint = int; + +/*snippet-begin*/ +template +struct variations { + struct iterator { + int l, h; + vector a; + + const vector& 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(n, l)}; + } + + iterator end() { + vector 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> a = {{1,1},{1,2},{1,3},{2,1},{2,2},{2,3},{3,1},{3,2},{3,3}}; + for (auto it = v.begin(); it != v.end(); ++it){ + auto b = *it; + for (int i = 0; i < a[ind].size(); ++i){ + if (b[i] != a[ind][i]) return 0; + } + ++ind; + } + + return (ind == a.size()); +} \ No newline at end of file From ba710e32efeea545b61b53dac9a76623679d8f4e Mon Sep 17 00:00:00 2001 From: Marko Date: Tue, 11 Jun 2024 23:28:06 +0200 Subject: [PATCH 2/5] Update variations.11.cpp --- math/variations.11.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/math/variations.11.cpp b/math/variations.11.cpp index e85586b..7c8eed2 100644 --- a/math/variations.11.cpp +++ b/math/variations.11.cpp @@ -1,13 +1,12 @@ -// Binomials +// Variations with repetitions #include using namespace std; -using mint = int; /*snippet-begin*/ -template +template struct variations { struct iterator { - int l, h; + T l, h; vector a; const vector& operator* () const { return a; } @@ -28,7 +27,7 @@ struct variations { variations(int n, int k, T l = 0) : n(n), k(k), l(l) {} iterator begin() { - return {l, l+k-1, vector(n, l)}; + return {l, l+k-1, vector(n, l)}; } iterator end() { @@ -47,10 +46,10 @@ int main() { for (auto it = v.begin(); it != v.end(); ++it){ auto b = *it; for (int i = 0; i < a[ind].size(); ++i){ - if (b[i] != a[ind][i]) return 0; + if (b[i] != a[ind][i]) return 1; } ++ind; } - return (ind == a.size()); -} \ No newline at end of file + return (ind != a.size()); +} From c12a64aa7224f0fd66b0663f044f21d01fa369c7 Mon Sep 17 00:00:00 2001 From: Marko Date: Wed, 12 Jun 2024 00:05:20 +0200 Subject: [PATCH 3/5] Update variations.11.cpp --- math/variations.11.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/math/variations.11.cpp b/math/variations.11.cpp index 7c8eed2..e134de4 100644 --- a/math/variations.11.cpp +++ b/math/variations.11.cpp @@ -44,10 +44,7 @@ int main() { int ind = 0; vector> a = {{1,1},{1,2},{1,3},{2,1},{2,2},{2,3},{3,1},{3,2},{3,3}}; for (auto it = v.begin(); it != v.end(); ++it){ - auto b = *it; - for (int i = 0; i < a[ind].size(); ++i){ - if (b[i] != a[ind][i]) return 1; - } + if (*it != a[ind]) return 1; ++ind; } From de3419eacd7c8a7d81d32bdff4853df2bc62844e Mon Sep 17 00:00:00 2001 From: Marko Date: Wed, 12 Jun 2024 01:32:23 +0200 Subject: [PATCH 4/5] Update variations.11.cpp --- math/variations.11.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/math/variations.11.cpp b/math/variations.11.cpp index e134de4..cae750a 100644 --- a/math/variations.11.cpp +++ b/math/variations.11.cpp @@ -1,5 +1,6 @@ // Variations with repetitions #include +#include using namespace std; /*snippet-begin*/ From 22c30e216fb19b96bc3af7eca04799566166ae82 Mon Sep 17 00:00:00 2001 From: Marko Date: Wed, 12 Jun 2024 10:35:27 +0200 Subject: [PATCH 5/5] Update variations.11.cpp --- math/variations.11.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/math/variations.11.cpp b/math/variations.11.cpp index cae750a..84a78b5 100644 --- a/math/variations.11.cpp +++ b/math/variations.11.cpp @@ -40,7 +40,7 @@ struct variations { /*snippet-end*/ int main() { - variations v(2,3,1); + variations<> v(2,3,1); int ind = 0; vector> a = {{1,1},{1,2},{1,3},{2,1},{2,2},{2,3},{3,1},{3,2},{3,3}}; @@ -49,5 +49,5 @@ int main() { ++ind; } - return (ind != a.size()); + return (ind != (int)a.size()); }