# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
341559 | rqi | Broken Device (JOI17_broken_device) | C++14 | 50 ms | 3204 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 "Annalib.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) begin(x), end(x)
static mt19937 rng((uint32_t)(13053));
static int N;
static vi perm;
static vi iperm;
static void genPerm(){
perm = vi(N, 0);
iperm = vi(N, 0);
for(int i = 0; i < N; i++){
perm[i] = i;
}
shuffle(all(perm), rng);
for(int i = 0; i < N; i++){
iperm[perm[i]] = i;
}
}
static vi offset;
static void genOffset(){
for(int i = 0; i < 38; i++){
offset.pb(rng() % 3);
}
}
static vi apply(vi a, vi dif){
vi na;
for(int i = 0; i < N; i++){
na.pb(a[dif[i]]);
}
return na;
}
static vi to_binary(ll a, int bitnum){
vi b;
for(int i = 0; i < bitnum; i++){
b.pb(a % 2);
a/=2;
}
return b;
}
static ll from_binary(vi b){
ll a = 0;
ll curt = 1;
for(auto u: b){
a+=curt*u;
curt*=2;
}
return a;
}
static vi to_ternary(ll a, int bitnum = 38){
vi b;
for(int i = 0; i < bitnum; i++){
b.pb(a % 3);
a/=3;
}
return b;
}
static ll from_ternary(vi b){
ll a = 0;
ll curt = 1;
for(auto u: b){
a+=curt*u;
curt*=3;
}
return a;
}
static vi to_sevary(ll a, int bitnum = 22){
vi b;
for(int i = 0; i < bitnum; i++){
b.pb(a % 7);
a/=7;
}
return b;
}
static ll from_sevary(vi b){
ll a = 0;
ll curt = 1;
for(auto u: b){
a+=curt*u;
curt*=7;
}
return a;
}
static vi res;
static vi bad;
void assignDoubBit(int pos, int val){
if(val == 0){
return; //0 double bit means 0
}
if(bad[pos] == 0){
res[pos] = 1;
}
else{
assert(bad[pos+1] == 0);
res[pos+1] = 1;
}
}
void Anna(int _N, ll _X, int K, int P[]){
N = _N;
//genPerm();
//genOffset();
res = vi(N, 0);
bad = vi(N, 0);
for(int i = 0; i < K; i++){
bad[P[i]] = 1;
}
vi X = to_ternary(_X);
assert(sz(X) == 38);
//bad = apply(bad, perm);
int fullbins = 0;
for(int i = 0; i < N; i+=2){
if(!bad[i] && !bad[i+1]) fullbins++;
}
if(fullbins >= 38){
int curbit = 0;
for(int i = 0; i < N; i+=2){
if(!bad[i] && !bad[i+1] && curbit < sz(X)){
//put the information in
if(X[curbit] == 0){
res[i] = 0;
res[i+1] = 1;
}
else if(X[curbit] == 1){
res[i] = 1;
res[i+1] = 0;
}
else if(X[curbit] == 2){
res[i] = 1;
res[i+1] = 1;
}
curbit++;
}
}
}
else{
X = to_sevary(_X); //22 bits
int curbit = 0;
for(int i = 0; i+6-1 < 150; i+=6){
if((bad[i] && bad[i+1]) || (bad[i+2] && bad[i+3]) || (bad[i+4] && bad[i+5]) || curbit >= sz(X)){
continue;
}
vi Y = to_binary(X[curbit]+1, 3);
for(int j = 0; j < 3; j++){
assignDoubBit(i+2*j, Y[j]);
}
curbit++;
}
//count number of nonzero double bits. If ==38, make last possible nonzero
int doubbits = 0;
for(int i = 0; i < 150; i+=2){
if(res[i] == 1 || res[i+1] == 1) doubbits++;
}
if(doubbits == 38){
for(int i = 148; i >= 0; i--){
if((!bad[i] || !bad[i+1]) && (res[i] == 0 && res[i+1] == 0)){
if(!bad[i]){
res[i] = 1;
}
if(!bad[i+1]){
res[i+1] = 1;
}
break;
}
}
}
}
//res = apply(res, iperm);
for(int i = 0; i < N; i++){
Set(i, res[i]);
//cout << res[i];
}
//cout << "\n";
}
#include "Brunolib.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) begin(x), end(x)
static mt19937 rng((uint32_t)(13053));
static int N;
static vi perm;
static vi iperm;
static void genPerm(){
perm = vi(N, 0);
iperm = vi(N, 0);
for(int i = 0; i < N; i++){
perm[i] = i;
}
shuffle(all(perm), rng);
for(int i = 0; i < N; i++){
iperm[perm[i]] = i;
}
}
static vi offset;
static void genOffset(){
for(int i = 0; i < 38; i++){
offset.pb(rng() % 3);
}
}
static vi apply(vi a, vi dif){
vi na;
for(int i = 0; i < N; i++){
na.pb(a[dif[i]]);
}
return na;
}
static vi to_binary(ll a, int bitnum){
vi b;
for(int i = 0; i < bitnum; i++){
b.pb(a % 2);
a/=2;
}
return b;
}
static ll from_binary(vi b){
ll a = 0;
ll curt = 1;
for(auto u: b){
a+=curt*u;
curt*=2;
}
return a;
}
static vi to_ternary(ll a, int bitnum = 38){
vi b;
for(int i = 0; i < bitnum; i++){
b.pb(a % 3);
a/=3;
}
return b;
}
static ll from_ternary(vi b){
ll a = 0;
ll curt = 1;
for(auto u: b){
a+=curt*u;
curt*=3;
}
return a;
}
static vi to_sevary(ll a, int bitnum = 22){
vi b;
for(int i = 0; i < bitnum; i++){
b.pb(a % 7);
a/=7;
}
return b;
}
static ll from_sevary(vi b){
ll a = 0;
ll curt = 1;
for(auto u: b){
a+=curt*u;
curt*=7;
}
return a;
}
ll Bruno(int _N, int A[]){
N = _N;
vi res;
for(int i = 0; i < N; i++){
res.pb(A[i]);
}
// genPerm();
// genOffset();
// res = apply(res, perm);
int doubbits = 0;
for(int i = 0; i < 150; i+=2){
if(res[i] == 1 || res[i+1] == 1) doubbits++;
}
ll ans;
if(doubbits == 38){
vi X;
for(int i = 0; i < N; i+=2){
if(res[i] == 0 && res[i+1] == 0) continue;
if(res[i] == 0 && res[i+1] == 1){
X.pb(0);
}
else if(res[i] == 1 && res[i+1] == 0){
X.pb(1);
}
else if(res[i] == 1 && res[i+1] == 1){
X.pb(2);
}
}
assert(sz(X) == 38);
ans = from_ternary(X);
}
else{
vi X;
for(int i = 0; i < 150; i+=6){
if(sz(X) >= 22) break;
vi Y;
for(int j = i; j < i+6; j+=2){
if(res[j] == 0 && res[j+1] == 0){
Y.pb(0);
}
else{
Y.pb(1);
}
}
int val = from_binary(Y);
assert(0 <= val && val <= 7);
if(val >= 1) X.pb(val-1);
}
assert(sz(X) == 22);
ans = from_sevary(X);
// cout << ans << "\n";
}
//cout << ans << "\n";
return ans;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |