Cod sursa(job #1811794)

Utilizator bciobanuBogdan Ciobanu bciobanu Data 21 noiembrie 2016 16:41:25
Problema Combinari Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <bits/stdc++.h>
#define SZ(x) ((int) (x).size())
using namespace std;

void BackTrack(const int& n, const int& k, vector<int> stack, ostream& out) {
    if (SZ(stack) == k + 1) {
        for (const int& x : stack) {
            if (x != 0) {
                out << x << ' ';
            }
        }
        out << '\n';
    } else {
        const int bound = n - k + SZ(stack) + 1;
        for (int i = stack.back() + 1; i <= bound; i++) {
            stack.emplace_back(i);
            BackTrack(n, k, stack, out);
            stack.pop_back();
        }
    }
}

int main() {
    #ifdef INFOARENA
    ifstream cin("combinari.in");
    ofstream cout("combinari.out");
    #endif
    cin.tie(0);
    ios_base::sync_with_stdio(false);

    int n, k; cin >> n >> k;
    vector<int> stack(1, 0);
    BackTrack(n, k, stack, cout);
}