//https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 より 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_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_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::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, bytes) => { read_value!($iter, String).bytes().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // type Ref = Box; type Link = Option; const SIZE: usize = 2; const W: usize = 18; #[derive(Default)] pub struct Node { next: [Link; SIZE], cnt: usize, bit: [usize; W], } impl Node { pub fn new() -> Ref { Box::new(Node::default()) } pub fn insert(&mut self, v: usize) { let mut po = self; for i in (0..W).rev() { po.cnt += 1; for (i, b) in po.bit.iter_mut().enumerate() { *b += v >> i & 1; } let x = v >> i & 1; po = po.next[x].get_or_insert_with(Node::new); } po.cnt += 1; for (i, b) in po.bit.iter_mut().enumerate() { *b += v >> i & 1; } } pub fn find(&self, v: usize, x: usize) -> usize { let mut po = self; let mut res = 0; for i in (0..W).rev() { if x >> i & 1 == 1 { po.next[v >> i & 1].as_ref().map(|po| { for (j, b) in po.bit.iter().enumerate() { if v >> j & 1 == 1 { res += (1 << j) * po.cnt; } else { res += (1 << j) * *b; } } }); } let k = (v ^ x) >> i & 1; if let Some(p) = po.next[k].as_ref() { po = p; } else { break; } } res } } fn run() { input! { n: usize, x: usize, a: [usize; n], } let mut root = Node::new(); let mut ans = 0; for a in a { ans += root.find(a, x); root.insert(a); } println!("{}", ans); } fn main() { run(); }