# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
824248 | esomer | Prisoner Challenge (IOI22_prison) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> ans(23);
vector<vector<int>> inds(8);
int val(int n, int exp){
vector<ll> pw(9);
pw[0] = 1;
for(int i = 1; i <= 8; i++) pw[i] = pw[i-1] * 3;
for(int i = 8; i >= 0; i--){
int which;
if(n >= pw[i] * 2) {n -= pw[i] * 2; which = 2;}
else if(n >= pw[i]) {n -= pw[i]; which = 1;}
else{
which = 0;
}
if(i == exp) return which;
}
return 0;
}
void build(int ind, int exp, int should){
if(exp == 0){
for(int i = 1; i <= N; i++){
int which = val(i, exp);
if(which != should){
if(which > should) ans[ind][i] = -(ans[ind][0] + 1);
else ans[ind][i] = -((1 - ans[ind][0]) + 1);
}
}
return;
}
if(exp == 1){
//Optimize 2 queries.
for(int i : inds[exp - 1]){
ans[i][0] = 1 - ans[ind][0];
}
for(int i = 1; i <= N; i++){
int which = val(i, exp);
if(which != should){
if(which > should) ans[ind][i] = -(ans[ind][0] + 1);
else ans[ind][i] = -((1 - ans[ind][0]) + 1);
}else{
which = val(i, exp - 1);
if(which == 2){
ans[ind][i] = -(ans[ind][0] + 1);
}else if(which == 0){
ans[ind][i] = -((1 - ans[ind][0]) + 1);
}else{
ans[ind][i] = inds[exp-1][0];
}
}
}
build(inds[exp - 1][0], exp - 1, 1);
return;
}
for(int i : inds[exp - 1]){
ans[i][0] = 1 - ans[ind][0];
}
for(int i = 1; i <= N; i++){
int which = val(i, exp);
if(which != should){
if(which > should) ans[ind][i] = -(ans[ind][0] + 1);
else ans[ind][i] = -((1 - ans[ind][0]) + 1);
}else{
which = val(i, exp - 1);
ans[ind][i] = inds[exp-1][which];
}
}
build(inds[exp - 1][0], exp - 1, 0);
build(inds[exp - 1][1], exp - 1, 1);
build(inds[exp - 1][2], exp - 1, 2);
}
vector<vector<int>> devise_strategy(int N) {
ans[0][0] = 0;
int curr = 1;
for(int i = 7; i >= 1; i--){
inds[i].push_back(curr); curr++;
inds[i].push_back(curr); curr++;
inds[i].push_back(curr); curr++;
}
inds[0].push_back(curr);
build(0, 8, 0);
return ans;
}