QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#437908#8770. Comparatormendicillin2AC ✓35ms20188kbRust5.6kb2024-06-09 19:48:482024-06-09 19:48:49

Judging History

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

  • [2024-06-09 19:48:49]
  • 评测
  • 测评结果:AC
  • 用时:35ms
  • 内存:20188kb
  • [2024-06-09 19:48:48]
  • 提交

answer

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

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 2136kb

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: 2060kb

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: 16ms
memory: 3952kb

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: 35ms
memory: 18448kb

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: 0ms
memory: 5000kb

input:

1 3
1 1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!...

output:

4 16 0

result:

ok single line: '4 16 0'

Test #6:

score: 0
Accepted
time: 0ms
memory: 2324kb

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: 2052kb

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: 2212kb

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: 2136kb

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: 17ms
memory: 2460kb

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: 2320kb

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: 3ms
memory: 18112kb

input:

1 1
1 1 ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((...

output:

1 1 0

result:

ok single line: '1 1 0'

Test #13:

score: 0
Accepted
time: 31ms
memory: 20188kb

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: 2132kb

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: 2028kb

input:

0 3
1

output:

8 64 0

result:

ok single line: '8 64 0'