Submission #927713

# Submission time Handle Problem Language Result Execution time Memory
927713 2024-02-15T09:16:07 Z minhnhatnoe Alice, Bob, and Circuit (APIO23_abc) C++17
12 / 100
113 ms 10172 KB
#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;
        }
    };
    void erase_deps(int la, int lb, vector<nbr> &results){
        vector<int> act(a.size());
        for (int i=0; i<la + lb; i++) act[i] = 1;
        for (const nbr &v: results){
            for (bit x: v.s){
                if (x.v == -1) continue;
                act[x.v] = 1;
            }
        }

        for (int i=a.size()-1; i>=la+lb; i--){
            if (act[i] == 0) continue;
            assert(a[i].x.v < i && a[i].y.v < i);
            act[a[i].x.v] = act[a[i].y.v] = 1;
        }

        int cnt = 0;
        vector<bit_construct> s;
        for (int i=0; i<a.size(); i++){
            if (act[i] == 0) continue;
            act[i] = cnt++;
            s.push_back({a[i].p, bit{act[a[i].x.v]}, bit{act[a[i].y.v]}});
        }
        a = move(s);
        for (nbr &r: results){
            for (bit &v: r.s) v.v = act[v.v];
        }
    }
    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;
        vector<nbr> result{x * y};
        bit zero = create_gate(OP_ZERO, bit{0}, bit{0});
        
        erase_deps(la, lb, result);
        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[0].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);
}

Compilation message

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 'void circuit_ns::erase_deps(int, int, std::vector<circuit_ns::nbr>&)':
abc.cpp:171:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<circuit_ns::bit_construct>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  171 |         for (int i=0; i<a.size(); i++){
      |                       ~^~~~~~~~~
abc.cpp: In function 'int circuit_ns::init(int, int, int*, int (*)[2], int (*)[16])':
abc.cpp:190: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]
  190 |         for (int i=la+lb; i<a.size(); i++){
      |                           ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 1292 KB Correct!
# Verdict Execution time Memory Grader output
1 Correct 0 ms 1292 KB Correct!
2 Correct 1 ms 1304 KB Correct!
# Verdict Execution time Memory Grader output
1 Correct 0 ms 1292 KB Correct!
2 Correct 1 ms 1304 KB Correct!
3 Correct 36 ms 5012 KB Correct!
4 Correct 39 ms 4888 KB Correct!
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 1536 KB WA Your functions alice(), bob(), circuit() finished successfully, but the final output binary string is incorrect.
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 1536 KB WA Your functions alice(), bob(), circuit() finished successfully, but the final output binary string is incorrect.
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 1536 KB WA Your functions alice(), bob(), circuit() finished successfully, but the final output binary string is incorrect.
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 113 ms 10172 KB WA Your functions alice(), bob(), circuit() finished successfully, but the final output binary string is incorrect.
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 113 ms 10172 KB WA Your functions alice(), bob(), circuit() finished successfully, but the final output binary string is incorrect.
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 1292 KB Correct!
2 Correct 1 ms 1304 KB Correct!
3 Correct 36 ms 5012 KB Correct!
4 Correct 39 ms 4888 KB Correct!
5 Incorrect 4 ms 1536 KB WA Your functions alice(), bob(), circuit() finished successfully, but the final output binary string is incorrect.
6 Halted 0 ms 0 KB -