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 { for i in 0..m { let mut j = 1; while j * j <= b[i] { if b[i] % j == 0 { let p = memo.entry(j).or_insert(0); *p += 1; if b[i] / j != j { let p = memo.entry(b[i] / j).or_insert(0); *p += 1; } } j += 1; } } for i in 0..n { let d = gcd(a[i], k); let p = memo.entry(k / d).or_insert(0); ans += *p; } } println!("{}", ans); }