QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#451314 | #8770. Comparator | henryx# | AC ✓ | 158ms | 4304kb | C++20 | 3.6kb | 2024-06-23 05:32:11 | 2024-06-23 05:32:12 |
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 {
bool bad = 0;
while (!ops.empty() && ops.top() != '(' && pri.at(ops.top()) >= pri.at(c)) {
if (ops.top() == '!' && c == '!') {
bad = 1;
ops.pop();
continue;
}
resolve_op();
}
if (!bad) {
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: 3576kb
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: 0
Accepted
time: 0ms
memory: 3812kb
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:
3 25 52
result:
ok single line: '3 25 52'
Test #3:
score: 0
Accepted
time: 49ms
memory: 3832kb
input:
1413 3 1 3 0 0 3 3 !x 0 2 2 x=0 1 1 2 !y^0 1 2 3 (x^1) 0 3 2 ((!0)) 1 1 1 !!1=(y) 0 2 2 !(1^x)&y 1 3 2 (y)&1|!!1 0 3 1 !x=(y&y=y) 0 2 1 (((!1)^!x)) 1 2 3 !0=(0&y)=1&y 0 1 2 ((((!0)))|!1) 0 3 1 !(y=!1=x|(!x)) 0 1 1 ((((y=!y)))&!0) 0 2 3 ((y=1)^!1^!!1|0) 1 2 3 1|(!x)&!x|1|(x=1) 1 2 3 !0^!!!!y&0=(!1&!0...
output:
4 16 0
result:
ok single line: '4 16 0'
Test #4:
score: 0
Accepted
time: 158ms
memory: 3692kb
input:
181737 10 5 2 1 1 1 10 !1=!x 0 10 1 (1^x) 0 2 4 !1 1 10 8 y=(!1)^1 1 6 2 !((x&!x)) 1 1 10 !!((!x)|x) 1 7 10 (((0))) 0 7 3 !(1)^!x 0 10 4 (!1)&x 0 7 7 !y&!0 1 8 8 !1=(x)|1^1 1 2 6 ((!!!x)) 0 7 2 1 1 2 2 y=1=0 0 6 3 (!0) 0 6 4 0 0 1 1 (!1) 1 1 8 y 1 3 5 !x|!x^!1 0 4 7 (!0) 0 3 4 !1&1&!1|!0 1 2 7 ((0|1...
output:
1024 1048576 0
result:
ok single line: '1024 1048576 0'
Test #5:
score: 0
Accepted
time: 20ms
memory: 4304kb
input:
1 3 1 1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!...
output:
4 16 0
result:
ok single line: '4 16 0'
Test #6:
score: 0
Accepted
time: 0ms
memory: 3620kb
input:
1 1 1 1 x^y|1 0 1
output:
1 1 0
result:
ok single line: '1 1 0'
Test #7:
score: 0
Accepted
time: 0ms
memory: 3476kb
input:
1 1 1 1 x&y|1 0 1
output:
0 0 0
result:
ok single line: '0 0 0'
Test #8:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
1 1 1 1 x=y|1 0 1
output:
0 0 0
result:
ok single line: '0 0 0'
Test #9:
score: 0
Accepted
time: 0ms
memory: 3744kb
input:
2 2 1 2 !x&!y 1 1 1 !x&y 0 1
output:
4 12 2
result:
ok single line: '4 12 2'
Test #10:
score: 0
Accepted
time: 12ms
memory: 4000kb
input:
2 10 9 8 ((((!((!x=1))^(!1&(x|x|!y))&((!y&!x=(x=y)))&!((((x=1))&(0=(y))^(!!(!!x^1=x)&(x)^y&1=!x&1=(((!0^(1)^1))^!(((((y))|x|!y))))^!!0=!y&(0)|(y=x&!y&y=x)))=((((!!y&!!0|!0^!0)=!x))))^0&(((!1=!(!x)))|(((((x=1|!y|y)=(!1^1^0^(0)))=!0^1)))=(!(!1^(((((!1)^!x^0))))=(1)^((((y=((x))|(0)|(!1^1)))|(!!!y))=((!...
output:
0 0 0
result:
ok single line: '0 0 0'
Test #11:
score: 0
Accepted
time: 0ms
memory: 3488kb
input:
4 3 1 1 !!!!!!x 0 2 1 !!y 1 1 2 !!!!!x 1 2 2 !!!x 0 1
output:
4 16 0
result:
ok single line: '4 16 0'
Test #12:
score: 0
Accepted
time: 2ms
memory: 3908kb
input:
1 1 1 1 ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((...
output:
1 1 0
result:
ok single line: '1 1 0'
Test #13:
score: 0
Accepted
time: 122ms
memory: 3744kb
input:
200000 10 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10...
output:
512 262144 134217728
result:
ok single line: '512 262144 134217728'
Test #14:
score: 0
Accepted
time: 0ms
memory: 3808kb
input:
10 3 3 1 (!x) 1 3 2 !1&x&1&!y 1 2 1 ((x)&y) 1 1 3 0 0 2 2 !1&0=y&0 1 3 3 (!y)^y 1 2 1 0|((!y)) 0 3 2 x 0 2 2 (y|1^x) 0 2 1 ((!0)|y) 0 1
output:
8 64 0
result:
ok single line: '8 64 0'
Test #15:
score: 0
Accepted
time: 0ms
memory: 3560kb
input:
0 3 1
output:
8 64 0
result:
ok single line: '8 64 0'