#include #define rep(i, a, b) for(int i = (a); i <= (b); i ++) using std::cin, std::cout, std::cerr; using ll = long long; void Solve() { int n, m, k; cin >> n >> m >> k; char op; cin >> op; std::vector a(n), b(m); rep(i, 0, m - 1) cin >> b[i]; rep(i, 0, n - 1) cin >> a[i]; if(op == '+') { std::map map; for(int x : a) map[x % k] ++; ll ans = 0; for(int x : b) ans += map[(k - x % k) % k]; cout << ans << '\n'; } else { std::map cnta, cntb; for(int x : a) { x = std::gcd(x, k); cnta[x] ++; } for(int x : b) { x = std::gcd(x, k); cntb[x] ++; } ll ans = 0; for(auto [x, cx] : cnta) for(auto[y, cy] : cntb) { if(ll(x) * y % k == 0) ans += ll(cx) * cy; } cout << ans << '\n'; } } int main() { std::ios::sync_with_stdio(false); int T = 1; while(T --) { Solve(); } }