Cod sursa(job #1811785)

Utilizator bciobanuBogdan Ciobanu bciobanu Data 21 noiembrie 2016 16:35:09
Problema Combinari Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.95 kb
#include <bits/stdc++.h>
#define SZ(x) ((int) (x).size())
using namespace std;

int GetNextCombination(const int &x) {
    const int y = (x | (x - 1)) + 1;
    return y | ((((y & -y) / (x & -x)) >> 1) - 1);
}

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> logarithm(1 << n);
    for (int i = 2; i < (1 << n); i++) {
        logarithm[i] = logarithm[i / 2] + 1;
    }
    vector<string> all;
    for (int mask = (1 << k) - 1; mask < (1 << n); mask = GetNextCombination(mask)) {
        string current = "";
        for (int aux = mask; aux; aux &= aux - 1) {
            current = current + to_string(1 + logarithm[aux & -aux]) + " ";
        }
        all.emplace_back(current);
    }
    sort(begin(all), end(all));
    for (const string& m_string : all) {
        cout << m_string << '\n';
    }
}