Josif_tepe

Untitled

Jun 29th, 2026
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <map>
  6. using namespace std;
  7. typedef long long ll;
  8. int n, k;
  9.  
  10. int letter[16][6], cnt[16], D;
  11. int pos_bitmask[6][26];
  12. int row_use[16];
  13.  
  14. ll res = 0;
  15. void rec(int x, const int * col_mask) {
  16.     if(x == n) {
  17.         vector<int> used(16);
  18.         for(int i = 0; i < D; i++) {
  19.             used[i] = row_use[i];
  20.         }
  21.         for(int j = 0; j < n; j++) {
  22.             int idx = __builtin_ctz(col_mask[j]);
  23.             used[idx]++;
  24.             if(used[idx] > cnt[idx]) {
  25.                 return;
  26.             }
  27.            
  28.         }
  29.         res++;
  30.         return;
  31.     }
  32.    
  33.     for(int r = 0; r < D; r++) {
  34.         if(row_use[r] + 1 > cnt[r]) continue;
  35.         int new_col_mask[6];
  36.         bool ok = true;
  37.         for(int j = 0; j < n; j++) {
  38.             int new_mask = col_mask[j] & pos_bitmask[x][letter[r][j]];
  39.             if(new_mask == 0) {
  40.                 ok = false;
  41.                 break;
  42.             }
  43.             new_col_mask[j] = new_mask;
  44.         }
  45.         if(!ok) continue;
  46.        
  47.         row_use[r]++;
  48.         rec(x + 1, new_col_mask);
  49.         row_use[r]--;
  50.     }
  51.    
  52. }
  53. int main() {
  54.     ios_base::sync_with_stdio(false);
  55.     cin >> n >> k;
  56.     D = 0;
  57.     map<string, int> m;
  58.     for(int i = 0; i < k; i++) {
  59.         string s;
  60.         cin >> s;
  61.         m[s]++;
  62.     }
  63.    
  64.     for(pair<string, int> p: m) {
  65.         cnt[D] = p.second;
  66.        
  67.         for(int i = 0; i < n; i++) {
  68.             letter[D][i] = p.first[i] - 'A';
  69.         }
  70.         D++;
  71.     }
  72.     memset(pos_bitmask, 0, sizeof pos_bitmask);
  73.    
  74.     for(int i = 0; i < D; i++){
  75.         for(int j = 0; j < n; j++) {
  76.             pos_bitmask[j][letter[i][j]] |= (1 << i);
  77.         }
  78.     }
  79.    
  80.     int mask[6];
  81.     int full_bitmask = (1 << D) - 1;
  82.     for(int j = 0; j < n; j++) {
  83.         mask[j] = full_bitmask;
  84.     }
  85.     for(int i = 0; i < D; i++) {
  86.         row_use[i] = 0;
  87.     }
  88.    
  89.     rec(0, mask);
  90.     cout << res << endl;
  91.     return 0;
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment