Advertisement
STANAANDREY

combi tpa

Apr 14th, 2023 (edited)
868
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <assert.h>
  5. #define NMAX 64
  6.  
  7. void mkAndPrintComb(int n, int k, const int arr[]) {
  8.   assert(n <= 64 && k <= n);
  9.   int cnt = 0;
  10.   for (uint64_t state = 0; state < (1 << n); state++) {
  11.     if (__builtin_popcount(state) != k) {
  12.       continue;
  13.     }
  14.     printf("#%d: ", ++cnt);
  15.     for (int i = 0; i < 64; i++) {
  16.       if (1 & (state >> i)) {
  17.         printf("%d ", arr[i]);
  18.       }
  19.     }
  20.     putchar('\n');
  21.   }
  22. }
  23.  
  24. int main(void) {
  25.   int n, k;
  26.   scanf("%d%d", &n, &k);
  27.   static int arr[NMAX];
  28.   for (int i = 0; i < n; i++) {
  29.     scanf("%d", arr + i);
  30.   }
  31.   mkAndPrintComb(n, k, arr);
  32.   return 0;
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement