use std::io::Read; fn run_length_encoding(a: &[T]) -> Vec<(T, usize)> { if a.len() == 0 { return vec![]; } let mut x = a[0].clone(); let mut cnt = 1; let mut ans = vec![]; for y in &a[1..] { if x == *y { cnt += 1; } else { ans.push((x, cnt)); x = y.clone(); cnt = 1; } } ans.push((x, cnt)); ans } fn gcd(a: u64, b: u64) -> u64 { if b == 0 { a } else { gcd(b, a % b) } } fn run() { let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); let mut it = s.trim().split_whitespace(); let h: usize = it.next().unwrap().parse().unwrap(); let w: usize = it.next().unwrap().parse().unwrap(); let k: u64 = it.next().unwrap().parse().unwrap(); let op = it.next().unwrap().chars().next().unwrap(); let mut b: Vec = (0..w).map(|_| it.next().unwrap().parse().unwrap()).collect(); let mut a: Vec = (0..h).map(|_| it.next().unwrap().parse().unwrap()).collect(); let ans = if op == '+' { for a in a.iter_mut().chain(b.iter_mut()) { *a %= k; } a.sort(); let a = run_length_encoding(&a); b.sort(); let b = run_length_encoding(&b); let mut ans = 0; for (a, x) in a.iter() { if let Ok(k) = b.binary_search_by(|p| p.0.cmp(&(k - a))) { ans += x * b[k].1; } } if a[0].0 == 0 && b[0].0 == 0 { ans += a[0].1 * b[0].1; } ans } else { for a in a.iter_mut().chain(b.iter_mut()) { *a = gcd(*a, k); } a.sort(); let a = run_length_encoding(&a); b.sort(); let b = run_length_encoding(&b); let mut ans = 0; for (a, x) in a.iter() { for (b, y) in b.iter() { if *a * *b % k == 0 { ans += x * y; } } } ans }; println!("{}", ans); } fn main() { run(); }