QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#437906#8770. Comparatormendicillin2Compile Error//C++205.6kb2024-06-09 19:47:032024-06-09 19:47:03

Judging History

你现在查看的是最新测评结果

  • [2024-06-09 19:47:03]
  • 评测
  • [2024-06-09 19:47:03]
  • 提交

answer

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!();
    }
}

Details

answer.code:16:19: error: too many decimal points in number
   16 |         for bx in 0..2 {
      |                   ^~~~
answer.code:17:23: error: too many decimal points in number
   17 |             for by in 0..2 {
      |                       ^~~~
answer.code:47:18: error: too many decimal points in number
   47 |         for y in 0..m {
      |                  ^~~~
answer.code:58:14: error: too many decimal points in number
   58 |     for x in 0..(1 << k) {
      |              ^~~
answer.code:62:18: error: too many decimal points in number
   62 |         for y in 0..(1 << k) {
      |                  ^~~
answer.code:77:2: error: invalid preprocessing directive #[
   77 | #[derive(Debug)]
      |  ^
answer.code:159:2: error: invalid preprocessing directive #[
  159 | #[macro_export]
      |  ^
answer.code:177:2: error: invalid preprocessing directive #[
  177 | #[macro_export]
      |  ^
answer.code:187:2: error: invalid preprocessing directive #[
  187 | #[macro_export]
      |  ^
answer.code:193:10: error: too many decimal points in number
  193 |         (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
      |          ^~~~~~~
answer.code:210:2: error: invalid preprocessing directive #[
  210 | #[cfg(test)]
      |  ^
answer.code:214:6: error: invalid preprocessing directive #[
  214 |     #[test]
      |      ^
answer.code:1:1: error: ‘use’ does not name a type
    1 | use core::panic;
      | ^~~
answer.code:3:1: error: ‘fn’ does not name a type
    3 | fn main() {
      | ^~
answer.code:79:5: error: ‘expr’ does not name a type
   79 |     expr: Vec<u8>,
      |     ^~~~
answer.code:84:6: error: expected initializer before ‘Evaluator’
   84 | impl Evaluator {
      |      ^~~~~~~~~
answer.code:160:1: error: ‘macro_rules’ does not name a type
  160 | macro_rules! input {
      | ^~~~~~~~~~~
answer.code:178:1: error: ‘macro_rules’ does not name a type
  178 | macro_rules! input_inner {
      | ^~~~~~~~~~~
answer.code:188:1: error: ‘macro_rules’ does not name a type
  188 | macro_rules! read_value {
      | ^~~~~~~~~~~
answer.code:211:1: error: ‘mod’ does not name a type
  211 | mod tests {
      | ^~~