Submission #754993

# Submission time Handle Problem Language Result Execution time Memory
754993 2023-06-09T08:33:23 Z Zanite Alice, Bob, and Circuit (APIO23_abc) C++17
8 / 100
173 ms 14496 KB
#include "abc.h"

// 赤コーダーになりたい
// お願い いいですか?

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

// Pragmas
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

// Namespaces
using namespace std;
using namespace __gnu_pbds;

// Data types
using si	= short int;
using ll	= long long;
using lll	= __int128;
using ld	= long double;

// Pairs
using pii	= pair<int, int>;
using psi	= pair<si, si>;
using pll	= pair<ll, ll>;
using plll	= pair<lll, lll>;
using pld	= pair<ld, ld>;
#define fi	first
#define se	second

// For
#define Frue(i, n, N)		for (int i = (n); i <= (N); i++)
#define  Fru(i, n, N)		for (int i = (n); i <  (N); i++)
#define Frde(i, n, N)		for (int i = (n); i >= (N); i--)
#define  Frd(i, n, N)		for (int i = (n); i >  (N); i--)

// PBDS
template<typename Z>
using ordered_set	= tree<Z, null_type, less<Z>, rb_tree_tag, tree_order_statistics_node_update>;

// Various outputs
template<typename Y, typename Z> ostream& operator<<(ostream &os, const pair<Y, Z> &p) {
    return os << '(' << p.fi << ',' << p.se << ')';
}
template<typename Z> ostream& operator<<(ostream &os, const vector<Z> &v) {
    os << '{'; bool _first = 1;
    for (auto &i : v) {if (!_first) os << ", "; os << i; _first = 0;}
    return os << '}';
}
template<typename Z, unsigned long long sz> ostream& operator<<(ostream &os, const array<Z, sz> &arr) {
    os << '{'; bool _first = 1;
    for (auto &i : arr) {if (!_first) os << ", "; os << i; _first = 0;}
    return os << '}';
}

// Quick macro functions
#define sqr(x)			((x)*(x))
#define debug(x)		cout << #x << " = " << (x) << '\n'
#define debugV(v, x)	cout << #v << "[" << (x) << "] = " << (v[x]) << '\n'
#define rrebug(x)		cerr << #x << " = " << (x) << '\n'
#define rrebugV(v, x)	cerr << #v << "[" << (x) << "] = " << (v[x]) << '\n'

#define All(x)			x.begin(), x.end()
#define Sort(x)			sort(All(x))
#define Reverse(x)		reverse(All(x))
#define Uniqueify(x)	Sort(x); x.erase(unique(All(x)), x.end())

#define RandomSeed			chrono::steady_clock::now().time_since_epoch().count()
#define MultipleTestcases	int _tc; cin >> _tc; for (int _cur_tc = 1; _cur_tc <= _tc; _cur_tc++)

// Check min and max
template<typename Z> void chmin(Z &a, Z b) {a = min(a, b);}
template<typename Z> void chmax(Z &a, Z b) {a = max(a, b);}
 
// Modular arithmetic
template<int MOD>
class ModInt {
  public:
    int v;
    ModInt() : v(0) {}
    ModInt(long long _v) {
        v = int((-MOD < _v && _v < MOD) ? (_v) : (_v % MOD));
        if (v < 0) v += MOD;
    }
 
    friend bool operator==(const ModInt &a, const ModInt &b) {return a.v == b.v;}
    friend bool operator!=(const ModInt &a, const ModInt &b) {return a.v != b.v;}
    friend bool operator< (const ModInt &a, const ModInt &b) {return a.v <  b.v;}
    friend bool operator<=(const ModInt &a, const ModInt &b) {return a.v <= b.v;}
    friend bool operator> (const ModInt &a, const ModInt &b) {return a.v >  b.v;}
    friend bool operator>=(const ModInt &a, const ModInt &b) {return a.v >= b.v;}
 
    ModInt &operator+=(const ModInt &a) {if ((v += a.v) >= MOD) v -= MOD; return *this;}
    ModInt &operator-=(const ModInt &a) {if ((v -= a.v) < 0) v += MOD; return *this;}
    ModInt &operator*=(const ModInt &a) {v = 1ll * v * a.v % MOD; return *this;}
    ModInt &operator/=(const ModInt &a) {return (*this) *= inverse(a);}
 
    friend ModInt pow(ModInt a, long long x) {
        ModInt res = 1;
        for (; x; x /= 2, a *= a) if (x & 1) res *= a;
        return res;
    }
    friend ModInt inverse(ModInt a) {return pow(a, MOD - 2);}
 
    ModInt operator+ () const {return ModInt( v);}
    ModInt operator- () const {return ModInt(-v);}
    ModInt operator++() const {return *this += 1;}
    ModInt operator--() const {return *this -= 1;}
 
    friend ModInt operator+(ModInt a, const ModInt &b) {return a += b;}
    friend ModInt operator-(ModInt a, const ModInt &b) {return a -= b;}
    friend ModInt operator*(ModInt a, const ModInt &b) {return a *= b;}
    friend ModInt operator/(ModInt a, const ModInt &b) {return a /= b;}
 
    friend istream &operator>>(istream &is, ModInt &v) {return is >> v.v;}
    friend ostream &operator<<(ostream &os, const ModInt &v) {return os << v.v;}
};
const int ModA	= 998244353;
const int ModC	= 1e9 + 7;
using MintA	= ModInt<ModA>;
using MintC	= ModInt<ModC>;

// Other constants
const ll INF	= 1e18;
const ll iINF	= 1e9;
const ld EPS	= 1e-9;
const ld iEPS	= 1e-6;

// 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 // returns la
alice(
    /*  in */ const int n,
    /*  in */ const char names[][5],
    /*  in */ const unsigned short numbers[],
    /* out */ bool outputs_alice[]
) {
	int num = numbers[0];
	for (int i = 0; i < 16; i++) {
		outputs_alice[i] = num & 1;
		num >>= 1;
	}
    return 16*n;
}


// 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 < m; i++) outputs_bob[i] = 0;
    return m;
}


// 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]
) {
	if (lb) {
		for (int j = 0; j < 16; j++) {
			operations[la + lb + j] = OP_AND;
			operands[la + lb + j][0] = operands[la + lb + j][1] = j;
			outputs_circuit[0][j] = la + lb + j;
		}
		return la + lb + 16;
	} else {
		operations[la + lb] = OP_ZERO;
		operands[la + lb][0] = 0; operands[la + lb][1] = 0;
		for(int j = 0; j < 16; ++j) {
			outputs_circuit[0][j] = la + lb;
		}
    	return la + lb + 1;
	}
}

#ifdef Zanite
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 < 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
1 Correct 3 ms 1164 KB Correct!
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1164 KB Correct!
2 Correct 3 ms 1204 KB Correct!
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1164 KB Correct!
2 Correct 3 ms 1204 KB Correct!
3 Incorrect 54 ms 5100 KB WA Your functions alice(), bob(), circuit() finished successfully, but the final output binary string is incorrect.
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 1568 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 7 ms 1568 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 7 ms 1568 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 173 ms 14496 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 173 ms 14496 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 3 ms 1164 KB Correct!
2 Correct 3 ms 1204 KB Correct!
3 Incorrect 54 ms 5100 KB WA Your functions alice(), bob(), circuit() finished successfully, but the final output binary string is incorrect.
4 Halted 0 ms 0 KB -