Cod sursa(job #1811798)

Utilizator bciobanuBogdan Ciobanu bciobanu Data 21 noiembrie 2016 16:43:55
Problema Combinari Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 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) {
        for (const int& x : stack) {
            out << x << ' ';
        }
        out << '\n';
    } else {
        const int bound = n - k + SZ(stack) + 1;
        for (int i = (stack.empty() ? 1 : 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;
    BackTrack(n, k, stack, cout);
}