#pragma GCC optimize ("Ofast") #include using namespace std; void *wmem; char memarr[96000000]; template inline void walloc1d(T **arr, int x, void **mem = &wmem){ static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] ); (*arr)=(T*)(*mem); (*mem)=((*arr)+x); } template void sortA_L(int N, T1 a[], T2 b[], void *mem = wmem){ int i; pair *arr; walloc1d(&arr, N, &mem); for(i=(0);i<(N);i++){ arr[i].first = a[i]; arr[i].second = b[i]; } sort(arr, arr+N); for(i=(0);i<(N);i++){ a[i] = arr[i].first; b[i] = arr[i].second; } } inline void rd(int &x){ int k; int m=0; x=0; for(;;){ k = getchar_unlocked(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = getchar_unlocked(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } inline void rd(char &c){ int i; for(;;){ i = getchar_unlocked(); if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF){ break; } } c = i; } inline int rd(char c[]){ int i; int sz = 0; for(;;){ i = getchar_unlocked(); if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF){ break; } } c[sz++] = i; for(;;){ i = getchar_unlocked(); if(i==' '||i=='\n'||i=='\r'||i=='\t'||i==EOF){ break; } c[sz++] = i; } c[sz]='\0'; return sz; } inline void wt_L(char a){ putchar_unlocked(a); } inline void wt_L(long long x){ int s=0; int m=0; char f[20]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ putchar_unlocked('-'); } while(s--){ putchar_unlocked(f[s]+'0'); } } template inline T GCD_L(T a, U b){ T r; while(b){ r=a; a=b; b=r%a; } return a; } template inline S moddw_L(S a, const T b){ a %= b; if(a < 0){ a += b; } return a; } template void Unique_L(int &N, T A[], int sorted=0){ int i; int k; if(!sorted){ sort(A, A+N); } k = 0; for(i=(0);i<(N);i++){ if(k==0 || A[k-1]!=A[i]){ A[k++] = A[i]; } } N = k; } template void Unique_L(int &N, T A[], S B[], int sorted=0){ int i; int k = 0; if(!sorted){ sortA_L(N, A, B); } for(i=(0);i<(N);i++){ if(!k || A[k-1]!=A[i]){ A[k] = A[i]; B[k] = B[i]; k++; } else{ B[k-1] += B[i]; } } N=k; } int N; int M; int K; int A[100000]; int B[100000]; char op; map mp; int cntA[100000]; int cntB[100000]; int main(){ wmem = memarr; int i; int j; int k; long long res = 0; rd(N); rd(M); rd(K); rd(op); { int Lj4PdHRW; for(Lj4PdHRW=(0);Lj4PdHRW<(M);Lj4PdHRW++){ rd(B[Lj4PdHRW]); } } { int e98WHCEY; for(e98WHCEY=(0);e98WHCEY<(N);e98WHCEY++){ rd(A[e98WHCEY]); } } if(op=='+'){ for(i=(0);i<(N);i++){ mp[A[i]%K]++; } for(i=(0);i<(M);i++){ j =moddw_L((-B[i]),K); if(mp.count(j)){ res += mp[j]; } } } else{ for(i=(0);i<(N);i++){ cntA[i] = 1; } for(i=(0);i<(M);i++){ cntB[i] = 1; } for(i=(0);i<(N);i++){ A[i] =GCD_L(A[i], K); } for(i=(0);i<(M);i++){ B[i] =GCD_L(B[i], K); } Unique_L(N,A,cntA); Unique_L(N,B,cntB); for(i=(0);i<(N);i++){ for(j=(0);j<(M);j++){ if( (long long)A[i]*B[j]%K==0 ){ res += (long long) cntA[i] * cntB[j]; } } } } wt_L(res); wt_L('\n'); return 0; } // cLay varsion 20200214-1 // --- original code --- // int N, M, K, A[1d5], B[1d5]; // char op; // // map mp; // int cntA[1d5], cntB[1d5]; // { // int i, j, k; // ll res = 0; // rd(N,M,K,op,B(M),A(N)); // // if(op=='+'){ // rep(i,N) mp[A[i]%K]++; // rep(i,M){ // j = (-B[i]) %% K; // if(mp.count(j)) res += mp[j]; // } // } else { // rep(i,N) cntA[i] = 1; // rep(i,M) cntB[i] = 1; // rep(i,N) A[i] = gcd(A[i], K); // rep(i,M) B[i] = gcd(B[i], K); // Unique(N,A,cntA); // Unique(N,B,cntB); // rep(i,N) rep(j,M) if( (ll)A[i]*B[j]%K==0 ) res += (ll) cntA[i] * cntB[j]; // } // // wt(res); // }