#include #include #include #include #include #include #include using namespace std; typedef long long ll; template T gcd(T a, T b) { if (a < b) swap(a, b); while (b != 0) { T tmp = b; b = a % b; a = tmp; } return a; } int N, M, K; char op; int A[100000]; int B[100000]; map mpa, mpb; void solve_plus(){ ll ans = 0; for(int i = 0; i < M; i++) { int m = B[i]%K; if(mpb.count(m) == 0){ mpb[m] = 1; }else{ mpb[m]++; } } for(int i = 0; i < N; i++) { int m = A[i]%K; if(mpa.count(m) == 0){ mpa[m] = 1; }else{ mpa[m]++; } } for(auto i : mpa){ int rem = (K-i.first)%K; if(mpb.count(rem) != 0){ ans += ((ll)i.second)*mpb[rem]; } } cout << ans << endl; } void solve_minus(){ ll ans = 0; for(int i = 0; i < M; i++) { int m = gcd(B[i], K); if(mpb.count(m) == 0){ mpb[m] = 1; }else{ mpb[m]++; } } for(int i = 0; i < N; i++) { int m = gcd(A[i], K); if(mpa.count(m) == 0){ mpa[m] = 1; }else{ mpa[m]++; } } for(auto i : mpa){ for(auto j : mpb){ ll m = i.first; ll n = j.first; // cout << n << ' ' << m << endl; if((n*m)%K == 0){ ans += i.second*j.second; } } } cout << ans << endl; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; cin >> N >> M >> K; cin >> op; for(int i = 0; i < M; i++) cin >> B[i]; for(int i = 0; i < N; i++) cin >> A[i]; if(op == '+'){ solve_plus(); }else{ solve_minus(); } }