#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } constexpr long long MAX = 5100000; constexpr long long INF = 1LL << 60; constexpr int inf = 1 << 28; constexpr long long mod = 1000000007LL; //constexpr long long mod = 998244353LL; using namespace std; typedef unsigned long long ull; typedef long long ll; ll gcd(ll x, ll y) { ll r; if (x < y) { swap(x, y); } while (y > 0) { r = x % y; x = y; y = r; } return x; } int main() { /* cin.tie(nullptr); ios::sync_with_stdio(false); */ ll N, M, K; cin >> N >> M >> K; char op; cin >> op; vector b(M); for (int i = 0; i < M; i++) cin >> b[i]; vector a(N); for (int i = 0; i < N; i++) cin >> a[i]; if (op == '+') { vector cnt(K); for (ll i = 0; i < N; i++) cnt[a[i] % K]++; ll res = 0; for (ll i = 0; i < M; i++) { if (b[i] % K == 0) res += cnt[0]; else { res += cnt[K - b[i] % K]; } } cout << res << endl; } else { map mp1, mp2; for (ll i = 0; i < M; i++) mp1[gcd(K, b[i])]++; for (ll i = 0; i < N; i++) mp2[gcd(K, a[i])]++; vector> vp1, vp2; for (auto p : mp1) { vp1.emplace_back(p.first, p.second); } for (auto p : mp2) { vp2.emplace_back(p.first, p.second); } ll res = 0; for (auto p1 : vp1) { for (auto p2 : vp2) { if (p1.first * p2.first % K == 0) { res += p1.second * p2.second; } } } cout << res << endl; } return 0; /* おまじないを使ったらscanfとprintf関連注意!!!!!!!!!!!! */ }