0477.cpp (419B)
1 class Solution { 2 public: 3 int totalHammingDistance(const vector<int> &nums) const { 4 const int n = size(nums); 5 int res = 0; 6 7 for (unsigned mask = 1; mask <= 1 << 30; mask <<= 1) { 8 int count = 9 accumulate(begin(nums), end(nums), 0, [&mask](int a, int b) { return a + !!(b & mask); }); 10 res += count * (n - count); 11 } 12 13 return res; 14 } 15 };