N, M, K = gets.split.map(&:to_i) op, *cols = gets.split op = op.to_sym B = cols.map(&:to_i) A = N.times.map{ gets.to_i } if op == :+ Amod = Hash.new(0) A.each{|a| Amod[a] += 1} puts B.inject(0){|s,b| s + Amod[(K - b) % K] } else Agcd = Hash.new(0) Bgcd = Hash.new(0) A.each{|a| Agcd[a.gcd(K)] += 1 } B.each{|b| Bgcd[b.gcd(K)] += 1 } ans = 0 Agcd.each do |ag, ac| Bgcd.each do |bg, bc| ans += ac * bc if (ag * bg) % K == 0 end end puts ans end