Advertisement
BaoJIaoPisu

Untitled

Nov 11th, 2022
1,140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.49 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. using ll = long long;
  6. using ld = long double;
  7. using ull = unsigned long long;
  8.  
  9. using pii = pair<int, int>;
  10. using pll = pair<ll, ll>;
  11. using pld = pair<ld, ld>;
  12.  
  13. #define fi first
  14. #define se second
  15. #define left BAO
  16. #define right ANH
  17. #define pb push_back
  18. #define pf push_front
  19. #define mp make_pair
  20. #define ins insert
  21. #define btpc __builtin_popcount
  22. #define btclz __builtin_clz
  23.  
  24. #define sz(x) (int)(x.size());
  25. #define all(x) x.begin(), x.end()
  26. #define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
  27.  
  28. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  29.  
  30. int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
  31. int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
  32. int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
  33.  
  34. template<class X, class Y>
  35.     bool minimize(X &x, const Y &y) {
  36.         if (x > y)
  37.         {
  38.             x = y;
  39.             return true;
  40.         }
  41.         return false;
  42.     }
  43. template<class X, class Y>
  44.     bool maximize(X &x, const Y &y) {
  45.         if (x < y)
  46.         {
  47.             x = y;
  48.             return true;
  49.         }
  50.         return false;
  51.     }
  52.  
  53. const int MOD = 1e9 + 7; //998244353
  54.  
  55. template<class X, class Y>
  56.     void add(X &x, const Y &y) {
  57.         x = (x + y);
  58.         if(x >= MOD) x -= MOD;
  59.     }
  60.  
  61. template<class X, class Y>
  62.     void sub(X &x, const Y &y) {
  63.         x = (x - y);
  64.         if(x < 0) x += MOD;
  65.     }
  66.  
  67. /* Author : Le Ngoc Bao Anh, 12A5, LQD High School for Gifted Student*/
  68.  
  69. const ll INF = 1e9;
  70. const int N = (1 << 20) + 10;
  71.  
  72. vector<int> path;
  73. vector<int> adj[N];
  74. bool visited[N], ok[N], used[N];
  75. int deg[N];
  76. int n, k;
  77.  
  78. void dfs(int u) {
  79.     visited[u] = true;
  80.     path.pb(u);
  81.     int nxt = 0, p = INF;
  82.     for(int i = 0; i < n; i++) {
  83.         int v = u ^ (1 << i);
  84.         if(ok[v]) --deg[v];
  85.     }
  86.  
  87.     for(int i = 0; i < n; i++) {
  88.         int v = u ^ (1 << i);
  89.         if(ok[v] && !visited[v]) {
  90.             if(!nxt) {
  91.                 nxt = v;
  92.                 p = deg[v];
  93.             }
  94.             else {
  95.                 int c = deg[v];
  96.                 if(c) {
  97.                     if(!p || minimize(p, c)) {
  98.                         p = c;
  99.                         nxt = v;
  100.                     }
  101.                 }
  102.             }
  103.         }
  104.     }
  105.        
  106.     if(nxt) dfs(nxt);
  107. }
  108.  
  109. void BaoJiaoPisu() {
  110.     cin >> n >> k;
  111.     vector<int> node;
  112.     for(int msk = 0; msk < (1 << n); msk++) {
  113.         int cnt = btpc(msk);
  114.         if(cnt == k || (cnt == k + 1)) node.pb(msk), ok[msk] = true;
  115.     }
  116.  
  117.     for(auto u : node) {
  118.         for(int i = 0; i < n; i++) {
  119.             int v = u ^ (1 << i);
  120.             if(ok[v]) ++deg[u];
  121.         }
  122.     }
  123.  
  124.     vector<int> ans;
  125.     int cnt = 0;
  126.     while(clock() <= 300ll * CLOCKS_PER_SEC) {
  127.         if(cnt == node.size()) break;
  128.         int u = rng() % (node.size() - cnt) + 1; ++cnt;
  129.         int root = 0;
  130.  
  131.         for(int i = 0; i < node.size(); i++) {
  132.             if(used[i]) continue; u--;
  133.             if(!u) {
  134.                 root = node[i];
  135.                 used[i] = true;
  136.                 break;
  137.             }
  138.         }
  139.  
  140.         path.clear();
  141.         dfs(root);
  142.  
  143.         if(path.size() > ans.size()) ans = path;
  144.         for(auto u : path) {
  145.             visited[u] = false;
  146.             for(int i = 0; i < n; i++) {
  147.                 int v = u ^ (1 << i);
  148.                 if(ok[v]) ++deg[v];
  149.             }
  150.         }
  151.     }
  152.  
  153.     cout << ans.size() << '\n';
  154.     for(auto u : ans) {
  155.         for(int i = 0; i < n; i++) {
  156.             cout << (u >> i & 1);
  157.         }
  158.         cout << '\n';
  159.     }
  160. }
  161.  
  162. int main()
  163. {
  164.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  165.     #ifndef ONLINE_JUDGE
  166.     freopen("input.txt", "r", stdin);
  167.     freopen("output.txt", "w", stdout);
  168.     #else
  169.     //online
  170.     #endif
  171.  
  172.     int tc = 1, ddd = 0;
  173.     // cin >> tc;
  174.     while(tc--) {
  175.         //ddd++;
  176.         //cout << "Case #" << ddd << ": ";
  177.         BaoJiaoPisu();
  178.     }
  179. }
  180.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement