Problem:
Score : 800 points

Problem Statement

Given are N pairwise distinct non-negative integers A_1,A_2,\ldots,A_N.
Find the number of ways to choose a set of between 1 and K numbers (inclusive) from the given numbers so that the following two conditions are satisfied:

The bitwise AND of the chosen numbers is S.

The bitwise OR of the chosen numbers is T.

Constraints

1 \leq N \leq 50

1 \leq K \leq N

0 \leq A_i < 2^{18}

0 \leq S < 2^{18}

0 \leq T < 2^{18}

A_i \neq A_j (1 \leq i < j \leq N)

Input

Input is given from Standard Input in the following format:

N K S T
A_1 A_2 ... A_N

Output

Print the answer.

Sample Input 1

3 3 0 3
1 2 3

Sample Output 1

2

The conditions are satisfied when we choose \{1,2\} or \{1,2,3\}.

Sample Input 2

5 3 1 7
3 4 9 1 5

Sample Output 2

2

Sample Input 3

5 4 0 15
3 4 9 1 5

Sample Output 3

3

Solution in Rust (1.42.0) (judge verdict: Wrong Answer)

#[allow(unused_imports)]
use std::cmp::{max, min};
use std::io::{stdin, Read};

#[allow(unused_macros)]
macro_rules! parse {
    ($it: ident ) => {};
    ($it: ident, ) => {};
    ($it: ident, $var:ident : $t:tt $($r:tt)*) => {
        let $var = parse_val!($it, $t);
        parse!($it $($r)*);
    };
    ($it: ident, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = parse_val!($it, $t);
        parse!($it $($r)*);
    };
}

#[allow(unused_macros)]
macro_rules! parse_val {
    ($it: ident, [$t:tt; $len:expr]) => {
        (0..$len).map(|_| parse_val!($it, $t)).collect::<Vec<_>>();
    };
    ($it: ident, ($($t: ty),*)) => {
        ($(parse_val!($it, $t)),*)
    };
    ($it: ident, u1) => {
        $it.next().unwrap().parse::<usize>().unwrap() -1
    };
    ($it: ident, $t: ty) => {
        $it.next().unwrap().parse::<$t>().unwrap()
    };
}

fn solve(s: &str) {
    let mut it = s.split_whitespace();
    parse!(it, n: usize, k: u32, s: usize, t: usize, a: [usize; n]);
    if t & s != s {
        println!("{}", 0);
        return;
    }
    let a: Vec<_> = a
        .into_iter()
        .filter(|ai| ai & s == s && ai | t == t)
        .collect();
    let n = a.len();
    let mut ret = 0;
    let dt: Vec<_> = (0..18usize)
        .filter(|i| (s >> i) & 1 == 0 && (t >> i) & 1 == 1)
        .collect();
    let m = dt.len();
    let mut targets = vec![];
    let mut csets = vec![];
    for i in 0..n {
        targets.clear();
        for &j in dt.iter() {
            targets.push((a[i] >> j) & 1)
        }

        csets.clear();
        for j in 0..m {
            let ti = dt[j];
            csets.push(0);
            for k in (i + 1)..n {
                if (a[k] >> ti) & 1 == targets[j] {
                    csets[j] |= 1 << k;
                }
            }
        }

        let mut cnum = 0i64;
        for sub in 0usize..(1 << m) {
            let mut iset = !0usize;
            for j in (0..m).filter(|j| (sub >> j) & 1 == 1) {
                iset &= csets[j];
            }
            if iset == !0usize {
                continue;
            }
            let c = sumcomb(iset.count_ones() as usize, k as usize - 1) as i64;
            if sub.count_ones() % 2 == 1 {
                cnum += c;
            } else {
                cnum -= c;
            }
        }
        cnum = max(cnum, 1);
        let a = sumcomb(n - 1 - i, (k - 1) as usize);
        ret += a - cnum as usize;
    }
    println!("{}", ret);
}
fn sumcomb(m: usize, n: usize) -> usize {
    (0..=n).map(|i| comb(m, i)).sum()
}
fn comb(m: usize, n: usize) -> usize {
    if m < n {
        return 0;
    }
    let mut ret = 1;
    for i in (m - n)..m {
        ret *= i + 1;
    }
    for i in 0..n {
        ret /= i + 1
    }
    ret
}

fn main() {
    let mut s = String::new();
    stdin().read_to_string(&mut s).unwrap();
    solve(&s);
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_input() {
        let s = "
        ";
        solve(s);
    }
}