Submission #750518

#TimeUsernameProblemLanguageResultExecution timeMemory
750518marvinthangAlice, Bob, and Circuit (APIO23_abc)C++17
66 / 100
97 ms8356 KiB
#include "abc.h" #include <bits/stdc++.h> using namespace std; #define fi first #define se second #define left ___left #define right ___right #define TIME (1.0 * clock() / CLOCKS_PER_SEC) #define MASK(i) (1LL << (i)) #define BIT(x, i) ((x) >> (i) & 1) #define __builtin_popcount __builtin_popcountll #define ALL(v) (v).begin(), (v).end() #define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i) #define REPD(i, n) for (int i = (n); i--; ) #define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i) #define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; ) #define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i) #define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i) #define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u) #define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u) #ifdef LOCAL #include "debug.h" #else #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); } #define DB(...) 23 #define db(...) 23 #define debug(...) 23 #endif template <class U, class V> scan_op(pair <U, V>) { return in >> u.first >> u.second; } template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; } template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; } template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); } template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); } template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; } template <class U, class V> bool maximize(U &a, V b) { if (a < b) { a = b; return true; } return false; } // end of template // 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 const int LOG = 16; // 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 == 1) { REP(i, LOG) outputs_alice[i] = BIT(numbers[0], i); return LOG; } vector <string> s; REP(i, n) s.push_back(names[i]); int l = 0; vector <int> pos(n); iota(ALL(pos), 0); sort(ALL(pos), [&] (int a, int b) { return s[a] < s[b]; }); REP(i, n) REP(j, LOG) outputs_alice[l++] = BIT(numbers[pos[i]], j); vector <int> rp(n); REP(i, n) rp[pos[i]] = i; REP(i, n) REP(j, n) outputs_alice[l++] = (j == rp[i]); return l; } // Bob int // returns lb bob( /* in */ const int m, /* in */ const char senders[][5], /* in */ const char recipients[][5], /* out */ bool outputs_bob[] ) { vector <string> s; REP(i, m) { s.push_back(senders[i]); s.push_back(recipients[i]); } sort(ALL(s)); s.erase(unique(ALL(s)), s.end()); if (s.size() <= 1) { REP(i, 10) outputs_bob[i] = BIT(m, i); return 10; } int n = s.size(); auto id = [&] (string x) { return lower_bound(ALL(s), x) - s.begin(); }; int l = 0; vector <vector <bool>> add(n, vector<bool>(n, false)); REP(i, m) add[id(senders[i])][id(recipients[i])] = true; REP(i, n) REP(j, n) outputs_bob[l++] = add[i][j]; return l; } // Circuit int // returns l circuit( /* in */ const int la, /* in */ const int lb, /* out */ int operations[], /* out */ int operands[][2], /* out */ int out[][16] ) { int cur = la + lb; auto op = [&] (int op, int x, int y) { operations[cur] = op; operands[cur][0] = x; operands[cur][1] = y; return cur++; }; int n = la == LOG ? 1 : sqrt(lb); vector <vector <int>> res(n, vector<int>(LOG, -1)); REP(i, n) REP(j, LOG) res[i][j] = op(OP_ZERO, 0, 0); auto add = [&] (int r, const vector <int> pos) { int rem = op(OP_ZERO, 0, 0); REP(i, LOG) { int p = op(OP_XOR, res[r][i], pos[i]); int a = op(OP_AND, res[r][i], pos[i]); int b = op(OP_AND, res[r][i], rem); int c = op(OP_AND, pos[i], rem); int x = op(OP_OR, a, b); res[r][i] = op(OP_XOR, p, rem); rem = op(OP_OR, x, c); } }; if (n == 1) { REP(i, lb) { vector <int> pos(LOG); REP(j, LOG) pos[j] = j < i ? op(OP_ZERO, 0, 0) : op(OP_AND, i + LOG, j - i); add(0, pos); } REP(i, LOG) out[0][i] = res[0][i]; return cur; } int f = la; REP(u, n) REP(v, n) { vector <int> pos(LOG); REP(j, LOG) pos[j] = op(OP_AND, f, u * LOG + j); add(v, pos); ++f; } f = n * LOG; REP(i, n) { REP(j, LOG) { int cur = -1; REP(k, n) { int x = op(OP_AND, f + k, res[k][j]); if (cur == -1) cur = x; else cur = op(OP_OR, cur, x); } out[i][j] = cur; } f += n; } return cur; } #ifdef LOCAL #include "abc.h" #include <chrono> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <sstream> #include <vector> using namespace std; 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() { file("abc"); // ==================== 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 < 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 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...