# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
561742 | nghiass001 | Parrots (IOI11_parrots) | 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 "encoderlib.h"
#include "decoderlib.h"
#include <bits/stdc++.h>
#define FOR(i,l,r) for(int i=(l); i<=(r); ++i)
#define REP(i,l,r) for(int i=(l); i<(r); ++i)
#define FORD(i,r,l) for(int i=(r); i>=(l); --i)
#define REPD(i,r,l) for(int i=(r)-1; i>=(l); --i)
#define getbit(x, i) (((x) >> (i)) & 1)
#define MASK(i) (1 << (i))
using namespace std;
/// BigNum
const long long MOD = (long long)1e16;
typedef vector<long long> BigNum;
bool operator < (BigNum A, BigNum B) {
if (A.size() != B.size()) return A.size() < B.size();
REPD(i, (int)A.size(), 0) if (A[i] != B[i]) return A[i] < B[i];
return false;
}
bool operator <= (BigNum A, BigNum B) {
if (A.size() != B.size()) return A.size() < B.size();
REPD(i, (int)A.size(), 0) if (A[i] != B[i]) return A[i] < B[i];
return true;
}
BigNum operator + (BigNum A, BigNum B) {
BigNum C(max(A.size(), B.size()));
long long carry = 0;
REP(i, 0, C.size()) {
if (i < A.size()) carry += A[i];
if (i < B.size()) carry += B[i];
C[i] = carry % MOD;
carry /= MOD;
}
if (carry) C.push_back(carry);
return C;
}
BigNum operator + (BigNum A, int B) {
return A + BigNum{B};
}
BigNum operator * (BigNum A, int B) {
long long carry = 0;
for(long long &v : A) {
carry += v * B;
v = carry % MOD;
carry /= MOD;
}
if (carry) A.push_back(carry);
return A;
}
BigNum operator / (BigNum A, int B) {
long long carry = 0;
REPD(i, (int)A.size(), 0) {
carry = carry * MOD + A[i];
A[i] = carry / B;
carry %= B;
}
while (!A.empty() && A.back() == 0) A.pop_back();
return A;
}
BigNum C[1000][1000];
BigNum getC(int n, int k) {
if (C[n][k].size()) return C[n][k];
if (k == 0) return C[n][k] = {1};
if (k > n) return {};
return C[n][k] = getC(n - 1, k) + getC(n - 1, k - 1);
}
void encode(int n, int a[]) {
int c[n * 8], code[n * 5];
int num = 0;
REP(i, 0, n) {
REP(j, 0, 8) {
c[i * 8 + j] = getbit(a[i], j);
}
}
int sz = n * 5;
BigNum order = {1};
REP(i, 0, n*8)
order = order * 2 + c[i];
auto get = [&](int n, int k) -> BigNum {/// x1 + x2 + ... + xk = n -> C(n + k - 1, k - 1)
return getC(n + k - 1, k - 1);
};
int prv = 0;
BigNum sum = {0};
REP(i, 0, sz) {
while (sum + get(sz - i - 1, 256 - prv) <= order) {
sum = sum + get(sz - i - 1, 256 - prv);
++prv;
}
code[i] = prv;
}
REP(i, 0, sz) send(code[i]);
}
void decode(int n, int sz, int b[]) {
sort(b, b + sz);
vector<int> a(n, 0);
auto get = [&](int n, int k) -> BigNum {/// x1 + x2 + ... + xk = n -> C(n + k - 1, k - 1)
return getC(n + k - 1, k - 1);
};
BigNum order = {0};
int prv = 0;
REP(i, 0, sz) {
while (prv < b[i]) {
order = order + get(sz - i - 1, 256 - prv);
++prv;
}
}
REPD(i, n*8, 0) {
if (order.size() && order[0] % 2 == 1) a[i / 8] += MASK(i % 8);
order = order / 2;
}
REP(i, 0, n) output(a[i]);
}