#include #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; using lint=long long; template T gcd(const T& a,const T& b){ return b==0?a:gcd(b,a%b); } class linear_sieve{ vector lpf,p; public: linear_sieve(int n):lpf(n+1){ for(int i=2;i<=n;i++){ if(lpf[i]==0){ lpf[i]=i; p.emplace_back(i); } for(int j=0;j& primes()const{ return p; } bool is_prime(int a)const{ assert(a<=(int)lpf.size()-1); return a>0 && lpf[a]==a; } map prime_factorize(int a)const{ assert(a<=(int)lpf.size()-1); map pf; for(;a>1;a/=lpf[a]) ++pf[lpf[a]]; return pf; } }; template vector divisors(const map& pf){ vector res={T(1)}; for(const auto& q:pf){ int m=res.size(); T pp=1; rep(i,q.second){ pp*=q.first; rep(i,m) res.emplace_back(res[i]*pp); } } sort(res.begin(),res.end()); return res; } int main(){ linear_sieve LS(1e6); int n,m; lint k; char op; cin>>n>>m>>k>>op; vector b(m),a(n); rep(j,m) cin>>b[j], b[j]%=k; rep(i,n) cin>>a[i], a[i]%=k; sort(b.begin(),b.end()); lint ans=0; if(op=='+'){ rep(i,n){ auto p=equal_range(b.begin(),b.end(),(k-a[i])%k); ans+=p.second-p.first; } } else{ vector cnt(1e7); rep(j,m) for(const auto& d:divisors(LS.prime_factorize(b[j]!=0?b[j]:k))) if(d<1e7) cnt[d]++; rep(i,n){ lint g=gcd(a[i],k); if(k/g<1e7){ ans+=cnt[k/g]; } else{ for(lint x=0;x