fn read() -> (i32, Vec) { let mut s = String::new(); use std::io::Read; std::io::stdin().read_to_string(&mut s).unwrap(); let mut it = s.trim().split_whitespace().flat_map(|s| s.parse::()); let mut next = || it.next().unwrap(); let n = next(); let k = next(); (k, (0..n).map(|_| next()).collect()) } fn calc(l: &[i32], r: &[i32], k: i32) -> usize { let mut map = std::collections::HashMap::new(); let mut min = std::i32::MAX; let mut cnt = 0; let mut x = 0; let mut ans = 0; for (i, &r) in r.iter().enumerate() { if min > r { min = r; cnt = 1; } else if min == r { cnt += 1; } while l.get(x).map_or(false, |l| *l > min) { let po = map.entry(l[x]).or_insert((0, 0)); po.0 += 1; po.1 += x + 1; x += 1; } if cnt == 1 { if let Some(po) = map.get(&(k - r)) { ans += po.0 * (i + 1) + po.1; } } } ans } fn run() { let (k, mut a) = read(); let mut ans = 0; let mut dfs = vec![a.as_mut_slice()]; while let Some(a) = dfs.pop() { if a.len() == 1 { if a[0] * 2 == k { ans += 1; } continue; } let m = a.len() / 2; let (l, r) = a.split_at_mut(m); l.reverse(); ans += calc(l, r, k) + calc(r, l, k); dfs.push(l); dfs.push(r); } println!("{}", ans); } fn main() { run(); }