Cod sursa(job #1756041)

Utilizator Luncasu_VictorVictor Luncasu Luncasu_Victor Data 11 septembrie 2016 18:16:56
Problema Combinari Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.82 kb
#include <algorithm>
#include <string>
#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <queue>
using namespace std;

#ifdef INFOARENA
#define TEST 0
#else
#define TEST 1
#endif

class Combinations {
 public:
  Combinations(int n, int k) : n_(n), k_(k) {
    config_ = vector<int>(k_, 0);
    used_digit_ = vector<bool>(n_ + 1, false);
    first_comb_ = true;
    end_combination_ = false;
  }
  int CountCombs() {
    int counter = 1;

  }
  void Generate(int pos) {
    if (pos < 0) {
      end_combination_ = true;
      return;
    }
    for (int i = config_[pos] + 1; i <= (n_ - k_ + pos + 1); ++i) {
      if (!used_digit_[i]) {
        used_digit_[config_[pos]] = 0;
        config_[pos] = i;
        used_digit_[i] = true;
        return;
      }
    }
    used_digit_[config_[pos]] = false;
    Generate(pos - 1);
    config_[pos] = config_[pos - 1] + 1;
    used_digit_[config_[pos]] = true;
  }
  vector<int> NextCombination() {
    if (!end_combination_) {
      if (first_comb_) {
        for (int i = 1; i <= k_; ++i) {
          config_[i - 1] = i;
          used_digit_[i] = true;
        }
        first_comb_ = false;
      } else {
        Generate(k_ - 1);
        if (end_combination_) {
          return {};
        }
      }
      return config_;
    }
    return {};
  }
 private:
  int n_, k_;
  bool first_comb_;
  bool end_combination_;
  vector<int> config_;
  vector<bool> used_digit_;
};

int main() {
  freopen("combinari.in","r",stdin);
  freopen("combinari.out","w",stdout);
  int n, k;
  scanf("%d %d", &n, &k);
  Combinations *comb = new Combinations(n, k);
  vector<int> next_comb = comb->NextCombination();
  while (!next_comb.empty()) {
    for (int num : next_comb) {
      printf("%d ", num);
    }
    printf("\n");
    next_comb = comb->NextCombination();
  }

  return 0;
}