C++17 · program complet
#include <bits/stdc++.h>
using namespace std;
// a contine k valori strict crescatoare din 1..n.
bool urmatoareaCombinare(vector<int>& a, int n) {
int k = (int)a.size();
int i = k - 1;
while (i >= 0 && a[i] == n - k + i + 1) --i;
if (i < 0) return false;
++a[i];
for (int j = i + 1; j < k; ++j)
a[j] = a[j - 1] + 1;
return true;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k; // 1 <= k <= n
vector<int> a(k);
iota(a.begin(), a.end(), 1); // 1, 2, ..., k
do {
for (int i = 0; i < k; ++i)
cout << a[i] << (i + 1 == k ? '\n' : ' ');
} while (urmatoareaCombinare(a, n));
}