#include #include #include #include using namespace std; template T gcd(T a,T b){ if(a%b==0)return b; else return gcd(b,a%b); } template T lcm(T a,T b){ return a/gcd(a,b)*b; } int main(){ long long n,m,k;cin>>n>>m>>k; long long ans = 0; char z;cin>>z; vector A(n); vector B(m); for(int i = 0; m > i; i++)cin>>B[i]; for(int i = 0; n > i; i++)cin>>A[i]; //A[i]と互いに素なB[j]の個数が答え..........O(NM)!!!!!!解散!!!!!笑 if(z == '*'){ for(int i = 0; m > i; i++){ B[i] = gcd(B[i],k); } for(int i = 0; n > i; i++){ A[i] = gcd(A[i],k); } map C; for(int i = 0; n > i; i++){ C[A[i]]++; } map D; for(int i = 0; m > i; i++){ D[B[i]]++; } for(auto x:C){ for(auto y:D){ if(!((x.first*y.first)%k)){ ans += (long long)x.second*y.second; } } } cout << ans << endl; }else{//O(n log m) for(int i = 0; m > i; i++){ B[i] = B[i]%k; } sort(B.begin(),B.end()); for(int i = 0; n > i; i++){ A[i] = A[i]%k; int want = (k-A[i])%k; ans += (upper_bound(B.begin(),B.end(),want)-lower_bound(B.begin(),B.end(),want)); } cout << ans << endl; } }