#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef pair P; typedef bitset<16> BS; struct edge { int to, cost, id; }; const ll MOD = 1E+09 + 7; // =998244353; const ll INF = 1E18; const int MAX_N = 1E+05; ll dx[4] = { -1, 1, 0, 0 }, dy[4] = { 0, 0, -1, 1 }; int N, M; ll K; ll A[MAX_N + 1], B[MAX_N + 1]; ll gcd(ll a, ll b); int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> N >> M >> K; char op; cin >> op; for (int i = 1; i <= M; i++) { cin >> B[i]; } for (int i = 1; i <= N; i++) { cin >> A[i]; } ll ans = 0; if (op == '+') { unordered_map AM, BM; for (int i = 1; i <= N; i++) { AM[A[i] % K]++; } for (int j = 1; j <= M; j++) { BM[B[j] % K]++; } for (auto p : AM) { ans += BM[K - p.first] * p.second; } } else { unordered_map AD, BD; for (int i = 1; i <= N; i++) { AD[gcd(A[i], K)]++; } for (int j = 1; j <= M; j++) { BD[gcd(B[j], K)]++; } for (auto pa : AD) { for (auto pb : BD) { ll da = pa.first, db = pb.first; if ((da * db) % K == 0) { ans += pa.second * pb.second; } } } } /* for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cout << "i = " << i << ", j = " << j << ", dp = " << dp[i][j] << "\n"; } } */ cout << ans << "\n"; return 0; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); }