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