#include <bits/stdc++.h>
using namespace std;
#define F0R(i, n) for (int i= 0; i < n;i++)
template<typename T>
using V = vector<T>;
using vi = V<int>;
int send(int bit);
int receive();
int iter = 0;
int send2(int bit) {
return send(bit) ^ iter;
}
int receive2() {
return receive() ^ iter;
}
constexpr int TRIES = 20;
void sendBit(int x) {
bool lied = false;
bool finished = false;
F0R(i, TRIES) {
if (finished) {
send(1);
continue;
}
if (lied) {
send(x);
finished = true;
continue;
}
if (send(1) == 0) lied = true;
}
if (!lied) send(x);
else send(1);
}
int recBit() {
bool next = false;
bool finished = false;
int ans = 0;
F0R(i, TRIES) {
int x = receive();
if (finished) continue;
if (next) {
finished = true;
ans = x;
continue;
}
if (x == 0) next = true;
}
int y = receive();
if (!finished) ans = y;
return ans;
}
void encode(int n, int x) {
srand(0);
iter = 0;
int bits = log2(n + 1) + 1;
F0R(i, bits) {
sendBit(x % 2);
x /= 2;
}
}
std::pair<int, int> decode(int n) {
srand(0);
int x = 0;
iter = 0;
int bits = log2(n + 1) + 1;
int mul = 1;
F0R(i, bits) {
x += mul * recBit();
mul *= 2;
}
return {x, n};
}
#if DEBUG
using namespace std;
void __attribute__((noreturn)) __attribute__((format(printf, 1, 2))) result(const char *msg, ...)
{
va_list args;
va_start(args, msg);
vfprintf(stdout, msg, args);
fprintf(stdout, "\n");
va_end(args);
exit(0);
}
namespace
{
enum { ENCODE, DECODE } current_phase;
int N, X;
vector<int> signals;
size_t cursor = 0;
bool flipped = false;
}
int send(int s)
{
if(current_phase == DECODE or (s != 0 and s != 1))
result("Invalid send.");
printf("send(%d) -> ", s); fflush(stdout);
int answer = flipped ? s : rand() % 2;
bool flipped_now = (s != answer);
if(flipped and flipped_now)
result("Invalid reply to send");
flipped = flipped_now;
signals.push_back(answer);
if(signals.size() > (size_t) 250)
result("Looks (and smells) fishy.");
return signals.back();
}
int receive()
{
if(current_phase == ENCODE) result("Invalid receive.");
if(cursor >= signals.size()) result("Assistant waiting for Godot.");
int r = signals[cursor++];
printf("receive() -> %d\n", r);
return r;
}
int main()
{
if(scanf("%d %d", &N, &X) != 2 or X < 1 or X > N)
result("Invalid input.");
current_phase = ENCODE;
encode(N, X);
current_phase = DECODE;
auto r = decode(N);
if(r.first < 1 or r.first > N or r.second < 1 or r.second > N)
result("Invalid answer.");
if(r.first == X or r.second == X)
result("Correct: %d signals sent.", (int) signals.size());
else
result("Wrong answer.");
}
#endif
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
3076 KB |
Output is correct |
2 |
Correct |
41 ms |
2724 KB |
Output is correct |
3 |
Incorrect |
37 ms |
332 KB |
Not correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Not correct |
2 |
Halted |
0 ms |
0 KB |
- |