#include"communication.h"
#include <bits/stdc++.h>
//
// --- Sample implementation for the task communication ---
//
// To compile this program with the sample grader, place:
// communication.h communication_sample.cpp sample_grader.cpp
// in a single folder, then open the terminal in this directory (right-click onto an empty spot in the directory,
// left click on "Open in terminal") and enter e.g.:
// g++ -std=c++17 communication_sample.cpp sample_grader.cpp
// in this folder. This will create a file a.out in the current directory which you can execute from the terminal
// as ./a.out
// See task statement or sample_grader.cpp for the input specification
using namespace std;
const int LOG = 30;
int f(int x) {
return 31 - __builtin_clz(x);
}
void encode(int N, int X) {
int ans1 = 0, ans2 = 1;
for (int i = 1; i < LOG; i++) {
if (ans2 < ans1) swap(ans1, ans2);
if (ans1 == ans2) {
ans2 ^= (1 << i);
continue;
}
int x = f(ans1 ^ ans2);
int a = send((X >> x) & 1), b = send((X >> i) & 1), c = send((X >> i) & 1), d = send((X >> x) & 1);
if (b == c) {
ans1 ^= (b << i);
ans2 ^= (b << i);
continue;
}
if (a == d) {
if (d) ans1 = ans2;
else ans2 = ans1;
ans2 ^= (1 << i);
continue;
}
int tans1 = ((d ? ans2 : ans1) ^ (b << i)); // 0101
int tans2 = ((a ? ans2 : ans1) ^ (c << i)); // 1010
ans1 = tans1, ans2 = tans2;
}
}
pair<int, int> decode(int N) {
int ans1 = 0, ans2 = 1;
for (int i = 1; i < LOG; i++) {
if (ans2 < ans1) swap(ans1, ans2);
if (ans1 == ans2) {
ans2 ^= (1 << i);
continue;
}
int x = f(ans1 ^ ans2);
int a = receive(), b = receive(), c = receive(), d = receive();
if (b == c) {
ans1 ^= (b << i);
ans2 ^= (b << i);
continue;
}
if (a == d) {
if (d) ans1 = ans2;
else ans2 = ans1;
ans2 ^= (1 << i);
continue;
}
int tans1 = ((d ? ans2 : ans1) ^ (b << i)); // 0101
int tans2 = ((a ? ans2 : ans1) ^ (c << i)); // 1010
ans1 = tans1, ans2 = tans2;
}
if (ans1 <= 0 || ans1 > N) ans1 = 1;
if (ans2 <= 0 || ans2 > N) ans2 = 1;
return {ans1, ans2};
}
Compilation message
communication.cpp: In function 'std::pair<int, int> decode(int)':
communication.cpp:64:7: warning: unused variable 'x' [-Wunused-variable]
64 | int x = f(ans1 ^ ans2);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
1716 KB |
Output is correct |
2 |
Correct |
112 ms |
1680 KB |
Output is correct |
3 |
Correct |
126 ms |
1820 KB |
Output is correct |
4 |
Correct |
112 ms |
1664 KB |
Output is correct |
5 |
Correct |
123 ms |
1684 KB |
Output is correct |
6 |
Correct |
324 ms |
1860 KB |
Output is correct |
7 |
Correct |
733 ms |
1700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
684 ms |
1696 KB |
Output is partially correct |
2 |
Partially correct |
377 ms |
1668 KB |
Output is partially correct |
3 |
Partially correct |
419 ms |
1772 KB |
Output is partially correct |
4 |
Partially correct |
917 ms |
1700 KB |
Output is partially correct |
5 |
Partially correct |
693 ms |
1764 KB |
Output is partially correct |
6 |
Partially correct |
677 ms |
1756 KB |
Output is partially correct |
7 |
Partially correct |
2108 ms |
1876 KB |
Output is partially correct |
8 |
Partially correct |
3502 ms |
1956 KB |
Output is partially correct |
9 |
Partially correct |
3153 ms |
1964 KB |
Output is partially correct |
10 |
Partially correct |
2840 ms |
2008 KB |
Output is partially correct |
11 |
Partially correct |
2925 ms |
1888 KB |
Output is partially correct |
12 |
Partially correct |
2981 ms |
1888 KB |
Output is partially correct |
13 |
Partially correct |
2715 ms |
1984 KB |
Output is partially correct |
14 |
Partially correct |
2802 ms |
1820 KB |
Output is partially correct |
15 |
Partially correct |
1565 ms |
1728 KB |
Output is partially correct |
16 |
Partially correct |
3155 ms |
1736 KB |
Output is partially correct |
17 |
Partially correct |
733 ms |
1808 KB |
Output is partially correct |
18 |
Partially correct |
878 ms |
1756 KB |
Output is partially correct |
19 |
Partially correct |
820 ms |
1892 KB |
Output is partially correct |
20 |
Partially correct |
773 ms |
1828 KB |
Output is partially correct |
21 |
Partially correct |
774 ms |
1816 KB |
Output is partially correct |
22 |
Partially correct |
876 ms |
1920 KB |
Output is partially correct |
23 |
Partially correct |
1202 ms |
1852 KB |
Output is partially correct |
24 |
Correct |
95 ms |
1668 KB |
Output is correct |
25 |
Correct |
159 ms |
1800 KB |
Output is correct |
26 |
Correct |
195 ms |
1700 KB |
Output is correct |
27 |
Correct |
64 ms |
1684 KB |
Output is correct |
28 |
Correct |
144 ms |
1768 KB |
Output is correct |
29 |
Correct |
355 ms |
1828 KB |
Output is correct |
30 |
Correct |
746 ms |
1792 KB |
Output is correct |