QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#435443 | #8770. Comparator | ucup-team3691 | AC ✓ | 101ms | 43504kb | C++17 | 5.7kb | 2024-06-08 20:08:58 | 2024-06-08 20:08:59 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
/* Function definitions */
bool eval(const string& expr, const int& x, const int& y);
bool evalTerm(const string& expr, int& ptr, const int& x, const int& y);
bool evalXor(const string& expr, int& ptr, const int& x, const int& y);
bool evalOr(const string& expr, int& ptr, const int& x, const int& y);
bool evalAnd(const string& expr, int& ptr, const int& x, const int& y);
bool evalEq(const string& expr, int& ptr, const int& x, const int& y);
bool evalTerm(const string& expr, int& ptr, const int& x, const int& y) {
if(expr[ptr] == '!') {
ptr++;
bool a = evalTerm(expr, ptr, x, y);
return !a;
}
if(expr[ptr] == '(') {
ptr++;
bool a = evalXor(expr, ptr, x, y);
// cerr << "A: " << a << endl;
// cerr << x << " " << y << " " << expr << " " << ptr << endl;
assert(expr[ptr++] == ')');
return a;
}
if(expr[ptr] == 'x') { ptr++; return x; }
if(expr[ptr] == 'y') { ptr++; return y; }
if(expr[ptr] == '0') { ptr++; return 0; }
if(expr[ptr] == '1') { ptr++; return 1; }
// cerr << x << " " << y << " " << expr << " " << ptr << endl;
assert(false);
}
bool evalXor(const string& expr, int& ptr, const int& x, const int& y) {
bool ret = evalOr(expr, ptr, x, y);
while(expr[ptr] == '^') {
ptr++;
ret ^= evalOr(expr, ptr, x, y);
}
return ret;
}
bool evalOr(const string& expr, int& ptr, const int& x, const int& y) {
bool ret = evalAnd(expr, ptr, x, y);
while(expr[ptr] == '|') {
ptr++;
ret |= evalAnd(expr, ptr, x, y);
}
return ret;
}
bool evalAnd(const string& expr, int& ptr, const int& x, const int& y) {
bool ret = evalEq(expr, ptr, x, y);
while(expr[ptr] == '&') {
ptr++;
ret &= evalEq(expr, ptr, x, y);
}
return ret;
}
bool evalEq(const string& expr, int& ptr, const int& x, const int& y) {
bool ret = evalTerm(expr, ptr, x, y);
while(expr[ptr] == '=') {
ptr++;
ret = (ret == evalTerm(expr, ptr, x, y));
}
return ret;
}
bool eval(const string& expr, const int& x, const int &y) {
string to_eval = "(" + expr + ")";
int ptr = 0;
return evalTerm(to_eval, ptr, x, y);
}
struct rule {
int x, y;
int truth[2][2];
int ret;
};
rule read_rule() {
int x, y; string expr; int ret;
cin >> x >> y >> expr >> ret;
rule r;
r.x = x; r.y = y;
r.ret = ret;
r.truth[0][0] = eval(expr, 0, 0);
r.truth[0][1] = eval(expr, 0, 1);
r.truth[1][0] = eval(expr, 1, 0);
r.truth[1][1] = eval(expr, 1, 1);
/* cerr << "Rule: " << x << " " << y << " " << expr << " " << ret << endl;
cerr << 0 << " " << 0 << " " << eval(expr, 0, 0) << endl;
cerr << 0 << " " << 1 << " " << eval(expr, 0, 1) << endl;
cerr << 1 << " " << 0 << " " << eval(expr, 1, 0) << endl;
cerr << 1 << " " << 1 << " " << eval(expr, 1, 1) << endl; */
return r;
}
pair<int, vector<rule>> read_rules() {
int n, k; cin >> n >> k;
vector<rule> rules;
for(int i = 0; i < n; i++) {
rules.push_back(read_rule());
}
int final_ret; cin >> final_ret;
rule final_rule;
final_rule.x = 1; final_rule.y = 1;
final_rule.ret = final_ret;
final_rule.truth[0][0] = final_rule.truth[0][1] = final_rule.truth[1][0] = final_rule.truth[1][1] = true;
rules.push_back(final_rule);
return {k, rules};
}
const int kmax = 10;
bitset<(1 << kmax)> f[(1 << kmax)];
bitset<(1 << kmax)> ftrans[(1 << kmax)];
void fill_by_mask(vector<int>& mask_a, vector<int>& mask_b, int ret, int bit_index = 0, int a = 0, int b = 0) {
if(bit_index == mask_a.size()) {
f[a][b] = ret;
ftrans[b][a] = ret;
return;
}
int ab = mask_a[bit_index];
int bb = mask_b[bit_index];
for(int i = 0; i <= 1; i++) {
for(int j = 0; j <= 1; j++) {
if(mask_a[bit_index] != -1 && mask_a[bit_index] != i) continue;
if(mask_b[bit_index] != -1 && mask_b[bit_index] != j) continue;
mask_a[bit_index] = i;
mask_b[bit_index] = j;
fill_by_mask(mask_a, mask_b, ret, bit_index + 1, a + (i << bit_index), b + (j << bit_index));
mask_a[bit_index] = ab;
mask_b[bit_index] = bb;
}
}
mask_a[bit_index] = ab;
mask_b[bit_index] = bb;
}
void fill_f(vector<int>& mask_a, vector<int>& mask_b, const vector<rule>& rules, int cnt_rule) {
// cerr << rules.size() << " " << cnt_rule << endl;
// this_thread::sleep_for(0.3s);
auto r = rules[cnt_rule];
int x = r.x;
int y = r.y;
int ax = mask_a[x - 1];
int by = mask_b[y - 1];
for(int i = 0; i <= 1; i++) {
for(int j = 0; j <= 1; j++) {
if(ax != -1 && ax != i) continue;
if(by != -1 && by != j) continue;
if(r.truth[i][j] == true) {
mask_a[x - 1] = i;
mask_b[y - 1] = j;
fill_by_mask(mask_a, mask_b, r.ret);
}
else {
mask_a[x - 1] = i;
mask_b[y - 1] = j;
fill_f(mask_a, mask_b, rules, cnt_rule + 1);
}
mask_a[x - 1] = ax;
mask_b[y - 1] = by;
}
}
mask_a[x - 1] = ax;
mask_b[y - 1] = by;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
auto [k, rules] = read_rules();
vector<int> mask_a(k, -1), mask_b(k, -1);
fill_f(mask_a, mask_b, rules, 0);
/* for(int i = 0; i < (1 << k); i++) {
for(int j = 0; j < (1 << k); j++) {
cout << bitset<10>(i) << " " << bitset<10>(j) << " " << f[i][j] << endl;
}
cout << endl;
} */
int refl = 0;
for(int x = 0; x < (1 << k); x++) {
if(f[x][x] == 1) refl++;
}
int symm = 0;
for(int x = 0; x < (1 << k); x++) {
for(int y = 0; y < (1 << k); y++) {
if(f[x][y] == 1) symm += (f[y][x] == 1);
}
}
int tran = 0;
for(int x = 0; x < (1 << k); x++) {
for(int z = 0; z < (1 << k); z++) {
if(f[x][z] == 0) {
bitset<(1 << kmax)> b;
b = f[x] & ftrans[z];
tran += b.count();
}
}
}
cout << refl << " " << symm << " " << tran << endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3664kb
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: 3664kb
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: 30ms
memory: 3744kb
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: 84ms
memory: 13364kb
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: 52ms
memory: 37220kb
input:
1 3 1 1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!...
output:
4 16 0
result:
ok single line: '4 16 0'
Test #6:
score: 0
Accepted
time: 0ms
memory: 3588kb
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: 3856kb
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: 3636kb
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: 3568kb
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: 63ms
memory: 4252kb
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: 3568kb
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: 20ms
memory: 30584kb
input:
1 1 1 1 ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((...
output:
1 1 0
result:
ok single line: '1 1 0'
Test #13:
score: 0
Accepted
time: 101ms
memory: 43504kb
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: 3620kb
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: 3632kb
input:
0 3 1
output:
8 64 0
result:
ok single line: '8 64 0'