#include #include #include using namespace std; typedef long long ll; int gcd(int a, int b) { int r = a % b; return r ? gcd(b, r) : b; } int main() { int n, m, k; cin >> n >> m >> k; char op; cin >> op; ll ans = 0; if (op == '+') { vector v(m); for (int i = 0; i < m; i++) { int b; cin >> b; v[i] = b % k; } sort(v.begin(), v.end()); for (int i = 0; i < n; i++) { int a; cin >> a; int x = k - a % k; int l = 0, r = m; while (l < r) { int c = (l + r) / 2; if (v[c] < x) l = c + 1; else r = c; } if (v[l] != x) continue; int tmp = l; r = m; while (l < r) { int c = (l + r) / 2; if (v[c] <= x) l = c + 1; else r = c; } ans += l - tmp; } } else { int d = 1; while (d * d <= k) d++; vector v(d); int count = 0; for (int i = 0; i < m; i++) { int b; cin >> b; if (b % k == 0) { count++; continue; } for (int j = 2; j < d; j++) { if (b % j == 0) v[j]++; } } for (int i = 0; i < n; i++) { int a; cin >> a; if (a % k == 0) { ans += m; continue; } int x = k / gcd(a, k); if (x < d) ans += v[x]; ans += count; } } cout << ans << endl; }