#include using namespace std; typedef long long ll; int main() { int N, M; ll K; cin >> N >> M >> K; vector A(N); ll B[M]; string op; cin >> op; map map_A, map_B; map_A[0] = 0; map_B[0] = 0; for (int i = 0; i < M; i++) { cin >> B[i]; if (map_B.count(B[i] % K)) { map_B[B[i] % K]++; } else { map_B[B[i] % K] = 1; } } for (int i = 0; i < N; i++) { cin >> A[i]; if (map_A[A[i] % K]) { map_A[A[i] % K]++; } else { map_A[A[i] % K] = 1; } } ll sum = 0; if (op == "+") { for (auto it = map_A.begin(); it != map_A.end(); it++) { if (map_B.count(K - it->first)) { sum += it->second * map_B[K - it->first]; } } } else { vector p; for (ll i = 2; i * i <= K; i++) { if ((K % i) == 0) { p.push_back(i); if (i * i != K) p.push_back(K / i); } } p.push_back(K); int n = p.size(); ll s[n]{}; ll t[n]{}; for (int i = 0; i < N; i++) { for (int j = n - 1; j >= 0; j--) { if (A[i] % p[j] == 0) { s[j]++; break; } } } for (int i = 0; i < M; i++) { for (int j = n - 1; j >= 0; j--) { if (B[i] % p[j] == 0) { s[j]++; break; } } } sort(p.begin(), p.end()); sum = s[n - 1] * M + t[n - 1] * N; for (int i = 0; i < n - 1; i++) { ll v = 0; ll k = K / p[i]; //cout << p[i] << " " << k << "\n"; for (int j = 0; j < n - 1; j++) { if ((p[j] % k) == 0) { v = max(v, s[i] * t[j]); //cout << i << " " << s[i] << " " << t[j] << "\n"; } } sum += v; } // for (int i = 0; i < n; i++) { // cout << p[i] << " " << s[i] << " " << t[i] << "\n"; // } } cout << sum << "\n"; return 0; }