QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#427564 | #8770. Comparator | ucup-team635# | AC ✓ | 475ms | 97852kb | Rust | 6.4kb | 2024-06-01 13:52:19 | 2024-06-01 13:52:20 |
Judging History
answer
use std::collections::*;
use std::io::Write;
type Map<K, V> = BTreeMap<K, V>;
type Set<T> = BTreeSet<T>;
type Deque<T> = VecDeque<T>;
fn main() {
input! {
n: usize,
k: usize,
cond: [(usize1, usize1, bytes, u8); n],
fix: u8,
}
let mut used = [[[[false; 2]; 2]; 10]; 10];
let mut memo = vec![];
for (a, b, s, r) in cond {
let mut solver = Parser {
s: s,
po: 0,
};
let mat = solver.xor();
for i in 0..2 {
for j in 0..2 {
if !used[a][b][i][j] && mat[i][j] == 1 {
used[a][b][i][j] = true;
memo.push((a, b, i, j, r));
}
}
}
}
let eval = |x: usize, y: usize| -> u8 {
for &(a, b, p, q, r) in memo.iter() {
if x >> a & 1 == p && y >> b & 1 == q {
return r;
}
}
fix
};
let w = 8 * std::mem::size_of::<usize>();
let len = ((1 << k) + w - 1) / 2;
let mut func = vec![vec![0; len]; 1 << k];
for (i, f) in func.iter_mut().enumerate() {
for j in 0..(1 << k) {
let q = j / w;
let r = j % w;
f[q] |= (eval(i, j) as usize) << r;
}
}
let at = |f: &[usize], x: usize| -> bool {
let q = x / w;
let r = x % w;
f[q] >> r & 1 == 1
};
let find = |x: usize, y: usize| -> bool {
at(&func[x], y)
};
let mut rev = vec![vec![0; len]; 1 << k];
for i in 0..(1 << k) {
for j in 0..(1 << k) {
let q = j / w;
let r = j % w;
rev[i][q] |= (find(j, i) as usize) << r;
}
}
let mut ans = (0, 0, 0);
for i in 0..(1 << k) {
if find(i, i) {
ans.0 += 1;
}
for j in 0..(1 << k) {
if find(i, j) && find(j, i) {
ans.1 += 1;
}
if !find(i, j) {
ans.2 += func[i].iter().zip(rev[j].iter()).fold(0, |s, a| s + (*a.0 & *a.1).count_ones());
}
}
}
println!("{} {} {}", ans.0, ans.1, ans.2);
}
#[derive(Debug)]
struct Parser {
s: Vec<u8>,
po: usize,
}
// [x][y] = res
impl Parser {
fn peek(&self) -> Option<u8> {
self.s.get(self.po).cloned()
}
fn eat(&mut self) -> u8 {
let c = self.peek().unwrap();
self.po += 1;
c
}
fn xor(&mut self) -> [[u8; 2]; 2] {
let mut l = self.or();
while self.peek().map_or(false, |c| c == b'^') {
self.eat();
let r = self.or();
for (l, r) in l.iter_mut().zip(r.iter()) {
for (l, r) in l.iter_mut().zip(r.iter()) {
*l ^= *r;
}
}
}
l
}
fn or(&mut self) -> [[u8; 2]; 2] {
let mut l = self.and();
while self.peek().map_or(false, |c| c == b'|') {
self.eat();
let r = self.and();
for (l, r) in l.iter_mut().zip(r.iter()) {
for (l, r) in l.iter_mut().zip(r.iter()) {
*l |= *r;
}
}
}
l
}
fn and(&mut self) -> [[u8; 2]; 2] {
let mut l = self.eq();
while self.peek().map_or(false, |c| c == b'&') {
self.eat();
let r = self.eq();
for (l, r) in l.iter_mut().zip(r.iter()) {
for (l, r) in l.iter_mut().zip(r.iter()) {
*l &= *r;
}
}
}
l
}
fn eq(&mut self) -> [[u8; 2]; 2] {
let mut l = self.not();
while self.peek().map_or(false, |c| c == b'=') {
self.eat();
let r = self.not();
for (l, r) in l.iter_mut().zip(r.iter()) {
for (l, r) in l.iter_mut().zip(r.iter()) {
*l = (*l == *r) as u8;
}
}
}
l
}
fn not(&mut self) -> [[u8; 2]; 2] {
if self.peek().unwrap() != b'!' {
return self.term();
}
let mut sign = 0;
if self.peek().unwrap() == b'!' {
self.eat();
sign = 1;
}
let mut res = self.not();
for a in res.iter_mut().flatten() {
*a ^= sign;
}
res
}
fn term(&mut self) -> [[u8; 2]; 2] {
let c = self.eat();
if c == b'0' {
[[0; 2]; 2]
} else if c == b'1' {
[[1; 2]; 2]
} else if c == b'x' {
[[0; 2], [1; 2]]
} else if c == b'y' {
[[0, 1]; 2]
} else {
assert!(c == b'(', "{}", c as char);
let res = self.xor();
let d = self.eat();
assert!(d == b')', "{}", d as char);
res
}
}
}
// ---------- begin input macro ----------
// reference: 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")
};
}
// ---------- end input macro ----------
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 2216kb
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: 2096kb
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: 3852kb
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: 18460kb
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: 19ms
memory: 97852kb
input:
1 3 1 1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!...
output:
4 16 0
result:
ok single line: '4 16 0'
Test #6:
score: 0
Accepted
time: 0ms
memory: 2252kb
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: 2060kb
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: 2120kb
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: 2132kb
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: 475ms
memory: 10784kb
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: 2224kb
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: 11ms
memory: 30336kb
input:
1 1 1 1 ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((...
output:
1 1 0
result:
ok single line: '1 1 0'
Test #13:
score: 0
Accepted
time: 273ms
memory: 20116kb
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: 2328kb
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: 2152kb
input:
0 3 1
output:
8 64 0
result:
ok single line: '8 64 0'