QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#435369 | #8770. Comparator | ucup-team3691# | RE | 0ms | 3568kb | C++17 | 5.5kb | 2024-06-08 19:51:25 | 2024-06-08 19:51:26 |
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 &= evalTerm(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);
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) {
if(bit_index == mask_a.size()) {
int a = 0, b = 0;
for(int i = 0; i < mask_a.size(); i++) {
a |= (mask_a[i] << i);
b |= (mask_b[i] << i);
}
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);
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: 3512kb
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: 3568kb
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: -100
Runtime Error
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...