Cod sursa(job #2244523)

Utilizator cosmin.pascaruPascaru Cosmin cosmin.pascaru Data 22 septembrie 2018 23:03:51
Problema Combinari Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.46 kb
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <array>
#include <algorithm>
#include <vector>
#include <stack>
#include <set>
#include <assert.h>
#include <queue>
#include <chrono>
#include <memory>

using LL = long long;
using ULL = int long long;

const std::string _problemName = "combinari";

namespace std {
std::ifstream fin(_problemName + ".in");
std::ofstream fout(_problemName + ".out");
}

 #define USE_FILES

#ifdef USE_FILES
#define cin fin
#define cout fout
#endif

class Generator {

public:

    Generator(int n, int k) : n_(n), k_(k), current_(k), used_(n) {}

    void generate() {
        generateImpl(0);
    }

private:

    void output() {
        for (auto i : current_) {
            std::cout << i + 1 << ' ';
        }
        std::cout << '\n';
    }

    void generateImpl(int pos) {
        if (pos >= k_) {
            output();
            return;
        }

        int firstChoice = current_.empty() ? 0 : current_.back() + 1;
        
        for (int choice = firstChoice; choice < n_; ++choice) {
            if (used_[choice]) {
                continue;
            }

            current_[pos] = choice;
            used_[choice] = true;

            generateImpl(pos + 1);

            used_[choice] = false;
        }
    }


    int n_;
    int k_;

    std::vector<int> current_;
    std::vector<bool> used_;
};

int main() {

    int n, k;
    std::cin >> n >> k;

    Generator(n, k).generate();

    return 0;
}