#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 nya; for(int i = 0; m > i; i++){ for(int j = 1; B[i] >= j*j; j++){ if(!(B[i]%j)){ nya[j]++; if(j*j != B[i])nya[B[i]/j]++; } } } for(int i = 0; n > i; i++){ ans += nya[k/A[i]]; } 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; } }