#include <bits/stdc++.h>
using namespace std;
int len(int x) {
int sz = 0;
while (x > 0) {
x /= 10;
sz++;
}
return sz;
}
int find_digit(int a, int d) {
string s = "";
while (a > 0) {
s += ((a % 10) + '0');
a /= 10;
}
reverse(s.begin(), s.end());
for (int i = 0; i < 4; i++) s += '0';
return s[d - 1] - '0';
}
string convert(int a) {
string tr = "";
while (a > 0) {
tr += ((a % 3) + '0');
a /= 3;
}
while (tr.size() < 8) tr += '0';
reverse(tr.begin(), tr.end());
return tr;
}
vector<vector<int>> devise_strategy(int n) {
vector<vector<int>> s(28, vector<int>(n+1, -1));
s[0][0] = 0;
// the first prisoner will write the amount of digits A has
// the second prisoner will compare this with the amount of digits B has
// if they have same no. digits:
/*
the current prisoner will write the current digit of A on the board (form: pd, where p is the position of the digit and d is its value)
the next prisoner will compare with the respective digit of B
if they are equal, he will write which digit of A must be inspected next (added + 4) (so it doesnt conflict with the number written by the first pr.)
max X: 49
*/
/*
Idea:
We can convert the number to base 3 and compare them
the prisoners will write on the board in the form vd, meaning that digit d has value of v
even digits -> it corresponds to A
odd digits -> it corresponds to B
*/
for (int i = 1; i <= n; i++) {
string t = convert(i); // read A and write down the first digit of its trinary rep
s[0][i] = (t[1] - '0') * 10 + 1;
}
for (int read = 1; read <= 27; read++) {
int val = read / 10;
int idx = read % 10; // which bit needs to be compared with val
if (idx >= 8) continue;
if (idx % 2 == 0) { // this corresponds to A
s[read][0] = 1; // read From B
for (int poss = 1; poss <= n; poss++) {
string t = convert(poss);
int k = t[idx] - '0';
if (k < val) {
s[read][poss] = -2;
}
else if (k > val) {
s[read][poss] = -1;
}
else {
if (idx + 1 <= 7) s[read][poss] = (t[idx + 1] - '0') * 10 + idx + 1;
}
}
}
else { // this corresponds to B
s[read][0] = 0;
for (int poss = 1; poss <= n; poss++) {
string t = convert(poss);
int k = t[idx] - '0';
if (k > val) {
s[read][poss] = -2;
}
else if (k < val) {
s[read][poss] = -1;
}
else {
if (idx + 1 <= 7) s[read][poss] = (t[idx + 1] - '0') * 10 + idx + 1;
}
}
}
}
return s;
}
/*
int main() {
int n; cin >> n;
cout << convert(n) << "\n";
vector<vector<int>> x = devise_strategy(10);
for (auto a: x) {
for (auto b: a) {
cout << b << " ";
}
cout << "\n";
}
return 0;
}*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |