fork download
  1. #include <iostream>
  2. #include <bitset>
  3.  
  4. using namespace std;
  5.  
  6. unsigned long long binomial(unsigned n, unsigned k)
  7. {
  8. if (k > n) return 0;
  9. unsigned result = 1;
  10. for (unsigned d=1; d <= k; d++) {
  11. result *= n--;
  12. result /= d;
  13. }
  14. return result;
  15. }
  16.  
  17. unsigned long long next_permutation(unsigned long long v)
  18. {
  19. unsigned long long t = (v | (v - 1)) + 1;
  20. return t | ((((t & -t) / (v & -v)) >> 1) - 1);
  21. }
  22.  
  23. int main()
  24. {
  25. const unsigned n = 5;
  26. const unsigned k = 3;
  27.  
  28. unsigned long long permutation = (1 << k) - 1;
  29.  
  30. for (unsigned int i = 0; i < binomial(n,k); ++i)
  31. {
  32. bitset<n> foo(permutation);
  33. cout << foo << '\n';
  34. permutation = next_permutation(permutation);
  35. }
  36. }
  37.  
Success #stdin #stdout 0s 3032KB
stdin
Standard input is empty
stdout
00111
01011
01101
01110
10011
10101
10110
11001
11010
11100