제출 #927672

#제출 시각아이디문제언어결과실행 시간메모리
927672minhnhatnoe앨리스, 밥, 서킷 (APIO23_abc)C++17
12 / 100
153 ms10236 KiB
#include "abc.h" #include <bits/stdc++.h> using namespace std; enum OP { OP_ZERO, // f(OP_ZERO, x0, x1) = 0 OP_NOR, // f(OP_NOR, x0, x1) = !(x0 || x1) OP_GREATER, // f(OP_GREATER, x0, x1) = (x0 > x1) OP_NOT_X1, // f(OP_NOT_X1, x0, x1) = !x1 OP_LESS, // f(OP_LESS, x0, x1) = (x0 < x1) OP_NOT_X0, // f(OP_NOT_X0, x0, x1) = !x0 OP_XOR, // f(OP_XOR, x0, x1) = (x0 ^ x1) OP_NAND, // f(OP_NAND, x0, x1) = !(x0 && x1) OP_AND, // f(OP_AND, x0, x1) = (x0 && x1) OP_EQUAL, // f(OP_EQUAL, x0, x1) = (x0 == x1) OP_X0, // f(OP_X0, x0, x1) = x0 OP_GEQ, // f(OP_GEQ, x0, x1) = (x0 >= x1) OP_X1, // f(OP_X1, x0, x1) = x1 OP_LEQ, // f(OP_LEQ, x0, x1) = (x0 <= x1) OP_OR, // f(OP_OR, x0, x1) = (x0 || x1) OP_ONE, // f(OP_ONE, x0, x1) = 1 }; // Alice int // returns la alice( /* in */ const int n, /* in */ const char names[][5], /* in */ const unsigned short numbers[], /* out */ bool outputs_alice[]) { int a = numbers[0]; for (int i=0; i<16; i++) outputs_alice[i] = a & (1 << i); return 16; } // Bob int // returns lb bob( /* in */ const int m, /* in */ const char senders[][5], /* in */ const char recipients[][5], /* out */ bool outputs_bob[]) { for (int i=0; i<16; i++) outputs_bob[i] = m & (1 << i); return 16; } namespace circuit_ns{ struct bit{ int v = 0; }; struct bit_construct{ OP p; bit x, y; }; vector<bit_construct> a; bit create_gate(OP p, bit x, bit y){ assert(x.v != -1 && y.v != -1); a.push_back({p, x, y}); return bit{int(a.size()-1)}; }; #define DEF_OP(op, name) bit operator op (const bit &a, const bit &b){return create_gate(name, a, b);} DEF_OP(&, OP_AND) DEF_OP(|, OP_OR) DEF_OP(^, OP_XOR) #undef DEF_OP struct nbr{ bit s[16]; nbr(){ memset(s, -1, sizeof s); } friend nbr operator+(const nbr &l, const nbr &r){ nbr result; int ptr = 0; for (; ptr<16 && (l.s[ptr].v == -1 && r.s[ptr].v == -1); ptr++){ result.s[ptr].v = -1; } for (; ptr<16 && (l.s[ptr].v == -1 || r.s[ptr].v == -1); ptr++){ result.s[ptr] = (l.s[ptr].v != -1 ? l.s[ptr] : r.s[ptr]); } for (bit three{-1}; ptr < 16; ptr++){ bit one = l.s[ptr], two = r.s[ptr]; assert(one.v != -1 && two.v != -1); if (three.v == -1){ result.s[ptr] = one ^ two; three = one & two; } else{ bit four = one ^ two; bit five = result.s[ptr] = four ^ three; bit six = one & two; bit seven = four & three; three = six | seven; } } return result; } friend nbr operator<<(const nbr &a, int b){ nbr result; copy(a.s, a.s + 16 - b, result.s + b); return result; } friend nbr operator&(const nbr &a, const bit &g){ nbr result; for (int i=0; i<16; i++){ if (a.s[i].v == -1) continue; result.s[i] = a.s[i] & g; } return result; } friend nbr operator*(const nbr &lhs, const nbr &rhs){ int sizeab = a.size(); nbr ab; { nbr nb = rhs; for (int i=0; i<16; i++){ if (lhs.s[i].v == -1) continue; nbr anb = nb & lhs.s[i]; ab = ab + anb; nb = nb << 1; } sizeab = a.size() - sizeab; } int sizeba = a.size(); nbr ba; { nbr na = lhs; for (int i=0; i<16; i++){ if (rhs.s[i].v == -1) continue; nbr ana = na & rhs.s[i]; ba = ba + ana; na = na << 1; } sizeba = a.size() - sizeab; } if (sizeab <= sizeba) return ab; else return ba; } }; int init(int la, int lb, int ops[], int opers[][2], int outputs[][16]){ a.resize(la + lb); nbr x, y; for (int i=0; i<16; i++) x.s[i].v = i, y.s[i].v = 16 + i; nbr result = x * y; bit zero = create_gate(OP_ZERO, bit{0}, bit{0}); for (int i=la+lb; i<a.size(); i++){ ops[i] = a[i].p; opers[i][0] = a[i].x.v; opers[i][1] = a[i].y.v; } for (int i=0; i<16; i++){ outputs[0][i] = result.s[i].v; if (outputs[0][i] == -1) outputs[0][i] = zero.v; } return a.size(); } } // Circuit int // returns l circuit( /* in */ const int la, /* in */ const int lb, /* out */ int operations[], /* out */ int operands[][2], /* out */ int outputs_circuit[][16]) { return circuit_ns::init(la, lb, operations, operands, outputs_circuit); }

컴파일 시 표준 에러 (stderr) 메시지

abc.cpp: In constructor 'circuit_ns::nbr::nbr()':
abc.cpp:78:35: warning: 'void* memset(void*, int, size_t)' writing to an object of non-trivial type 'struct circuit_ns::bit'; use assignment instead [-Wclass-memaccess]
   78 |             memset(s, -1, sizeof s);
      |                                   ^
abc.cpp:53:12: note: 'struct circuit_ns::bit' declared here
   53 |     struct bit{
      |            ^~~
abc.cpp: In function 'circuit_ns::nbr circuit_ns::operator+(const circuit_ns::nbr&, const circuit_ns::nbr&)':
abc.cpp:100:25: warning: variable 'five' set but not used [-Wunused-but-set-variable]
  100 |                     bit five = result.s[ptr] = four ^ three;
      |                         ^~~~
abc.cpp: In function 'int circuit_ns::init(int, int, int*, int (*)[2], int (*)[16])':
abc.cpp:161:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<circuit_ns::bit_construct>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  161 |         for (int i=la+lb; i<a.size(); i++){
      |                           ~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...