Pagini recente » Cod sursa (job #893409) | Cod sursa (job #3030628) | Cod sursa (job #912530) | Cod sursa (job #1892453) | Cod sursa (job #1756041)
#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;
}