#include "abc.h"
#include <bits/stdc++.h>
using namespace std;
#define BIT(n, i) (((n) >> (i)) & 1ll)
// 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
// Alice
int alice(const int n, const char names[][5],
const unsigned short numbers[], bool outputs_alice[]) {
for (int i = 0; i < 16; ++i) outputs_alice[i] = BIT(numbers[0], i);
return 16;
}
// Bob
int bob(const int m, const char senders[][5],
const char recipients[][5], bool outputs_bob[]) {
for (int i = 0; i < 10; ++i) outputs_bob[i] = BIT(m, i);
return 10;
}
// Circuit
int circuit(const int la, const int lb, int operations[],
int operands[][2], int outputs_circuit[][16]) {
auto op = [&](int k, int x, int y, int z) {
operations[z] = k;
operands[z][0] = x, operands[z][1] = y;
};
int idx = la + lb; vector<int> where(10, 0);
for (int i = 0; i < 10; ++i) {
where[i] = idx;
for (int j = 0; j < i; ++j) op(OP_ZERO, 0, 0, idx++);
for (int j = i; j < 16; ++j) op(OP_AND, la + i, j - i, idx++);
}
auto add = [&](int x, int y, int ans, int carry) {
op(OP_XOR, x, y, ans);
op(OP_AND, x, y, carry);
};
// Add two 16-bit numbers
auto add16 = [&](int x, int y) {
int p = idx; idx += 4 * 15 + 1;
int ret = idx;
add(x, y, idx++, p++);
for (int i = 1; i < 16; ++i) {
add(x + i, y + i, p, p + 1);
add(p - 1, p, idx++, p + 2);
op(OP_OR, p + 1, p + 2, p + 3);
p += 4;
}
return ret;
};
add16(where[0], where[1]);
for (int i = 2; i < 10; ++i) add16(where[i], idx - 16);
for (int i = 0; i < 16; ++i) outputs_circuit[0][i] = idx - 16 + i;
return idx;
}
#ifdef hwe
const int MAX_LA = 100000;
const int MAX_LB = 100000;
const int MAX_L = 20000000;
void fatal(const string& msg) {
cerr << msg << endl;
exit(1);
}
void ensureImpl(bool condition, const char* conditionStr) {
if (condition)
return;
fatal("Condition does not satisfy: " + string(conditionStr));
}
#define ensure(x...) ensureImpl(x, #x)
template <typename F>
void timed(const string& name, const F& func) {
cerr << "Running " << name << "..." << endl;
auto start = chrono::high_resolution_clock::now();
func();
auto end = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::nanoseconds>(end - start);
cerr << name << " took " << fixed << setprecision(6) << (double) duration.count() / 1e9 << " seconds" << endl;
}
int main() {
freopen("abc.log", "w", stderr);
// ==================== Read Input ====================
int n, m;
cin >> n >> m;
ensure(0 <= n && n <= 700);
ensure(0 <= m && m <= 1000);
auto readUshort = [&]() {
string s;
cin >> s;
stringstream ss(s);
unsigned short x;
ss >> x;
if (s != to_string(x))
fatal("Invalid input for unsigned short: " + s);
return x;
};
vector<pair<string, unsigned short>> students;
map<string, unsigned short> studentsMap;
for (int i = 0; i < n; i++) {
string name;
cin >> name;
if (name.empty())
fatal("Expected name");
if (name.size() > 4)
fatal("Name too long: " + name);
bool ok = true;
for (char c : name)
if (c < 'a' || c > 'z')
ok = false;
if (!ok)
fatal("Invalid name: " + name);
unsigned short number = readUshort();
students.emplace_back(name, number);
if (studentsMap.count(name))
fatal("Duplicate name: " + name);
studentsMap[name] = number;
}
auto readExistingName = [&] {
string name;
cin >> name;
if (!studentsMap.count(name))
fatal("Unknown name in Bob's input: " + name);
return name;
};
vector<pair<string, string>> messages;
for (int i = 0; i < m; i++) {
string sender = readExistingName();
string recipient = readExistingName();
messages.emplace_back(sender, recipient);
}
// ==================== Call Alice ====================
char (*aliceNames)[5] = new char[n][5];
memset(aliceNames[0], 0, n * 5);
unsigned short* aliceNumbers = new unsigned short[n];
bool* aliceOutputs = new bool[MAX_LA];
memset(aliceOutputs, 0, MAX_LA);
for (int i = 0; i < n; i++) {
strcpy(aliceNames[i], students[i].first.c_str());
aliceNumbers[i] = students[i].second;
}
int la;
timed("Alice", [&]() {
la = alice(n, aliceNames, aliceNumbers, aliceOutputs);
});
cerr << "la = " << la << endl;
ensure(0 <= la && la <= MAX_LA);
// ==================== Call Bob ====================
char (*bobSenders)[5] = new char[m][5];
memset(bobSenders[0], 0, m * 5);
char (*bobRecipients)[5] = new char[m][5];
memset(bobRecipients[0], 0, m * 5);
bool* bobOutputs = new bool[MAX_LB];
memset(bobOutputs, 0, MAX_LB);
for (int i = 0; i < m; i++) {
strcpy(bobSenders[i], messages[i].first.c_str());
strcpy(bobRecipients[i], messages[i].second.c_str());
}
int lb;
timed("Bob", [&]() {
lb = bob(m, bobSenders, bobRecipients, bobOutputs);
});
cerr << "lb = " << lb << endl;
ensure(0 <= lb && lb <= MAX_LB);
// ==================== Call Circuit ====================
int* circuitOperations = new int[MAX_L];
memset(circuitOperations, 0xff, MAX_L * sizeof(int));
int (*circuitOperands)[2] = new int[MAX_L][2];
memset(circuitOperands[0], 0xff, MAX_L * 2 * sizeof(int));
int (*circuitOutputs)[16] = new int[n][16];
memset(circuitOutputs[0], 0xff, n * 16 * sizeof(int));
int l;
timed("Circuit", [&]() {
l = circuit(la, lb, circuitOperations, circuitOperands, circuitOutputs);
});
cerr << "l = " << l << endl;
ensure(0 <= l && l <= MAX_L);
bool* values = new bool[l];
memcpy(values, aliceOutputs, la * sizeof(bool));
memcpy(values + la, bobOutputs, lb * sizeof(bool));
for (int i = la + lb; i < l; i++) {
int op = circuitOperations[i];
if (op < 0 || op > 15)
fatal("Invalid operation: " + to_string(op) + " at Circuit index " + to_string(i));
int a = circuitOperands[i][0];
int b = circuitOperands[i][1];
if (a < 0 || a >= i)
fatal("Invalid operand 0: " + to_string(a) + " at Circuit index " + to_string(i));
if (b < 0 || b >= i)
fatal("Invalid operand 1: " + to_string(b) + " at Circuit index " + to_string(i));
bool x0 = values[a];
bool x1 = values[b];
values[i] = op >> (x0 + 2 * x1) & 1;
}
for (int i = 0; i < la; ++i) cout << values[i];
cout << '\n';
for (int i = 0; i < lb; ++i) cout << values[i + la];
cout << '\n';
// int idx = la + lb;
// for (int i = 0; i < 10; ++i) {
// for (int j = 0; j < 16; ++j) cout << values[idx++];
// cout << '\n';
// }
// for (int i = 0; i < 16; ++i) {
// for (int j = 0; j < 16; ++j) cout << values[343 + 77 * i + j];
// cout << '\n';
// }
for (int i = 0; i < n; i++)
for (int j = 0; j < 16; j++)
if (circuitOutputs[i][j] < 0 || circuitOutputs[i][j] >= l)
fatal("Invalid output: " + to_string(circuitOutputs[i][j]) + " at Circuit index " + to_string(i) + ", " + to_string(j));
vector<unsigned short> answers;
for (int i = 0; i < n; i++) {
unsigned short ans = 0;
for (int j = 0; j < 16; j++)
ans |= (unsigned short) values[circuitOutputs[i][j]] << j;
answers.push_back(ans);
}
// ==================== Calculate Reference Answer ====================
map<string, unsigned short> referenceMap;
for (int i = 0; i < m; i++)
referenceMap[messages[i].second] += studentsMap[messages[i].first];
bool ok = true;
for (int i = 0; i < n; i++)
if (answers[i] != referenceMap[students[i].first]) {
ok = false;
cerr << "Wrong answer for " << students[i].first << ": expected " << referenceMap[students[i].first] << ", but got " << answers[i] << endl;
}
if (ok)
cerr << "Your answer seems correct" << endl;
// ==================== Print Answer ====================
for (int i = 0; i < n; i++)
cout << answers[i] << '\n';
return 0;
}
#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... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |