use std::io::*; fn gcd(a: usize, b: usize) -> usize { if b == 0 { return a; } gcd(b, a % b) } fn main() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.trim().split_whitespace(); let n: usize = itr.next().unwrap().parse().unwrap(); let m: usize = itr.next().unwrap().parse().unwrap(); let k: usize = itr.next().unwrap().parse().unwrap(); let op: char = itr.next().unwrap().parse().unwrap(); let b: Vec = (0..m) .map(|_| itr.next().unwrap().parse().unwrap()) .collect(); let a: Vec = (0..n) .map(|_| itr.next().unwrap().parse().unwrap()) .collect(); let mut memo = std::collections::HashMap::new(); let mut ans = 0; if op == '+' { for i in 0..m { let p = memo.entry(b[i] % k).or_insert(0); *p += 1; } for i in 0..n { let p = memo.entry(k - a[i] % k).or_insert(0); ans += *p; } } else { let mut mome = std::collections::HashMap::new(); for i in 0..m { let d = gcd(k, b[i]); let p = mome.entry(d).or_insert(0); *p += 1; } for i in 0..n { let d = gcd(k, a[i]); let p = memo.entry(d).or_insert(0); *p += 1; } for (k1, v1) in memo.iter() { for (k2, v2) in mome.iter() { if k1 * k2 % k == 0 { ans += v1 * v2; } } } } println!("{}", ans); }