제출 #765390

#제출 시각아이디문제언어결과실행 시간메모리
765390pandapythoner앨리스, 밥, 서킷 (APIO23_abc)C++17
66 / 100
2976 ms2097152 KiB
#include "abc.h"

// 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

#include <bits/stdc++.h>

using namespace std;

#define ll long long

const int bts = 16;
const int name_bts = 19;

int encode_name(const char *name) {
    int rs = 0;
    int t = 1;
    for (int len = 1;; len += 1) {
        t *= 26;
        if (name[len] == 0) {
            ll f = 0;
            for (int i = 0; i < len; i += 1) {
                f = f * 26 + (name[i] - 'a');
            }
            rs += f;
            break;
        }
        rs += t;
    }
    return rs;
}

// Alice
int  // returns la
alice(
    /*  in */ const int n,
    /*  in */ const char names[][5],
    /*  in */ const unsigned short numbers[],
    /* out */ bool outputs_alice[]) {
    if (n == 0) {
        outputs_alice[0] = 0;
        return 1;
    }
    int sz = n * (name_bts + bts);
    int t = 0;
    for (int i = 0; i < n; i += 1) {
        int e = encode_name(names[i]);
        for (int j = 0; j < name_bts; j += 1) {
            outputs_alice[t + j] = ((e >> j) & 1);
        }
        t += name_bts;
    }
    for (int i = 0; i < n; i += 1) {
        int e = numbers[i];
        for (int j = 0; j < bts; j += 1) {
            outputs_alice[t + j] = ((e >> j) & 1);
        }
        t += bts;
    }
    return sz;
}

// Bob
int  // returns lb
bob(
    /*  in */ const int m,
    /*  in */ const char senders[][5],
    /*  in */ const char recipients[][5],
    /* out */ bool outputs_bob[]) {
    int sz = m * (name_bts + name_bts);
    int t = 0;
    for (int i = 0; i < m; i += 1) {
        int e = encode_name(senders[i]);
        for (int j = 0; j < name_bts; j += 1) {
            outputs_bob[t + j] = ((e >> j) & 1);
        }
        t += name_bts;
    }
    for (int i = 0; i < m; i += 1) {
        int e = encode_name(recipients[i]);
        for (int j = 0; j < name_bts; j += 1) {
            outputs_bob[t + j] = ((e >> j) & 1);
        }
        t += name_bts;
    }
    return sz;
}

int c = 0;
vector<int> operations;
vector<vector<int>> operands;

int add_operation(int op_type, int op1, int op2) {
    operations.push_back(op_type);
    operands.push_back(vector<int>({op1, op2}));
    c += 1;
    return c - 1;
}

vector<int> get_if_equal(vector<int> a, vector<int> b, vector<int> num) {
    int d = add_operation(OP_XOR, a[0], b[0]);
    for (int i = 1; i < (int)a.size(); i += 1) {
        int t = add_operation(OP_XOR, a[i], b[i]);
        d = add_operation(OP_OR, d, t);
    }
    d = add_operation(OP_NOT_X0, d, d);
    vector<int> rs(num.size());
    for (int i = 0; i < (int)num.size(); i += 1) {
        rs[i] = add_operation(OP_AND, d, num[i]);
    }
    return rs;
}

pair<int, int> add_bits(int x, int y, int z = -1) {
    int t = add_operation(OP_XOR, x, y);
    int nxt = add_operation(OP_AND, x, y);
    if (z != -1) {
        nxt = add_operation(OP_AND, x, y);
        nxt = add_operation(OP_OR, nxt, add_operation(OP_AND, t, z));
        t = add_operation(OP_XOR, t, z);
    }
    return make_pair(t, nxt);
}

vector<int> add_numbers(vector<int> a, vector<int> b) {
    int nxt = -1;
    vector<int> rs(a.size());
    for (int i = 0; i < (int)a.size(); i += 1) {
        auto [x, new_nxt] = add_bits(a[i], b[i], nxt);
        nxt = new_nxt;
        rs[i] = x;
    }
    return rs;
}

vector<int> get_or(vector<int> a, vector<int> b) {
    vector<int> rs(a.size());
    for (int i = 0; i < (int)a.size(); i += 1) {
        rs[i] = add_operation(OP_OR, a[i], b[i]);
    }
    return rs;
}

vector<int> get_zero(int sz) {
    vector<int> rs(sz);
    for (int i = 0; i < sz; i += 1) {
        rs[i] = add_operation(OP_ZERO, 0, 0);
    }
    return rs;
}

// 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 n = la / (bts + name_bts);
    int m = lb / (name_bts + name_bts);
    int t = 0;
    vector<vector<int>> names;
    for (int i = 0; i < n; i += 1) {
        vector<int> f(name_bts);
        for (int j = 0; j < name_bts; j += 1) {
            f[j] = t + j;
        }
        t += name_bts;
        names.push_back(f);
    }
    vector<vector<int>> numbers;
    for (int i = 0; i < n; i += 1) {
        vector<int> f(bts);
        for (int j = 0; j < bts; j += 1) {
            f[j] = t + j;
        }
        t += bts;
        numbers.push_back(f);
    }
    vector<vector<int>> senders;
    for (int i = 0; i < m; i += 1) {
        vector<int> f(name_bts);
        for (int j = 0; j < name_bts; j += 1) {
            f[j] = t + j;
        }
        t += name_bts;
        senders.push_back(f);
    }
    vector<vector<int>> recipients;
    for (int i = 0; i < m; i += 1) {
        vector<int> f(name_bts);
        for (int j = 0; j < name_bts; j += 1) {
            f[j] = t + j;
        }
        t += name_bts;
        recipients.push_back(f);
    }

    c = t;
    operations.clear();
    operands.clear();

    vector<vector<int>> senders_nums(m);
    for (int i = 0; i < m; i += 1) {
        vector<int> num = get_zero(bts);
        for (int j = 0; j < n; j += 1) {
            num = get_or(num, get_if_equal(senders[i], names[j], numbers[j]));
        }
        senders_nums[i] = num;
    }
    vector<vector<int>> results(n);
    for (int i = 0; i < n; i += 1) {
        vector<int> num = get_zero(bts);
        for (int j = 0; j < m; j += 1) {
            auto summand = get_if_equal(names[i], recipients[j], senders_nums[j]);
            num = add_numbers(num, summand);
        }
        results[i] = num;
    }
    for (int i = 0; i < (int)operations.size(); i += 1) {
        _operations[i + la + lb] = operations[i];
        for (int j = 0; j < 2; j += 1) {
            _operands[i + la + lb][j] = operands[i][j];
        }
    }
    for (int i = 0; i < n; i += 1) {
        for (int j = 0; j < bts; j += 1) {
            outputs_circuit[i][j] = results[i][j];
        }
    }
    return c;
}
#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...