QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#437711 | #8770. Comparator | mendicillin2 | WA | 53ms | 114664kb | C++20 | 4.5kb | 2024-06-09 16:09:57 | 2024-06-09 16:09:57 |
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>>;
const string BINOPS = "^|&=";
int op(int p, int x, int y) {
if (p == 0) return x ^ y;
else if (p == 1) return x | y;
else if (p == 2) return x & y;
else if (p == 3) return 15 ^ x ^ y;
assert(false);
}
int evaluate(string expr) {
expr.push_back(char(0));
int cur = 0;
auto evaluate = [&](auto&& self, int p) -> int {
if (p < 4) {
// p-th binary operator
int res = self(self, p+1);
while (expr[cur] == BINOPS[p]) {
cur++;
auto rres = self(self, p+1);
res = op(p, res, rres);
}
return res;
} else if (p == 4) {
char c = expr[cur++];
if (c == 'x') {
return (1 << 1) | (1 << 3);
} else if (c == 'y') {
return (1 << 2) | (1 << 3);
} else if (c == '0') {
return 0;
} else if (c == '1') {
return 15;
} else if (c == '!') {
return 15 ^ self(self, 4);
} else if (c == '(') {
int res = self(self, 0);
cur++; // Skip ')'
return res;
} else {
cerr << "Unexpected character: " << c << endl;
assert(false);
}
} else assert(false);
};
return evaluate(evaluate, 0);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(20);
int N, K;
cin >> N >> K;
using Map = array<int, 5>; // (a, b, x, y, r)
auto encode = [&](int a, int b, int x, int y) -> int {
return a * (K*2*2)
+ b * (2*2)
+ x * 2
+ y;
};
V<bool> seen(K*K*2*2, false);
V<Map> interesting;
for (int i = 0; i < N; i++) {
int a, b, r;
string expr;
cin >> a >> b >> expr >> r;
a--, b--;
auto result = evaluate(expr);
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 2; y++) {
int result_xy = (result >> (x + 2*y)) & 1;
if (result_xy) {
int e = encode(a, b, x, y);
if (!seen[e]) {
seen[e] = true;
interesting.push_back(Map{a, b, x, y, r});
}
}
}
}
}
int R_end;
cin >> R_end;
auto compute_f = [&](int x, int y) -> int {
for (const auto& [a, b, dx, dy, r] : interesting) {
if (((x >> a) & 1) == dx && ((y >> b) & 1) == dy) {
return r;
}
}
return R_end;
};
using Word = uint64_t;
constexpr int Width = 8 * sizeof(Word);
const int M = 1 << K;
const int num_blocks = (M + Width - 1) / Width;
VV<Word> fxy(M, V<Word>(num_blocks));
VV<Word> fyx(M, V<Word>(num_blocks));
for (int x = 0; x < M; x++) {
for (int y = 0; y < M; y++) {
auto f = compute_f(x, y);
if (f) {
{
int q = y / Width;
int r = y % Width;
fxy[x][q] |= 1 << r;
}
{
int q = x / Width;
int r = x % Width;
fyx[y][q] |= 1 << r;
}
}
}
}
auto at = [&](int x, int y) -> bool {
int q = y / Width;
int r = y % Width;
return bool((fxy[x][q] >> r) & 1);
};
cout << [&]() -> int {
return ranges::count_if(ranges::iota_view{0, M}, [&](int x) -> bool {
return at(x, x);
});
}() << ' ' << [&]() -> int {
int cnt = 0;
for (int x = 0; x < M; x++) {
for (int y = 0; y < M; y++) {
if (at(x, y) && at(y, x)) {
cnt++;
}
}
}
return cnt;
}() << ' ' << [&]() -> int {
int cnt = 0;
for (int x = 0; x < M; x++) {
for (int z = 0; z < M; z++) {
if (at(x, z)) continue;
for (int i = 0; i < num_blocks; i++) {
cnt += __builtin_popcount(fxy[x][i] & fyx[z][i]);
}
}
}
return cnt;
}() << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3548kb
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: 1ms
memory: 3612kb
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: 12ms
memory: 3704kb
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: 44ms
memory: 3952kb
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: 19ms
memory: 114664kb
input:
1 3 1 1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!...
output:
4 16 0
result:
ok single line: '4 16 0'
Test #6:
score: 0
Accepted
time: 0ms
memory: 3604kb
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: 3520kb
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: 3648kb
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: 1ms
memory: 3528kb
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: 43ms
memory: 3872kb
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: 1ms
memory: 3604kb
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: 58636kb
input:
1 1 1 1 ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((...
output:
1 1 0
result:
ok single line: '1 1 0'
Test #13:
score: -100
Wrong Answer
time: 53ms
memory: 3972kb
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 67108864
result:
wrong answer 1st lines differ - expected: '512 262144 134217728', found: '512 262144 67108864'