Submission #769693

# Submission time Handle Problem Language Result Execution time Memory
769693 2023-06-30T04:00:13 Z Plurm Alice, Bob, and Circuit (APIO23_abc) C++17
54 / 100
75 ms 11088 KB
#include "abc.h"
#include <bits/stdc++.h>
using namespace std;

// you may find the definitions useful
const int OP_ZERO = 0;    // f(OP_ZERO,    x0, x1) = 0
const int OP_NOR = 1;     // f(OP_NOR,     x0, x1) = !(x0 || x1)
const int OP_GREATER = 2; // f(OP_GREATER, x0, x1) = (x0 > x1)
const int OP_NOT_X1 = 3;  // f(OP_NOT_X1,  x0, x1) = !x1
const int OP_LESS = 4;    // f(OP_LESS,    x0, x1) = (x0 < x1)
const int OP_NOT_X0 = 5;  // f(OP_NOT_X0,  x0, x1) = !x0
const int OP_XOR = 6;     // f(OP_XOR,     x0, x1) = (x0 ^ x1)
const int OP_NAND = 7;    // f(OP_NAND,    x0, x1) = !(x0 && x1)
const int OP_AND = 8;     // f(OP_AND,     x0, x1) = (x0 && x1)
const int OP_EQUAL = 9;   // f(OP_EQUAL,   x0, x1) = (x0 == x1)
const int OP_X0 = 10;     // f(OP_X0,      x0, x1) = x0
const int OP_GEQ = 11;    // f(OP_GEQ,     x0, x1) = (x0 >= x1)
const int OP_X1 = 12;     // f(OP_X1,      x0, x1) = x1
const int OP_LEQ = 13;    // f(OP_LEQ,     x0, x1) = (x0 <= x1)
const int OP_OR = 14;     // f(OP_OR,      x0, x1) = (x0 || x1)
const int OP_ONE = 15;    // f(OP_ONE,     x0, x1) = 1

vector<tuple<int, int, int>> &
operator+=(vector<tuple<int, int, int>> &x,
           const vector<tuple<int, int, int>> &y) {
    for (auto p : y)
        x.push_back(p);
    return x;
}

vector<tuple<int, int, int>> adder(int i1, int i2, int offs) {
    // 16-bit-adder from i1..i1+15 and i2..i2+15
    vector<tuple<int, int, int>> ret;
    ret.push_back({OP_AND, i1, i2}); // offs
    ret.push_back({OP_XOR, i1, i2}); // offs+1
    for (int j = 1; j < 16; j++) {
        int c = offs + 2 + 8 * (j - 1);
        int carry = j == 1 ? offs : c - 1;
        ret.push_back({OP_XOR, i1 + j, i2 + j}); // c
        ret.push_back({OP_XOR, carry, c});       // c+1
        ret.push_back({OP_OR, i1 + j, i2 + j});  // c+2
        ret.push_back({OP_NOR, carry, c + 2});   // c+3
        ret.push_back({OP_AND, i1 + j, i2 + j}); // c+4
        ret.push_back({OP_AND, carry, c + 4});   // c+5
        ret.push_back({OP_NOR, c + 1, c + 3});   // c+6
        ret.push_back({OP_OR, c + 5, c + 6});    // c+7
                                                 // (c+1, c+7): (output, carry)
    }
    ret.push_back({OP_OR, offs + 1, offs + 1});
    for (int j = 1; j < 16; j++) {
        ret.push_back({OP_OR, offs + 3 + 8 * (j - 1), offs + 3 + 8 * (j - 1)});
    }
    return ret; // the last 16 bits is the result
}

vector<tuple<int, int, int>> multiplier(int i1, int i2, int offs) {
    vector<tuple<int, int, int>> ret;
    int last = i2;
    for (int j = 1; j < 16; j++) {
        ret += adder(last, last, offs + 138 * (j - 1));
        last = offs + 138 * j - 16;
    }
    // i2, offs + 138*j - 16
    int start = offs + 138 * 15;
    for (int k = 0; k < 16; k++)
        ret.push_back({OP_AND, i1, i2 + k});
    for (int j = 1; j < 16; j++)
        for (int k = 0; k < 16; k++)
            ret.push_back({OP_AND, i1 + j, offs + 138 * j - 16 + k});
    int stop = start + 256;
    ret += adder(start, start + 16, stop);
    for (int j = 2; j < 16; j++) {
        ret += adder(stop + 138 * (j - 1) - 16, start + 16 * j,
                     stop + 138 * (j - 1));
    }
    // result: offs + 138*15 + 256 + 138*15 - 16
    return ret; // the last 16 bits is the result
}

// Alice
int // returns la
alice(
    /*  in */ const int n,
    /*  in */ const char names[][5],
    /*  in */ const unsigned short numbers[],
    /* out */ bool outputs_alice[]) {
    set<string> names2;
    for (int i = 0; i < n; i++) {
        names2.insert(names[i]);
    }
    vector<string> sorted(names2.begin(), names2.end());
    vector<int> bck(n);
    for (int i = 0; i < n; i++) {
        bck[lower_bound(sorted.begin(), sorted.end(), names[i]) -
            sorted.begin()] = i;
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 16; j++) {
            outputs_alice[16 * i + j] = (numbers[bck[i]] & (1 << j)) ? 1 : 0;
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 16; j++) {
            outputs_alice[16 * n + 16 * i + j] = (bck[i] & (1 << j)) ? 1 : 0;
        }
    }
    return 32 * n;
}

// Bob
int // returns lb
bob(
    /*  in */ const int m,
    /*  in */ const char senders[][5],
    /*  in */ const char recipients[][5],
    /* out */ bool outputs_bob[]) {
    set<string> names;
    for (int i = 0; i < m; i++) {
        names.insert(senders[i]);
        names.insert(recipients[i]);
    }
    vector<string> sorted(names.begin(), names.end());
    for (int i = 0; i < sorted.size() * sorted.size(); i++)
        outputs_bob[i] = 0;
    for (int i = 0; i < m; i++) {
        outputs_bob[(lower_bound(sorted.begin(), sorted.end(), senders[i]) -
                     sorted.begin()) *
                        sorted.size() +
                    (lower_bound(sorted.begin(), sorted.end(), recipients[i]) -
                     sorted.begin())] = 1;
    }
    return sorted.size() * sorted.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]) {
    int offs = la + lb;
    vector<int> tmps;
    int n = la / 32;
    for (int j = 0; j < n; j++) {
        for (int i = 0; i < n; i++) {
            for (int k = 0; k < 16; k++) {
                operations[offs] = OP_AND;
                operands[offs][0] = la + n * i + j;
                operands[offs][1] = 16 * i + k;
                offs++;
            }
        }
        int pivot = offs - 16;
        for (int i = 1; i < n; i++) {
            auto ret = adder(pivot - i * 16, offs - 16, offs);
            for (int j = 0; j < 138; j++) {
                operations[offs + j] = get<0>(ret[j]);
                operands[offs + j][0] = get<1>(ret[j]);
                operands[offs + j][1] = get<2>(ret[j]);
            }
            offs += 138;
        }
        tmps.push_back(offs - 16);
    }
    operations[offs] = OP_ZERO;
    operands[offs][0] = operands[offs][1] = 0;
    int BIT0 = offs;
    offs++;
    operations[offs] = OP_ONE;
    operands[offs][0] = operands[offs][1] = 0;
    int BIT1 = offs;
    offs++;
    for (int j = 0; j < n; j++) {
        int last2 = -1;
        for (int i = 0; i < n; i++) {
            int st = offs;
            for (int k = 0; k < 16; k++) {
                operations[offs] = OP_EQUAL;
                operands[offs][0] = 16 * n + 16 * i + k;
                operands[offs][1] = (j & (1 << k)) ? BIT1 : BIT0;
                offs++;
            }
            int last = st;
            for (int k = 1; k < 16; k++) {
                operations[offs] = OP_AND;
                operands[offs][0] = last;
                operands[offs][1] = st + k;
                last = offs;
                offs++;
            }
            for (int k = 0; k < 16; k++) {
                operations[offs] = OP_AND;
                operands[offs][0] = last;
                operands[offs][1] = tmps[i] + k;
                offs++;
            }
            if (last2 == -1)
                last2 = offs - 16;
            else {
                auto ret = adder(last2, offs - 16, offs);
                for (int j = 0; j < 138; j++) {
                    operations[offs + j] = get<0>(ret[j]);
                    operands[offs + j][0] = get<1>(ret[j]);
                    operands[offs + j][1] = get<2>(ret[j]);
                }
                offs += 138;
                last2 = offs - 16;
            }
        }
        for (int k = 0; k < 16; k++)
            outputs_circuit[j][k] = last2 + k;
    }
    return offs;
}

Compilation message

abc.cpp: In function 'int bob(int, const char (*)[5], const char (*)[5], bool*)':
abc.cpp:123:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::__cxx11::basic_string<char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  123 |     for (int i = 0; i < sorted.size() * sorted.size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 1188 KB WA circuit() returns invalid operand (<0 or >=current label)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 1188 KB WA circuit() returns invalid operand (<0 or >=current label)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 1188 KB WA circuit() returns invalid operand (<0 or >=current label)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 6208 KB Correct!
2 Correct 36 ms 7952 KB Correct!
3 Correct 43 ms 8656 KB Correct!
# Verdict Execution time Memory Grader output
1 Correct 10 ms 6208 KB Correct!
2 Correct 36 ms 7952 KB Correct!
3 Correct 43 ms 8656 KB Correct!
4 Correct 45 ms 8040 KB Correct!
5 Correct 60 ms 8608 KB Correct!
# Verdict Execution time Memory Grader output
1 Correct 10 ms 6208 KB Correct!
2 Correct 36 ms 7952 KB Correct!
3 Correct 43 ms 8656 KB Correct!
4 Correct 45 ms 8040 KB Correct!
5 Correct 60 ms 8608 KB Correct!
6 Correct 41 ms 8864 KB Correct!
7 Correct 75 ms 11088 KB Correct!
# Verdict Execution time Memory Grader output
1 Runtime error 50 ms 6380 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 50 ms 6380 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 1188 KB WA circuit() returns invalid operand (<0 or >=current label)
2 Halted 0 ms 0 KB -