#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rep(i, n) repr(i, 0, n) #define INF 2e9 #define MOD 1000000007 //#define MOD 998244353 #define LINF (long long)4e18 #define jck 3.141592 using ll = long long; using Pi = pair; using Pl = pair; set divisor(ll n){ set s; for(ll i = 2; i*i <= n; i++){ if(n % i == 0){ s.insert(i); s.insert(n/i); } } return s; } int main(){ int N,M,K; cin >> N >> M >> K; char op; cin >> op; vector A(N),B(M); rep(i,M){ cin >> B[i]; B[i] %= K; } rep(i,N){ cin >> A[i]; A[i] %= K; } map mp1,mp2; rep(i,N){ mp1[A[i]]++; } rep(i,M){ mp2[B[i]]++; } if(op == '+'){ ll ans = 0; rep(i,N){ ans += mp2[K-A[i]]; } cout << ans << endl; } else{ set div = divisor(K); ll ans = 0; ans += mp1[0]*M+mp2[0]*N-mp1[0]*mp2[0]; rep(i,N){ set div1 = divisor(A[i]); int k = K; for(int u : div1){ while(k%u == 0){ k /= u; } } for(int u : div){ if(u % k == 0){ ans += mp2[u]; } } } cout << ans << endl; } }