/** * author: otera **/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define int long long typedef long long ll; typedef unsigned long long ul; typedef unsigned int ui; typedef long double ld; const int inf=1e9+7; const ll INF=1LL<<60 ; const ll mod=1e9+7 ; #define rep(i,n) for(int i=0;i=0;i--) #define Rep(i,sta,n) for(int i=sta;i=1;i--) #define Rep1(i,sta,n) for(int i=sta;i<=n;i++) typedef complex Point; const ld eps = 1e-8; const ld pi = acos(-1.0); typedef pair P; typedef pair LDP; typedef pair LP; #define fr first #define sc second #define all(c) c.begin(),c.end() #define pb push_back #define debug(x) cerr << #x << " = " << (x) << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } int gcd(int a, int b) { if(b == 0) return a; return gcd(b, a % b); } void solve() { int n, m, k; cin >> n >> m >> k; char op; cin >> op; vector b(m), a(n); rep(i, m) { cin >> b[i]; } rep(i, n) { cin >> a[i]; } if(op == '+') { map mp; rep(i, m) { mp[b[i] % k] ++; } int ans = 0; rep(i, n) { ans += mp[(a[i] % k == 0 ? 0 : k - a[i] % k)]; } cout << ans << endl; } else { vector div; int j = 0; map mp; for(int i = 1; i * i <= k; ++i) { if(k % i == 0) { div.push_back(i); mp[i] = j; ++j; if(i * i != k) { div.push_back(k / i); mp[k / i] = j; ++j; } } } int sz = div.size(); vector cntb(sz, 0), cntbb(sz, 0); rep(i, m) { int d = gcd(k, b[i]); cntb[mp[d]] ++; } rep(i, sz) { int d = div[i]; for(int j = 0; j < sz; ++j) { int d2 = div[j]; if(d2 % d == 0) { cntbb[i] += cntb[j]; } } } int ans = 0; rep(i, n) { int d = gcd(k, a[i]); ans += cntbb[mp[k / d]]; } cout << ans << endl; } } signed main() { ios::sync_with_stdio(false); cin.tie(0); //cout << fixed << setprecision(10); //int t; cin >> t; rep(i, t)solve(); solve(); return 0; }