// #define DEBUGGING #include #define endl '\n' #define ALL(V) (V).begin(), (V).end() #define ALLR(V) (V).rbegin(), (V).rend() using ll = std::int64_t; using ull = std::uint64_t; using PLL = std::pair; using TLL = std::tuple; template using V = std::vector; template using VV = V>; template const T& var_min(const T &t) { return t; } template const T& var_max(const T &t) { return t; } template const T& var_min(const T &t, const Tail&... tail) { return std::min(t, var_min(tail...)); } template const T& var_max(const T &t, const Tail&... tail) { return std::max(t, var_max(tail...)); } template void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); } template void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); } template const T& clamp(const T &t, const T &low, const T &high) { return std::max(low, std::min(high, t)); } template void chclamp(T &t, const T &low, const T &high) { return t = clamp(t, low, high); } namespace init__ { struct InitIO { InitIO() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(30); } } init_io; } #define mv_rec make_v(init, tail...) template T make_v(T init) { return init; } template auto make_v(T init, size_t s, Tail... tail) { return V(s, mv_rec); } #undef mv_rec using namespace std; #ifdef DEBUGGING #include "../../debug/debug.cpp" #else #define DEBUG(...) 0 #define DEBUG_SEPARATOR_LINE 0 #endif ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll solve_p(ll N, ll M, ll K, const V &A, const V &B) { map cnt; for (ll e : A) cnt[e % K]++; ll ret = 0; for (ll e : B) { ll mod = e % K; ll key = (K - mod) % K; ret += cnt[key]; } return ret; } ll solve_m(ll N, ll M, ll K, const V &A, const V &B) { map gcd_v; for (ll e : B) gcd_v[gcd(e, K)]++; V divs; for (ll i = 1; i * i <= K; i++) { if (K % i) continue; divs.push_back(i); if (i * i < K) divs.push_back(K / i); } sort(ALL(divs)); DEBUG(divs); map cnt; for (ll i = 0; i < divs.size(); i++) { ll c = gcd_v[divs[i]]; DEBUG(make_tuple(c, divs[i])); for (ll j = 0; j <= i; j++) if (divs[i] % divs[j] == 0) { DEBUG(make_tuple(divs[i], divs[j])); cnt[divs[j]] += c; } } for (auto &&e : cnt) DEBUG(e); ll ret = 0; for (ll e : A) { ll g = gcd(e, K); ret += cnt[K / g]; } return ret; } int main() { ll N, M, K; cin >> N >> M >> K; char op; cin >> op; V B(M), A(N); for (ll &e : B) cin >> e; for (ll &e : A) cin >> e; ll ans = (op == '+' ? solve_p(N, M, K, A, B) : solve_m(N, M, K, A, B)); cout << ans << endl; return 0; }