#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef pair P; typedef pair Pid; typedef pair Pdi; typedef pair Pl; typedef pair> PP; const double PI = 3.1415926535897932; // acos(-1) const double EPS = 1e-15; const int INF = 1001001001; const int mod = 1e+9 + 7; #define chmax(x, y) x = max(x, y) #define chmin(x, y) x = min(x, y) #define chadd(x, y) x = (x + y) % mod ll gcd(ll a, ll b){ if(b == 0) return a; return gcd(b, a % b); } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; ll k; char op; cin >> n >> m >> k >> op; vector b(m); vector a(n); ll res = 0; if(op == '+'){ for(int i = 0; i < m; ++i){ cin >> b[i]; b[i] %= k; } for(int i = 0; i < n; ++i){ cin >> a[i]; a[i] %= k; } sort(b.begin(), b.end()); for(int i = 0; i < n; ++i){ res += upper_bound(b.begin(), b.end(), k - a[i]) - lower_bound(b.begin(), b.end(), k - a[i]); } } else{ for(int i = 0; i < m; ++i) cin >> b[i]; for(int i = 0; i < n; ++i) cin >> a[i]; map gcd_a, gcd_b; for(int i = 0; i < n; ++i) ++gcd_a[gcd(a[i], k)]; for(int i = 0; i < m; ++i) ++gcd_b[gcd(b[i], k)]; for(auto A : gcd_a){ for(auto B : gcd_b){ if(A.first * B.first % k == 0) res += A.second * B.second; } } } cout << res << endl; }