#include using namespace std; typedef long long ll; typedef pair P; #define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0) #define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0) ll gcd(ll a,ll b) { while (b) { a %= b; swap(a,b); } return a; } int const MAX_N = 100000; vector prime; vector is_prime(MAX_N,true); void Eratosthenes() { is_prime[0] = is_prime[1] = false; for (ll i = 2;i*i < MAX_N;++i) if (is_prime[i]) for (ll j = 2*i;j < MAX_N;j += i) is_prime[j] = false; for (ll i = 0;i < MAX_N;++i) if (is_prime[i]) prime.push_back(i); } vector

prime_factorization(ll a) { vector

prime_factor; if (is_prime[0]) Eratosthenes(); for (ll& i : prime) { if (i*i > a) break; if (a%i == 0) { P p = P(i,0); while (a%i == 0) { a /= i; p.second++; } prime_factor.push_back(p); } } if (a != 1) prime_factor.push_back(P(a,1)); return prime_factor; } void rec(vector& div,vector

& fact,int i,ll d) { if (i == fact.size()) { div.push_back(d); return; } for (int j = 0;j <= fact[i].second;++j) { rec(div,fact,i+1,d); d *= fact[i].first; } } // unsorted vector divisor(ll a) { vector div; vector

prime_factor = prime_factorization(a); rec(div,prime_factor,0,1); return div; } int main() { int n,m,k; char c; cin >> n >> m >> k >> c; vector a(n),b(m); for (int i = 0;i < m;++i) cin >> b[i]; for (int i = 0;i < n;++i) cin >> a[i]; if (c == '+') { ll ans = 0; map mp; for (int i = 0;i < m;++i) mp[b[i]%k]++; for (int i = 0;i < n;++i) ans += mp[(k-a[i]%k)%k]; cout << ans << endl; } else { ll ans = 0; map mp1,mp2; for (int i = 0;i < m;++i) mp1[gcd(b[i],k)]++; for (auto p : mp1) { for (ll i : divisor(p.first)) mp2[i] += p.second; } for (int i = 0;i < n;++i) { if (a[i]%k == 0) ans += m; else { ans += mp2[k/gcd(a[i],k)]; } } cout << ans << endl; } }