QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#434829 | #8770. Comparator | mendicillin2# | AC ✓ | 180ms | 9076kb | C++20 | 4.1kb | 2024-06-08 17:34:54 | 2024-06-08 17:35:01 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
int get_priority(char c) {
if (c == '(') {
return 0;
} else if (c == '!') {
return 5;
} else if (c == '=') {
return 4;
} else if (c == '&') {
return 3;
} else if (c == '|') {
return 2;
} else if (c == '^') {
return 1;
} else {
return 6;
}
}
bool is_operand(char c) {
const string OPR = "01xy";
return OPR.find(c) != string::npos;
}
bool is_operator(char c) {
const string OP = "!=&|^";
return OP.find(c) != string::npos;
}
int evaluate(string expr, int x, int y) {
string suf_expr;
{
V<char> stk;
for (char c : expr) {
if (is_operand(c) || c == '(') {
stk.push_back(c);
} else if (c == ')') {
while (true) {
assert(!stk.empty());
char b = stk.back();
stk.pop_back();
if (b == '(') break;
suf_expr.push_back(b);
}
} else {
assert(is_operator(c));
// Operator
while (true) {
if (stk.empty()) break;
char b = stk.back();
if (get_priority(c) >= get_priority(b)) break;
suf_expr += b;
stk.pop_back();
}
stk.push_back(c);
}
}
while (true) {
if (stk.empty()) break;
suf_expr.push_back(stk.back());
stk.pop_back();
}
}
V<int> stk;
for (char c : suf_expr) {
if (c == '0') {
stk.push_back(0);
} else if (c == '1') {
stk.push_back(1);
} else if (c == 'x') {
stk.push_back(x);
} else if (c == 'y') {
stk.push_back(y);
} else if (c == '!') {
assert(!stk.empty());
// Negate
stk.back() ^= 1;
} else {
// Binary operators
assert(stk.size() >= 2);
int a = stk.back(); stk.pop_back();
int b = stk.back(); stk.pop_back();
int result;
if (c == '=') {
result = (a == b);
} else if (c == '&') {
result = (a & b);
} else if (c == '|') {
result = (a | b);
} else if (c == '^') {
result = (a ^ b);
} else assert(false);
stk.push_back(result);
}
}
assert(stk.size() == 1);
return stk[0];
}
const int MAXK = 10;
using Bitset = bitset<1 << MAXK>;
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
{
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 2; y++) {
assert(evaluate("(x=0)&(y=1)", x, y) == ((x==0)&&(y==1)));
assert(evaluate("(x=1)&(y=(x^x))", x, y) == ((x==1)&&(y==(x^x))));
assert(evaluate("(x=1)|(y=0)", x, y) == ((x==1)||(y==0)));
assert(evaluate("x=0&(y=1)", x, y) == ((x==0)&&(y==1)));
assert(evaluate("!x^!!y", x, y) == ((!x)^y));
assert(evaluate("((x|1)=y)&1&1", x, y) == y);
assert(evaluate("!x&!x&!x", x, y) == (!x));
}
}
}
int N, K;
cin >> N >> K;
using Input = array<int, 4>;
auto encode = [&](int a, int b, int x, int y) -> int {
assert(0 <= a && a < MAXK);
assert(0 <= b && b < MAXK);
return a * (MAXK * 2 * 2)
+ b * (2 * 2)
+ x * 2
+ y;
};
V<bool> seen(MAXK * MAXK * 2 * 2);
V<pair<Input, int>> interesting;
for (int i = 0; i < N; i++) {
int a, b, r;
string expr;
cin >> a >> b >> expr >> r;
a--, b--;
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 2; y++) {
if (evaluate(expr, x, y) == 1) {
int e = encode(a, b, x, y);
if (!seen[e]) {
seen[e] = true;
interesting.emplace_back(Input{a, b, x, y}, r);
}
}
}
}
}
int R_except;
cin >> R_except;
auto F = [&](int x, int y) -> int {
for (auto [inp, r] : interesting) {
if (((x >> inp[0]) & 1) == inp[2] && ((y >> inp[1]) & 1) == inp[3]) {
return r;
}
}
return R_except;
};
V<Bitset> fxy(1 << K);
V<Bitset> fyx(1 << K);
for (int x = 0; x < (1 << K); x++) {
for (int y = 0; y < (1 << K); y++) {
int result = F(x, y);
fxy[x][y] = fyx[y][x] = result;
}
}
array<int, 3> ans{};
for (int x = 0; x < (1 << K); x++) {
if (fxy[x][x]) ans[0]++;
}
for (int x = 0; x < (1 << K); x++) {
for (int y = 0; y < (1 << K); y++) {
if (fxy[x][y] && fxy[y][x]) ans[1]++;
}
}
for (int x = 0; x < (1 << K); x++) {
for (int z = 0; z < (1 << K); z++) {
if (!fxy[x][z]) {
ans[2] += int((fxy[x] & fyx[z]).count());
}
}
}
cout << ans[0] << ' ' << ans[1] << ' ' << ans[2] << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3592kb
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: 3624kb
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: 90ms
memory: 3644kb
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: 164ms
memory: 3596kb
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: 94ms
memory: 9076kb
input:
1 3 1 1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!...
output:
4 16 0
result:
ok single line: '4 16 0'
Test #6:
score: 0
Accepted
time: 0ms
memory: 3592kb
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: 3508kb
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: 3872kb
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: 3824kb
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: 62ms
memory: 3932kb
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: 3620kb
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: 8ms
memory: 3988kb
input:
1 1 1 1 ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((...
output:
1 1 0
result:
ok single line: '1 1 0'
Test #13:
score: 0
Accepted
time: 180ms
memory: 3516kb
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: 3596kb
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: 3524kb
input:
0 3 1
output:
8 64 0
result:
ok single line: '8 64 0'