代码拉取完成,页面将自动刷新
// Time: O(nlogn), n is the max of A
// Space: O(n)
// Reference: https://blog.csdn.net/john123741/article/details/76576925
// FWT solution
class Solution {
public:
int countTriplets(vector<int>& A) {
static const int k = 3;
const auto max_A = *max_element(A.cbegin(), A.cend());
int n = 1;
for (; n <= max_A; n <<= 1);
vector<int> B(n);
for (const auto& x : A) {
++B[x];
}
FWT(&B, 1);
for(auto& x : B) {
x = pow(x, k);
}
FWT(&B, -1);
return B[0];
}
private:
void FWT(vector<int> *A, int v) {
for (int d = 1; d < A->size(); d <<= 1) {
for (int m = d << 1, i = 0; i < A->size(); i += m) {
for (int j = 0; j < d ; ++j) {
(*A)[i + j] += (*A)[i + j + d] * v;
}
}
}
}
};
// Time: O(n^3), n is the length of A
// Space: O(n^2)
class Solution2 {
public:
int countTriplets(vector<int>& A) {
unordered_map<int, int> count;
for (int i = 0 ;i < A.size(); ++i) {
for (int j = 0; j < A.size(); ++j) {
++count[A[i] & A[j]];
}
}
int result = 0;
for (int k = 0; k < A.size(); ++k) {
for (const auto& kvp : count) {
if ((A[k] & kvp.first) == 0) {
result += kvp.second;
}
}
}
return result;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。