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(); let mut div = Vec::new(); let mut i = 1; while i * i <= k { if k % i == 0 { div.push(i); if k / i != i { div.push(k / i); } } i += 1; } 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 i in 0..div.len() { for j in 0..div.len() { if div[i] * div[j] % k == 0 { let p = memo.entry(div[i]).or_insert(0); let q = mome.entry(div[j]).or_insert(0); ans += *p * *q; } } } } println!("{}", ans); }