// #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 map memo; set calced; V primes; ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll dfs(ll cur, map &cur_m, map &max_m, map &gcd_v) { if (calced.find(cur) != calced.end()) return memo[cur]; calced.insert(cur); DEBUG(cur); V his; ll ret = gcd_v[cur]; for (ll i = 0; i < primes.size(); i++) { ll p = primes[i]; ll c = cur_m[p]; ll m = max_m[p]; if (c < m) { cur_m[p]++; ret += dfs(cur * p, cur_m, max_m, gcd_v); cur_m[p]--; for (ll e : his) { DEBUG(make_tuple(ret, cur, e, cur * p, lcm(e, cur * p), memo[lcm(e, cur * p)])); ret -= memo[lcm(e, cur * p)]; } his.push_back(cur * p); } } memo[cur] = ret; return ret; } 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)]++; for (auto &&e : gcd_v) DEBUG(e); ll cpy = K; map max_m; auto add_p = [&](ll key, ll val) { primes.push_back(key); max_m[key] += val; }; for (ll i = 2; i * i <= K; i++) { ll c = 0; while (cpy % i == 0) { cpy /= i; c++; } if (c) add_p(i, c); } if (1 < cpy) add_p(cpy, 1); map cur_m; dfs(1, cur_m, max_m, gcd_v); ll ret = 0; for (auto &&e : memo) DEBUG(e); for (ll e : A) { ll g = gcd(e, K); DEBUG(K/g); ret += memo[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; }