Given three integer arrays a, b, and c, return the number of triplets (a[i], b[j], c[k]), such that the bitwise XOR between the elements of each triplet has an even number of set bits.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution { public: longlongtripletCount(vector<int>& a, vector<int>& b, vector<int>& c){ vector<vector<longlong>> dp(3,vector<longlong>(2)); for(auto& n : a) dp[0][__builtin_popcount(n) & 1]++; for(auto& n : b) dp[1][__builtin_popcount(n) & 1]++; for(auto& n : c) dp[2][__builtin_popcount(n) & 1]++; longlong res = 0; for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++) for(int k = 0; k < 2; k++) { if((i + j + k) & 1) continue; res += dp[0][i] * dp[1][j] * dp[2][k]; } return res; } };