#include <bits/stdc++.h>
#include "communication.h"
using namespace std;
// 0 -> 0000, 1 -> 0110, 2 -> 1001, 3 -> 1111
int e[4] = {0, 6, 9, 15};
pair<int, int> d[16] = {{0, 2},
{0, 2},
{0, 1},
{1, 2},
{0, 1},
{0, 3},
{1, 3},
{1, 3},
{0, 2},
{0, 2},
{0, 3},
{2, 3},
{1, 2},
{2, 3},
{1, 3},
{1, 3}};
int bit_reverse(int X)
{
int Y = 0;
for (unsigned i = 0; i < 30; i++)
Y |= ((X >> i) & 1) << (30 - i - 1);
return Y;
}
void encode(int N, int X) // Send exactly 120 bits to encode any message.
{
X = bit_reverse(X);
for (unsigned i = 0; i < 30; i++)
{
int Y = e[X & 3], Z = 0;
for (unsigned j = 0; j < 4; j++)
Z = (Z << 1) | send(Y & 1), Y >>= 1;
X >>= 2;
X = (X << 1) | (int)(d[Z].second == Y);
}
}
pair<int, int> get_interval(pair<int, int> p, pair<int, int> q, int i)
{
switch (i)
{
case 0:
return {p.first, (p.first + p.second) / 2};
case 1:
return {(p.first + p.second) / 2, p.second};
case 2:
return {q.first, (q.first + q.second) / 2};
case 3:
return {(q.first + q.second) / 2, q.second};
default:
assert(0);
}
}
std::pair<int, int> decode(int N)
{
pair<int, int> p = {0, 1 << 15}, q = {1 << 15, 1 << 30};
for (unsigned i = 0; i < 30; i++)
{
int Y = 0;
for (unsigned j = 0; j < 4; j++)
Y = (Y << 1) | receive();
pair<int, int> r = get_interval(p, q, d[Y].first);
q = get_interval(p, q, d[Y].second);
p = r;
}
return {p.first, q.first};
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
43 ms |
284 KB |
Not correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
444 ms |
200 KB |
Not correct |
2 |
Halted |
0 ms |
0 KB |
- |