QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#451312 | #8770. Comparator | henryx# | WA | 0ms | 3560kb | C++20 | 3.5kb | 2024-06-23 05:25:13 | 2024-06-23 05:25:13 |
Judging History
answer
#include <bits/stdc++.h>
const char nl = '\n';
using namespace std;
using ll = long long;
using ld = long double;
const int K = 10;
int f(char op, int x, int y) {
if (op == '&') return x & y;
else if (op == '|') return x | y;
else if (op == '^') return x ^ y;
else if (op == '=') return x == y;
assert(0);
return 0;
}
const map<char, int> pri = {
{'!', 5},
{'=', 4},
{'&', 3},
{'|', 2},
{'^', 1}
};
int eval(const string& s, int x, int y) {
stack<int> exprs, ops;
auto resolve_op = [&] {
if (ops.empty() || ops.top() == '(') return;
if (ops.top() == '!') {
ops.pop();
int e = exprs.top(); exprs.pop();
exprs.push(!e);
} else {
char op = ops.top(); ops.pop();
/*
if (!ops.empty()) {
char op2 = ops.top();
if (pri.at(op2) >= pri.at(op1))
}*/
int e1 = exprs.top(); exprs.pop();
int e2 = exprs.top(); exprs.pop();
//cerr << e1 << op << e2 << nl;
exprs.push(f(op, e1, e2));
}
};
//cerr << "parsing " << s << nl;
for (char c : s) {
//cerr << c << nl;
if (c == 'x') {
exprs.push(x);
//resolve_op();
} else if (c == 'y') {
exprs.push(y);
//resolve_op();
} else if (c == '0') {
exprs.push(0);
//resolve_op();
} else if (c == '1') {
exprs.push(1);
//resolve_op();
} else if (c == '(') {
ops.push('(');
} else if (c == ')') {
while (!ops.empty()) {
if (ops.top() == '(') {
ops.pop();
break;
}
resolve_op();
}
} else {
while (!ops.empty() && ops.top() != '(' && pri.at(ops.top()) >= pri.at(c)) {
if (ops.top() == '!' && c == '!') {
ops.pop();
continue;
}
resolve_op();
}
ops.push(c);
}
}
while (!ops.empty()) resolve_op();
//cerr << exprs.size() << " " << exprs.top() << nl;
assert(exprs.size() == 1);
return exprs.top();
}
char vis[K][K][16];
bitset<1 << K> tt[1 << K];
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, k; cin >> n >> k;
vector<tuple<int, int, int, int>> exprs;
for (int i = 0; i < n; i++) {
int a, b; string expr; int r; cin >> a >> b >> expr >> r; a--; b--;
int val = 0;
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 2; y++) {
val |= (eval(expr, x, y) << (2*x+y));
}
}
//cerr << expr << " -> " << bitset<4>(val) << nl;
if (vis[a][b][val]) continue;
vis[a][b][val] = 1;
if (val != 0) {
exprs.emplace_back(a, b, val, r);
}
}
int fr; cin >> fr;
for (int x = 0; x < (1 << k); x++) {
for (int y = 0; y < (1 << k); y++) {
bool bad = 1;
for (auto [a, b, val, r] : exprs) {
int xi = (x >> a) & 1, yi = (y >> b) & 1;
if ((val >> (2*xi + yi)) & 1) {
tt[x][y] = r;
bad = 0;
break;
}
}
if (bad) tt[x][y] = fr;
}
}
// reflexive
int ans = 0;
for (int x = 0; x < (1 << k); x++) {
if (tt[x][x]) ans++;
}
cout << ans << " ";
ans = 0;
for (int x = 0; x < (1 << k); x++) {
for (int y = 0; y < (1 << k); y++) {
if (tt[x][y] && tt[y][x]) ans++;
}
}
cout << ans << " ";
ans = 0;
for (int x = 0; x < (1 << k); x++) {
auto nx = ~tt[x];
for (int y = 0; y < (1 << k); y++) {
if (!tt[x][y]) continue;
ans += (nx & tt[y]).count();
}
}
cout << ans << nl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3552kb
input:
3 2 1 1 (x=0)&(y=1) 1 1 1 (x=1)&(y=(x^x)) 0 2 2 (x=1)|(y=0) 0 1
output:
0 0 0
result:
ok single line: '0 0 0'
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 3560kb
input:
4 3 2 1 x=0&(y=1) 1 1 2 !x^!!y 0 2 3 ((x|1)=y)&1&1 1 3 1 !x&!x&!x 0 1
output:
4 16 52
result:
wrong answer 1st lines differ - expected: '3 25 52', found: '4 16 52'