This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#ifndef LOCAL
#include "vision.h"
#else
#include <vector>
void construct_network(int H, int W, int K);
int add_and(std::vector<int> Ns);
int add_or(std::vector<int> Ns);
int add_xor(std::vector<int> Ns);
int add_not(int N);
#endif
#include <bits/stdc++.h>
#ifdef LOCAL
#include "template\debug.hpp"
#else
#define dbg(...) ;
#define timer(...) ;
#endif
constexpr int BITS = 9;
using Number = std::array<int, BITS>;
int zero_index, one_index;
Number get_number(int x) {
Number res;
for (int i = 0; i < BITS; i++) {
res[i] = (x >> i & 1) ? one_index : zero_index;
}
return res;
}
Number add(Number a, Number b) {
int carry_bit = zero_index;
Number c;
for (int i = 0; i < BITS; i++) {
int pxor = add_xor({a[i], b[i]});
c[i] = add_xor({pxor, carry_bit});
int pand = add_and({a[i], b[i]});
int cand = add_and({pxor, carry_bit});
carry_bit = add_or({pand, cand});
}
return c;
}
Number negate(Number a) {
Number res;
for (int i = 0; i < BITS; i++) {
res[i] = add_not(a[i]);
}
return add(res, get_number(1));
}
Number sub(Number a, Number b) {
return add(a, negate(b));
}
Number calculate(std::vector<int> cells) {
int n = cells.size();
std::vector<int> not_cells(n);
for (int i = 0; i < n; i++) {
not_cells[i] = add_not(cells[i]);
}
std::vector<int> multiplier(n);
multiplier[0] = one_index;
for (int i = 0; i + 1 < n; i++) {
multiplier[i + 1] = add_and({multiplier[i], not_cells[i]});
}
Number l, r;
for (int b = 0; b < BITS; b++) {
std::vector<int> res = {zero_index};
for (int i = 0; i < n; i++) {
if (i >> b & 1) {
res.push_back(add_and({cells[i], multiplier[i]}));
}
}
l[b] = add_or(res);
}
multiplier[n - 1] = one_index;
for (int i = n - 1; i > 0; i--) {
multiplier[i - 1] = add_and({multiplier[i], not_cells[i]});
}
for (int b = 0; b < BITS; b++) {
std::vector<int> res = {zero_index};
for (int i = 0; i < n; i++) {
if (i >> b & 1) {
res.push_back(add_and({cells[i], multiplier[i]}));
}
}
r[b] = add_or(res);
}
return sub(r, l);
}
void compare(Number a, Number b) {
std::vector<int> nice;
for (int i = 0; i < BITS; i++) nice.push_back(add_xor({a[i], b[i]}));
add_not(add_or(nice));
}
void construct_network(int H, int W, int K) {
auto get_id = [&](int i, int j) -> int {
return i * W + j;
};
zero_index = add_and({add_not(0), 0});
one_index = add_not(zero_index);
std::vector<int> rows(H), cols(W);
for (int i = 0; i < H; i++) {
std::vector<int> inputs(W);
for (int j = 0; j < W; j++) {
inputs[j] = get_id(i, j);
}
rows[i] = add_or(inputs);
}
for (int i = 0; i < W; i++) {
std::vector<int> inputs(H);
for (int j = 0; j < H; j++) {
inputs[j] = get_id(j, i);
}
cols[i] = add_or(inputs);
}
dbg(rows);
dbg(cols);
auto f1 = calculate(rows);
auto f2 = calculate(cols);
auto d = add(f1, f2);
compare(d, get_number(K));
}
#ifdef LOCAL
#include <cstdio>
#include <cassert>
#include <string>
using namespace std;
static const int MAX_INSTRUCTIONS = 10000;
static const int MAX_INPUTS = 1000000;
static const int _AND = 0;
static const int _OR = 1;
static const int _XOR = 2;
static const int _NOT = 3;
// 10000 instructions
static inline bool increasing(int a, int b, int c) {
return a <= b && b <= c;
}
[[noreturn]] static inline void error(string message) {
printf("%s\n", message.c_str());
exit(0);
}
class InstructionNetwork {
struct Instruction {
int type;
vector<int> input_indexes;
inline Instruction(int _type, const vector<int>& _input_indexes):
type(_type), input_indexes(_input_indexes) {
}
inline int apply(int a, int b) const {
switch (type) {
case _AND:
return a & b;
case _OR:
return a | b;
case _XOR:
return a ^ b;
default:
return 0;
}
}
inline int compute(const vector<int>& memory_cells) const {
int r = memory_cells[input_indexes[0]];
if (type == _NOT)
return 1 - r;
for (int j = 1; j < (int)input_indexes.size(); j++)
r = apply(r, memory_cells[input_indexes[j]]);
return r;
}
};
int input_size;
int total_inputs;
vector<Instruction> instructions;
public:
inline void init(int _input_size) {
this->input_size = _input_size;
this->total_inputs = 0;
this->instructions.clear();
}
inline int add_instruction(int type, const vector<int>& input_indexes) {
if (input_indexes.size() == 0)
error("Instruction with no inputs");
if (instructions.size() + 1 > MAX_INSTRUCTIONS)
error("Too many instructions");
if (total_inputs + input_indexes.size() > MAX_INPUTS)
error("Too many inputs");
instructions.emplace_back(type, input_indexes);
total_inputs += input_indexes.size();
int new_index = input_size + (int)instructions.size() - 1;
for (int input_index : input_indexes)
if (!increasing(0, input_index, new_index-1))
error("Invalid index");
return new_index;
}
inline int compute(vector<int> &memory_cells) const {
for (auto &instruction : instructions)
memory_cells.push_back(instruction.compute(memory_cells));
return memory_cells.back();
}
};
static InstructionNetwork instructionNetwork;
int main() {
int H, W, K;
assert(3 == scanf("%d%d%d", &H, &W, &K));
FILE *log_file = fopen("log.txt","w");
instructionNetwork.init(H * W);
construct_network(H, W, K);
while (true) {
int rowA, colA, rowB, colB;
assert(1 == scanf("%d", &rowA));
if (rowA == -1)
break;
assert(3 == scanf("%d%d%d", &colA, &rowB, &colB));
if ((!increasing(0, rowA, H-1)) ||
(!increasing(0, colA, W-1)) ||
(!increasing(0, rowB, H-1)) ||
(!increasing(0, colB, W-1)) ||
(rowA == rowB && colA == colB)) {
printf("-1\n");
fprintf(log_file, "-1\n");
fflush(stdout);
fflush(log_file);
continue;
}
vector<int> memory_cells;
for (int row = 0; row < H; row++)
for (int col = 0; col < W; col++) {
bool active = (row == rowA && col == colA) || (row == rowB && col == colB);
memory_cells.push_back(active ? 1 : 0);
}
int computation_result = instructionNetwork.compute(memory_cells);
printf("%d\n", computation_result);
fflush(stdout);
for(int i = 0; i < (int)memory_cells.size(); i++)
fprintf(log_file, (i ? " %d" : "%d"), memory_cells[i]);
fprintf(log_file, "\n");
fflush(log_file);
}
fclose(stdin);
}
int counter = 0;
int add_and(vector<int> Ns) {
counter++;
return instructionNetwork.add_instruction(_AND, Ns);
}
int add_or(vector<int> Ns) {
counter++;
return instructionNetwork.add_instruction(_OR, Ns);
}
int add_xor(vector<int> Ns) {
counter++;
return instructionNetwork.add_instruction(_XOR, Ns);
}
int add_not(int N) {
counter++;
vector<int> Ns = {N};
return instructionNetwork.add_instruction(_NOT, Ns);
}
#endif
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |