ivan100sic / competelib-snippets

Snippets for competitive programming
The Unlicense
5 stars 2 forks source link

Add variations #12

Closed ivan100sic closed 2 months ago

ivan100sic commented 1 year ago
struct variations {
    struct iterator {
        int l, h;
        vector<int> a;

        const vector<int>& 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, l;
    variations(int n, int k, int l = 0) : n(n), k(k), l(l) {}

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

    iterator end() {
        vector<int> e(n, l);
        e[0] = l+k;
        return {l, l+k-1, move(e)};
    }
};
ivan100sic commented 2 months ago

Closed by #23