#include "message.h"
#include <bits/stdc++.h>
using namespace std;
const int LIMIT = 66;
void send_message(std::vector<bool> M, std::vector<bool> C) {
  std::vector<bool> A(31, 0);
  int N = (int)M.size();
  vector<int> dist(31, -1);
  for (int i = 0; i < 31; ++i) {
    if (C[i] == 0) {
      int d = 1;
      for (int j = (i + 1) % 31; C[j] == 1; j = (j + 1) % 31) {
        d++;
      }
      dist[i] = d;
    }
  }
  // cout << "DIST:" << endl;
  // for (int i = 0; i < 31; ++i) {
  //   cout << dist[i] << " ";
  // }
  // cout << endl;
  vector<bool> send[31];
  int ptr = 0;
  for (int j = 0; j < LIMIT; ++j) {
    for (int i = 0; i < 31; ++i) {
      if (C[i] == 0) {
          if (dist[i] > 0) {
            send[i].push_back(1);
            dist[i]--;
            if (dist[i] == 0) send[i].push_back(0);
          } else if (ptr < N) {
            send[i].push_back(M[ptr++]);
          }
      }
    }
  }
  // for (int i = 0; i < 31; ++i) {
  //   if (C[i] == 0) {
  //     cout << i << " = ";
  //     for (auto u : send[i]) {
  //       cout << u << " ";
  //     }
  //     cout << endl;
  //   }
  // }
  // cout << "HERE!" << endl;
  for (int i = 0; i < 31; ++i) {
    // cout << i << endl;
    while ((int)send[i].size() < LIMIT) {
      send[i].insert(send[i].begin(), 0);
    }
    // cout << i << endl;
  }
  for (int i = 0; i < LIMIT; ++i) {
    for (int j = 0; j < 31; ++j) {
      A[j] = send[j][i];
    }
    send_packet(A);
  }
  // cout << "DONE SENDING..." << endl;
}
std::vector<bool> receive_message(std::vector<std::vector<bool>> R) {
  vector<bool> res;
  vector<int> nxt(31, -1);
  vector<bool> C[31];
  int N = (int)R.size();
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < 31; ++j) {
      // cout << R[i][j] << " ";
      C[j].push_back(R[i][j]);
    }
    // cout << endl;
  }
  // cout << endl;
  for (int i = 0; i < 31; ++i) {
    auto& v = C[i];
    // for (auto u : v) {
    //   cout << u << " ";
    // }
    // cout << endl;
    while (v.size() && v[0] == 0) {
      v.erase(v.begin());
    }
    if (!v.size()) continue;
    int dist = 0;
    while (v.size() && v[0] == 1) {
      dist++;
      v.erase(v.begin());
    }
    // cout << i << " = " << dist << "\n";
    if (v.size() == 0) continue;
    nxt[i] = (i + dist) % 31;
    v.erase(v.begin());
  }
  int start = -1;
  // for (int i = 0; i < 31; ++i) {
  //   cout << nxt[i] << " ";
  // }
  // cout << endl;
  for (int i = 0; start == -1 && i < 31; ++i) {
    if (nxt[i] == -1) continue;
    int j = i;
    int d = 0;
    while (d < 16 && j >= i) {
      d++;
      j = nxt[j];
      if (j == i) break;
    }
    if (d == 16 && j == i) {
      start = i;
    }
    // cout << i << " -> " << j << endl;
  }
  assert(start != -1);
  bool good[31];
  for (int i = 0; i < 31; ++i) good[i] = false;
  {
    int ni = start;
    good[ni] = true;
    ni = nxt[ni];
    while (ni != start) {
      good[ni] = true;
      ni = nxt[ni];
    }
  }
  while (true) {
    bool got = false;
    for (int i = 0; i < 31; ++i) {
      if (good[i]) {
        auto& v = C[i];
        if (v.size()) {
          res.push_back(v[0]);
          v.erase(v.begin());
          got = true;
        }
      }
    }
    if (!got) break;
  }
  return res;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |