/* -*- coding: utf-8 -*- * * 990.cc: No.990 N×Mマス計算(Kの倍数) - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 100000; const int MAX_M = 100000; const int MAX_DN = 32; /* typedef */ typedef long long ll; typedef map mii; /* global variables */ int as[MAX_N], bs[MAX_M]; mii brs; int kds[MAX_DN], bdns[MAX_DN]; /* subroutines */ /* main */ int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); char s[4]; scanf("%s", s); char op = s[0]; int asum = 0, bsum = 0; for (int j = 0; j < m; j++) scanf("%d", bs + j); for (int i = 0; i < n; i++) scanf("%d", as + i); ll sum = 0; if (op == '+') { for (int j = 0; j < m; j++) brs[bs[j] % k]++; for (int i = 0; i < n; i++) { mii::iterator mit = brs.find((k - as[i] % k) % k); if (mit != brs.end()) sum += mit->second; } } else { int kdn = 0; for (int p = 1; p * p <= k; p++) if (k % p == 0) { kds[kdn++] = p; int q = k / p; if (q != p) kds[kdn++] = q; } sort(kds, kds + kdn); for (int j = 0; j < m; j++) for (int di = 0; di < kdn; di++) if (bs[j] % kds[di] == 0) bdns[di]++; for (int i = 0; i < n; i++) for (int di = kdn - 1; di >= 0; di--) if (as[i] % kds[di] == 0) { sum += bdns[kdn - 1 - di]; break; } } printf("%lld\n", sum); return 0; }