use core::panic;
fn main() {
input! {
n: usize,
k: usize,
conds: [(usize1, usize1, bytes, u8); n],
r_otherwise: u8
}
let mut seen = vec![false; k * k * 2 * 2];
let mut interesting = vec![];
for (a, b, expr, r) in conds {
let mut eval = Evaluator::new(expr);
let result = eval.evaluate();
for bx in 0..2 {
for by in 0..2 {
if result >> (bx + 2 * by) & 1 == 0 {
continue;
}
let e = a * (k * 2 * 2) + b * (2 * 2) + bx * 2 + by;
if !seen[e] {
seen[e] = true;
interesting.push((a, b, bx, by, r))
}
}
}
}
let compute_f = |x: usize, y: usize| -> u8 {
for &(a, b, bx, by, r) in interesting.iter() {
if x >> a & 1 == bx && y >> b & 1 == by {
return r;
}
}
r_otherwise
};
type Word = usize;
let width = 8 * std::mem::size_of::<Word>();
let m = 1 << k;
let num_words = (m + width - 1) / width;
let mut fxy = vec![vec![0; num_words]; m];
let mut fyx = vec![vec![0; num_words]; m];
for (x, f) in fxy.iter_mut().enumerate() {
for y in 0..m {
if compute_f(x, y) == 1 {
f[y / width] |= (1 as usize) << (y % width);
fyx[y][x / width] |= (1 as usize) << (x % width);
}
}
}
let at = |x: usize, y: usize| -> bool { fxy[x][y / width] >> (y % width) & 1 == 1 };
let mut ans = (0, 0, 0);
for x in 0..(1 << k) {
if at(x, x) {
ans.0 += 1;
}
for y in 0..(1 << k) {
if at(x, y) && at(y, x) {
ans.1 += 1;
}
if !at(x, y) {
ans.2 += fxy[x]
.iter()
.zip(fyx[y].iter())
.fold(0, |s, a| s + (*a.0 & *a.1).count_ones());
}
}
}
println!("{} {} {}", ans.0, ans.1, ans.2);
}
#[derive(Debug)]
struct Evaluator {
expr: Vec<u8>,
cursor: usize,
}
// Reference: https://qoj.ac/submission/427564
impl Evaluator {
fn peek(&self) -> Option<u8> {
self.expr.get(self.cursor).cloned()
}
fn eat(&mut self) -> u8 {
let c = self.peek().unwrap();
self.cursor += 1;
c
}
fn evaluate(&mut self) -> u8 {
self.do_xor()
}
fn do_xor(&mut self) -> u8 {
let mut result = self.do_or();
while self.peek().map_or(false, |c| c == b'^') {
self.eat();
result = result ^ self.do_or();
}
result
}
fn do_or(&mut self) -> u8 {
let mut result = self.do_and();
while self.peek().map_or(false, |c| c == b'|') {
self.eat();
result = result | self.do_and();
}
result
}
fn do_and(&mut self) -> u8 {
let mut result = self.do_eq();
while self.peek().map_or(false, |c| c == b'&') {
self.eat();
result = result & self.do_eq();
}
result
}
fn do_eq(&mut self) -> u8 {
let mut result = self.do_term();
while self.peek().map_or(false, |c| c == b'=') {
self.eat();
result = 15 ^ result ^ self.do_term();
}
result
}
fn do_term(&mut self) -> u8 {
let c = self.eat();
let result: u8;
match c {
b'(' => {
result = self.evaluate();
let d = self.eat();
assert_eq!(d, b')');
}
b'!' => result = 15 ^ self.do_term(),
b'0' => result = 0,
b'1' => result = 15,
b'x' => result = (1 << 1) | (1 << 3),
b'y' => result = (1 << 2) | (1 << 3),
_ => panic!("Unexpected character: {}", c as char),
}
result
}
fn new(expr: Vec<u8>) -> Self {
Self { expr, cursor: 0 }
}
}
// Copied: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[macro_export]
macro_rules! input {
(source = $s:expr, $($r:tt)*) => {
let mut iter = $s.split_whitespace();
input_inner!{iter, $($r)*}
};
($($r:tt)*) => {
let s = {
use std::io::Read;
let mut s = String::new();
std::io::stdin().read_to_string(&mut s).unwrap();
s
};
let mut iter = s.split_whitespace();
input_inner!{iter, $($r)*}
};
}
#[macro_export]
macro_rules! input_inner {
($iter:expr) => {};
($iter:expr, ) => {};
($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
let $var = read_value!($iter, $t);
input_inner!{$iter $($r)*}
};
}
#[macro_export]
macro_rules! read_value {
($iter:expr, ( $($t:tt),* )) => {
( $(read_value!($iter, $t)),* )
};
($iter:expr, [ $t:tt ; $len:expr ]) => {
(0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
};
($iter:expr, chars) => {
read_value!($iter, String).chars().collect::<Vec<char>>()
};
($iter:expr, bytes) => {
read_value!($iter, String).bytes().collect::<Vec<u8>>()
};
($iter:expr, usize1) => {
read_value!($iter, usize) - 1
};
($iter:expr, $t:ty) => {
$iter.next().unwrap().parse::<$t>().expect("Parse error")
};
}
// Tests
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_eval() {
todo!();
}
}