use std::io::Read; use std::collections::HashMap; fn main() { let mut all_data = String::new(); std::io::stdin().read_to_string(&mut all_data).ok(); let mut all_data = all_data.trim().split('\n').map(|s| s.trim()); let nmk: Vec = all_data.next().unwrap().split_whitespace().map(|s| s.parse().unwrap()).collect(); let n: usize = nmk[0]; let k: usize = nmk[2]; let opb: Vec<&str> = all_data.next().unwrap().split_whitespace().collect(); let mut bval2count: HashMap = HashMap::new(); opb.iter().skip(1).map(|s| s.parse::().unwrap()).for_each(|i| { let target = i % k; if let Some(x) = bval2count.get_mut(&target) { *x += 1; } else { bval2count.insert(target, 1); } }); let op: &str = opb[0]; let mut aval2count: HashMap = HashMap::new(); all_data.take(n).map(|s| s.parse::().unwrap()).for_each(|i| { let target = i % k; if let Some(x) = aval2count.get_mut(&target) { *x += 1; } else { aval2count.insert(target, 1); } }); let mut result: usize = 0; for (aval, acount) in aval2count.iter() { for (bval, bcount) in bval2count.iter() { let val = match op { "+" => (aval + bval) % k, "*" => (aval * bval) % k, _ => 0 }; if val == 0 { result += acount * bcount; } } } println!("{}", result); }