QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#681437 | #8770. Comparator | fwitt | AC ✓ | 395ms | 35632kb | C++20 | 4.8kb | 2024-10-27 09:23:00 | 2024-10-27 09:23:00 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) begin(x), end(x)
#define sz(x) int((x).size())
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
#ifdef LOCAL
auto operator<<(auto& o, auto x) -> decltype(x.first, o);
auto operator<<(auto& o, auto x) -> decltype(x.end(), o) {
o << "{";
for (int i = 0; auto y : x) o << ", " + !i++ * 2 << y;
return o << "}";
}
auto operator<<(auto& o, auto x) -> decltype(x.first, o) {
return o << "(" << x.first << ", " << x.second << ")";
}
void __print(auto... x) { ((cerr << x << " "), ...) << endl; }
#define debug(x...) __print("[" #x "]:", x)
#else
#define debug(...) 2137
#endif
vector<int> is_op;
vector<int> is_lit;
vector<int> lit_val;
pair<int, int> parse_multiple(const string &s, int pos);
// gdzie skonczyl
pair<int, int> parse_one(const string &s, const int pos) {
if(is_lit[s[pos]]) {
return {pos + 1, lit_val[s[pos]]};
}
if(s[pos] == '(') {
auto [epos, ret] = parse_multiple(s, pos + 1);
assert(s[epos] == ')');
return {epos + 1, ret};
}
if(s[pos] == '!') {
auto [epos, ret] = parse_one(s, pos+ 1);
return {epos, ret ^ 0b1111};
}
assert(false);
}
using f_t = function<int(int, int)>;
vector<pair<char, f_t>> ops;
int eq(int a, int b) {return (~(a^b)) & 0b1111;}
int an(int a, int b) {return a & b;}
int rr(int a, int b) {return a | b;}
int xr(int a, int b) {return a ^ b;}
pair<int, int> parse_multiple(const string &s, const int pos) {
vector<int> v;
int ptr = pos;
while(ptr < sz(s) && s[ptr] != ')') {
if(is_op[s[ptr]]) {
v.emplace_back(s[ptr++]);
continue;
}
else {
auto [epos, val] = parse_one(s, ptr);
ptr = epos;
v.emplace_back(val);
}
}
for(auto [id, op] : ops) {
int i = 0;
vector<int> vv;
while(i < sz(v)) {
if(v[i] == id) {
auto val = vv.back();
vv.pop_back();
vv.push_back(op(val, v[i + 1]));
i += 2;
continue;
}
if(is_op[v[i]]) {
vv.push_back(v[i++]);
continue;
}
assert(v[i] <= 0b1111);
vv.push_back(v[i]);
i += 1;
}
v = vv;
}
return {ptr, v.back()};
}
int main() {
cin.tie(0)->sync_with_stdio(0);
ops = {
{'=', eq},
{'&', an},
{'|', rr},
{'^', xr},
};
is_op.resize(300);
is_lit.resize(300);
lit_val.resize(300);
for(auto i : vector<char>{'=', '&', '|', '^'})
is_op[i] = 1;
for(auto i : vector<char>{'x', 'y', '0', '1'})
is_lit[i] = 1;
lit_val['0'] = 0b0000;
lit_val['1'] = 0b1111;
lit_val['x'] = 0b0011;
lit_val['y'] = 0b0101;
int n, k;
cin >> n >> k;
const int M = (1 << (k + k));
vector d00(M, n + 1), d01(M, n + 1), d10(M, n + 1), d11(M, n + 1);
vector<int> va(n);
for(int _ = 0; _< n; _++) {
int a, b;
cin >> a >> b;
--a;
--b;
string s;
cin >> s;
cin >> va[_];
auto rr = parse_multiple(s, 0);
int result = rr.second;
auto inn = [&] (int &x, int y) {
x = min(x, y);
};
debug(s, result);
const int mm = (1 << (a + k)) ^ (1 << b);
debug(mm);
if(result & 1)
inn(d11[mm], _);
if(result & 2)
inn(d10[mm], _);
if(result & 4)
inn(d01[mm], _);
if(result & 8)
inn(d00[mm], _);
}
auto fi = [&] (vector<int> &a) {
rep(i, 0, M) {
rep(j, 0, 2 * k) {
if(i & (1 << j)) {
a[i] = min(a[i], a[i ^ (1 << j)]);
}
}
}
};
int pad;
cin >> pad;
fi(d00);
fi(d01);
fi(d10);
fi(d11);
const int hm = (1 << k) - 1;
using B = bitset<1024>;
debug(k, M);
vector<B> ve(1 << k, B());
rep(i, 0, 1 << k) {
rep(j, 0, 1 << k) {
int ve0 = (d00[(i << k) | j]);
int ve1 = (d01[(i << k) | (j ^ hm)]);
int ve2 = (d10[((i ^ hm) << k) | j]);
int ve3 = (d11[((i ^ hm) << k) | (j ^ hm)]);
int mn = min({ve0, ve1, ve2, ve3});
if(mn != n + 1)
ve[i][j] = va[mn];
else
ve[i][j] = pad;
debug(i, j, mn, ve[i][j]);
debug(ve0, ve1, ve2, ve3);
}
}
// rep(i, 0, 1 << k) {
// rep(j, 0, 1 << k)
// cerr << ve[i][j] << " ";
// cerr << endl;
// }
int c0 = 0;
rep(i, 0, 1 << k) {
if(ve[i][i])
++c0;
}
int c1 = 0;
rep(i, 0, 1 << k) {
rep(j, 0, 1 << k) {
if(ve[i][j] && ve[j][i])
++c1;
}
}
int c2 = 0;
rep(i, 0, 1 << k) {
rep(j, 0, 1 << k) {
if(ve[i][j]) {
int add = (~ve[i] & ve[j]).count();
c2 += add;
debug(i, j, c2, ve[i] == ve[j]);
}
}
}
cout << c0 << " " << c1 << " " << c2 << endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3796kb
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: 3860kb
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: 47ms
memory: 3716kb
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: 395ms
memory: 20524kb
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: 7ms
memory: 35632kb
input:
1 3 1 1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!...
output:
4 16 0
result:
ok single line: '4 16 0'
Test #6:
score: 0
Accepted
time: 0ms
memory: 3816kb
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: 3640kb
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: 3852kb
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: 3572kb
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: 252ms
memory: 19832kb
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: 3796kb
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: 12ms
memory: 33640kb
input:
1 1 1 1 ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((...
output:
1 1 0
result:
ok single line: '1 1 0'
Test #13:
score: 0
Accepted
time: 355ms
memory: 20696kb
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: 3828kb
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: 3628kb
input:
0 3 1
output:
8 64 0
result:
ok single line: '8 64 0'