#include "encoder.h"
#include "encoderlib.h"
#include <bitset>
#include <cassert>
#include <iostream>
#include <array>
using namespace std;
const int LEN = 514, LIM = 517;
static bitset<LEN> operator+ (const bitset<LEN> &lhs, const bitset<LEN> &rhs) {
bitset<LEN> res;
res.reset();
bool carry = 0;
for (int i = 0; i < LEN; i++) {
res[i] = carry ^ lhs[i] ^ rhs[i];
if ((carry && lhs[i]) || (carry && rhs[i]) || (lhs[i] && rhs[i])) {
carry = 1;
} else {
carry = 0;
}
}
assert(carry == 0);
return res;
}
static bool fullSubtractor(bool b1, bool b2, bool& borrow) {
bool diff;
if (borrow) {
diff = !(b1 ^ b2);
borrow = !b1 || (b1 && b2);
}
else {
diff = b1 ^ b2;
borrow = !b1 && b2;
}
return diff;
}
static bitset<LEN> operator- (const bitset<LEN> &lhs, const bitset<LEN> &rhs) {
bool borrow = 0;
bitset<LEN> ans;
ans.reset();
for (int i = 0; i < LEN; i++) {
ans[i] = fullSubtractor(lhs[i], rhs[i], borrow);
}
return ans;
}
bool lessThanOrEqualTo(const bitset<LEN> &lhs, const bitset<LEN> &rhs) {
auto tmp = lhs ^ rhs;
int pos = -1;
for (int i = LEN - 1; i >= 0; i--) {
if (tmp[i]) {
pos = i;
break;
}
}
if (pos == -1 || rhs[pos]) return 1;
else return 0;
}
static bitset<LEN> nck[LIM][LIM];
static void precompute_binomials() {
for (int i = 0; i < LIM; i++) {
for (int j = 0; j <= i; j++) {
if (i == 0 || j == 0) {
nck[i][j] = j == 0;
} else {
nck[i][j] = nck[i - 1][j] + nck[i - 1][j - 1];
}
}
}
}
static bool init;
void encode(int N, int M[]) {
int K = min(261, N * 5);
if (!init) {
precompute_binomials();
init = 1;
}
bitset<LEN> cur;
cur.reset();
for (int i = 0; i < N; i++) {
for (int j = 0; j < 8; j++) {
cur[i * 8 + j] = !!(M[i] & (1 << j));
}
}
cur = (cur + 1);
for (int i = 0, val = 0; i < K; i++) {
while (1) {
int len_left = K - 1 - i;
int vals_left = 256 - val;
if (lessThanOrEqualTo(nck[len_left + vals_left][vals_left], cur)) {
cur = cur - nck[len_left + vals_left][vals_left];
val++;
} else {
break;
}
}
send(val);
}
}
#include "decoder.h"
#include "decoderlib.h"
#include <bitset>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <array>
using namespace std;
int A[105];
const int LEN = 514, LIM = 517;
static bitset<LEN> operator+ (const bitset<LEN> &lhs, const bitset<LEN> &rhs) {
bitset<LEN> res;
res.reset();
bool carry = 0;
for (int i = 0; i < LEN; i++) {
res[i] = carry ^ lhs[i] ^ rhs[i];
if ((carry && lhs[i]) || (carry && rhs[i]) || (lhs[i] && rhs[i])) {
carry = 1;
} else {
carry = 0;
}
}
assert(carry == 0);
return res;
}
static bool fullSubtractor(bool b1, bool b2, bool& borrow) {
bool diff;
if (borrow) {
diff = !(b1 ^ b2);
borrow = !b1 || (b1 && b2);
}
else {
diff = b1 ^ b2;
borrow = !b1 && b2;
}
return diff;
}
static bitset<LEN> operator- (const bitset<LEN> &lhs, const bitset<LEN> &rhs) {
bool borrow = 0;
bitset<LEN> ans;
ans.reset();
for (int i = 0; i < LEN; i++) {
ans[i] = fullSubtractor(lhs[i], rhs[i], borrow);
}
return ans;
}
static bitset<LEN> nck[LIM][LIM];
static void precompute_binomials() {
for (int i = 0; i < LIM; i++) {
for (int j = 0; j <= i; j++) {
if (i == 0 || j == 0) {
nck[i][j] = j == 0;
} else {
nck[i][j] = nck[i - 1][j] + nck[i - 1][j - 1];
}
}
}
}
static bool init;
void decode(int N, int L, int X[]) {
if (!init) {
precompute_binomials();
init = 1;
}
sort(X, X + L);
bitset<LEN> num(0);
for (int i = 0; i < L; i++) {
int len_left = L - i - 1;
for (int val = i ? X[i - 1] : 0; val < X[i]; val++) {
int val_left = 256 - val;
num = num + nck[len_left + val_left][val_left];
}
}
num = num - 1;
for (int i = 0; i < N; i++) {
A[i] = 0;
for (int j = 0; j < 8; j++) {
if (num[i * 8 + j]) {
A[i] |= (1 << j);
}
}
output(A[i]);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
486 ms |
23404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
495 ms |
23668 KB |
Output is correct |
2 |
Correct |
498 ms |
23804 KB |
Output is correct |
3 |
Correct |
512 ms |
23776 KB |
Output is correct |
4 |
Correct |
514 ms |
23768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
233 ms |
12332 KB |
Error : Bad encoded integer |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
235 ms |
12096 KB |
Error : Bad encoded integer |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
227 ms |
12036 KB |
Error : Bad encoded integer |
2 |
Incorrect |
229 ms |
12024 KB |
Error : Bad encoded integer |
3 |
Incorrect |
226 ms |
11888 KB |
Error : Bad encoded integer |
4 |
Incorrect |
226 ms |
11980 KB |
Error : Bad encoded integer |
5 |
Incorrect |
227 ms |
12112 KB |
Error : Bad encoded integer |
6 |
Incorrect |
222 ms |
11848 KB |
Error : Bad encoded integer |
7 |
Incorrect |
220 ms |
11904 KB |
Error : Bad encoded integer |