|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: 中等 |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3215.Count%20Triplets%20with%20Even%20XOR%20Set%20Bits%20II/README.md |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- problem:start --> |
| 8 | + |
| 9 | +# [3215. Count Triplets with Even XOR Set Bits II 🔒](https://leetcode.cn/problems/count-triplets-with-even-xor-set-bits-ii) |
| 10 | + |
| 11 | +[English Version](/solution/3200-3299/3215.Count%20Triplets%20with%20Even%20XOR%20Set%20Bits%20II/README_EN.md) |
| 12 | + |
| 13 | +## 题目描述 |
| 14 | + |
| 15 | +<!-- description:start --> |
| 16 | + |
| 17 | +Given three integer arrays <code>a</code>, <code>b</code>, and <code>c</code>, return the number of triplets <code>(a[i], b[j], c[k])</code>, such that the bitwise <code>XOR</code> between the elements of each triplet has an <strong>even</strong> number of <span data-keyword="set-bit">set bits</span>. |
| 18 | + |
| 19 | +<p> </p> |
| 20 | +<p><strong class="example">Example 1:</strong></p> |
| 21 | + |
| 22 | +<div class="example-block"> |
| 23 | +<p><strong>Input:</strong> <span class="example-io">a = [1], b = [2], c = [3]</span></p> |
| 24 | + |
| 25 | +<p><strong>Output:</strong> <span class="example-io">1</span></p> |
| 26 | + |
| 27 | +<p><strong>Explanation:</strong></p> |
| 28 | + |
| 29 | +<p>The only triplet is <code>(a[0], b[0], c[0])</code> and their <code>XOR</code> is: <code>1 XOR 2 XOR 3 = 00<sub>2</sub></code>.</p> |
| 30 | +</div> |
| 31 | + |
| 32 | +<p><strong class="example">Example 2:</strong></p> |
| 33 | + |
| 34 | +<div class="example-block"> |
| 35 | +<p><strong>Input:</strong> <span class="example-io">a = [1,1], b = [2,3], c = [1,5]</span></p> |
| 36 | + |
| 37 | +<p><strong>Output:</strong> <span class="example-io">4</span></p> |
| 38 | + |
| 39 | +<p><strong>Explanation:</strong></p> |
| 40 | + |
| 41 | +<p>Consider these four triplets:</p> |
| 42 | + |
| 43 | +<ul> |
| 44 | + <li><code>(a[0], b[1], c[0])</code>: <code>1 XOR 3 XOR 1 = 011<sub>2</sub></code></li> |
| 45 | + <li><code>(a[1], b[1], c[0])</code>: <code>1 XOR 3 XOR 1 = 011<sub>2</sub></code></li> |
| 46 | + <li><code>(a[0], b[0], c[1])</code>: <code>1 XOR 2 XOR 5 = 110<sub>2</sub></code></li> |
| 47 | + <li><code>(a[1], b[0], c[1])</code>: <code>1 XOR 2 XOR 5 = 110<sub>2</sub></code></li> |
| 48 | +</ul> |
| 49 | +</div> |
| 50 | + |
| 51 | +<p> </p> |
| 52 | +<p><strong>Constraints:</strong></p> |
| 53 | + |
| 54 | +<ul> |
| 55 | + <li><code>1 <= a.length, b.length, c.length <= 10<sup>5</sup></code></li> |
| 56 | + <li><code>0 <= a[i], b[i], c[i] <= 10<sup>9</sup></code></li> |
| 57 | +</ul> |
| 58 | + |
| 59 | +<!-- description:end --> |
| 60 | + |
| 61 | +## 解法 |
| 62 | + |
| 63 | +<!-- solution:start --> |
| 64 | + |
| 65 | +### 方法一:位运算 |
| 66 | + |
| 67 | +对于两个整数,异或结果中 $1$ 的个数的奇偶性,取决于两个整数的二进制表示中 $1$ 的个数的奇偶性。 |
| 68 | + |
| 69 | +我们可以用三个数组 `cnt1`、`cnt2`、`cnt3` 分别记录数组 `a`、`b`、`c` 中每个数的二进制表示中 $1$ 的个数的奇偶性。 |
| 70 | + |
| 71 | +然后我们在 $[0, 1]$ 的范围内枚举三个数组中的每个数的二进制表示中 $1$ 的个数的奇偶性,如果三个数的二进制表示中 $1$ 的个数的奇偶性之和为偶数,那么这三个数的异或结果中 $1$ 的个数也为偶数,此时我们将这三个数的组合数相乘累加到答案中。 |
| 72 | + |
| 73 | +最后返回答案即可。 |
| 74 | + |
| 75 | +时间复杂度 $O(n)$,其中 $n$ 为数组 `a`、`b`、`c` 的长度。空间复杂度 $O(1)$。 |
| 76 | + |
| 77 | +<!-- tabs:start --> |
| 78 | + |
| 79 | +#### Python3 |
| 80 | + |
| 81 | +```python |
| 82 | +class Solution: |
| 83 | + def tripletCount(self, a: List[int], b: List[int], c: List[int]) -> int: |
| 84 | + cnt1 = Counter(x.bit_count() & 1 for x in a) |
| 85 | + cnt2 = Counter(x.bit_count() & 1 for x in b) |
| 86 | + cnt3 = Counter(x.bit_count() & 1 for x in c) |
| 87 | + ans = 0 |
| 88 | + for i in range(2): |
| 89 | + for j in range(2): |
| 90 | + for k in range(2): |
| 91 | + if (i + j + k) & 1 ^ 1: |
| 92 | + ans += cnt1[i] * cnt2[j] * cnt3[k] |
| 93 | + return ans |
| 94 | +``` |
| 95 | + |
| 96 | +#### Java |
| 97 | + |
| 98 | +```java |
| 99 | +class Solution { |
| 100 | + public long tripletCount(int[] a, int[] b, int[] c) { |
| 101 | + int[] cnt1 = new int[2]; |
| 102 | + int[] cnt2 = new int[2]; |
| 103 | + int[] cnt3 = new int[2]; |
| 104 | + for (int x : a) { |
| 105 | + ++cnt1[Integer.bitCount(x) & 1]; |
| 106 | + } |
| 107 | + for (int x : b) { |
| 108 | + ++cnt2[Integer.bitCount(x) & 1]; |
| 109 | + } |
| 110 | + for (int x : c) { |
| 111 | + ++cnt3[Integer.bitCount(x) & 1]; |
| 112 | + } |
| 113 | + long ans = 0; |
| 114 | + for (int i = 0; i < 2; ++i) { |
| 115 | + for (int j = 0; j < 2; ++j) { |
| 116 | + for (int k = 0; k < 2; ++k) { |
| 117 | + if ((i + j + k) % 2 == 0) { |
| 118 | + ans += 1L * cnt1[i] * cnt2[j] * cnt3[k]; |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + return ans; |
| 124 | + } |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +#### C++ |
| 129 | + |
| 130 | +```cpp |
| 131 | +class Solution { |
| 132 | +public: |
| 133 | + long long tripletCount(vector<int>& a, vector<int>& b, vector<int>& c) { |
| 134 | + int cnt1[2]{}; |
| 135 | + int cnt2[2]{}; |
| 136 | + int cnt3[2]{}; |
| 137 | + for (int x : a) { |
| 138 | + ++cnt1[__builtin_popcount(x) & 1]; |
| 139 | + } |
| 140 | + for (int x : b) { |
| 141 | + ++cnt2[__builtin_popcount(x) & 1]; |
| 142 | + } |
| 143 | + for (int x : c) { |
| 144 | + ++cnt3[__builtin_popcount(x) & 1]; |
| 145 | + } |
| 146 | + long long ans = 0; |
| 147 | + for (int i = 0; i < 2; ++i) { |
| 148 | + for (int j = 0; j < 2; ++j) { |
| 149 | + for (int k = 0; k < 2; ++k) { |
| 150 | + if ((i + j + k) % 2 == 0) { |
| 151 | + ans += 1LL * cnt1[i] * cnt2[j] * cnt3[k]; |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + return ans; |
| 157 | + } |
| 158 | +}; |
| 159 | +``` |
| 160 | +
|
| 161 | +#### Go |
| 162 | +
|
| 163 | +```go |
| 164 | +func tripletCount(a []int, b []int, c []int) (ans int64) { |
| 165 | + cnt1 := [2]int{} |
| 166 | + cnt2 := [2]int{} |
| 167 | + cnt3 := [2]int{} |
| 168 | + for _, x := range a { |
| 169 | + cnt1[bits.OnesCount(uint(x))%2]++ |
| 170 | + } |
| 171 | + for _, x := range b { |
| 172 | + cnt2[bits.OnesCount(uint(x))%2]++ |
| 173 | + } |
| 174 | + for _, x := range c { |
| 175 | + cnt3[bits.OnesCount(uint(x))%2]++ |
| 176 | + } |
| 177 | + for i := 0; i < 2; i++ { |
| 178 | + for j := 0; j < 2; j++ { |
| 179 | + for k := 0; k < 2; k++ { |
| 180 | + if (i+j+k)%2 == 0 { |
| 181 | + ans += int64(cnt1[i] * cnt2[j] * cnt3[k]) |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + return |
| 187 | +} |
| 188 | +``` |
| 189 | + |
| 190 | +#### TypeScript |
| 191 | + |
| 192 | +```ts |
| 193 | +function tripletCount(a: number[], b: number[], c: number[]): number { |
| 194 | + const cnt1: [number, number] = [0, 0]; |
| 195 | + const cnt2: [number, number] = [0, 0]; |
| 196 | + const cnt3: [number, number] = [0, 0]; |
| 197 | + for (const x of a) { |
| 198 | + ++cnt1[bitCount(x) & 1]; |
| 199 | + } |
| 200 | + for (const x of b) { |
| 201 | + ++cnt2[bitCount(x) & 1]; |
| 202 | + } |
| 203 | + for (const x of c) { |
| 204 | + ++cnt3[bitCount(x) & 1]; |
| 205 | + } |
| 206 | + let ans = 0; |
| 207 | + for (let i = 0; i < 2; ++i) { |
| 208 | + for (let j = 0; j < 2; ++j) { |
| 209 | + for (let k = 0; k < 2; ++k) { |
| 210 | + if ((i + j + k) % 2 === 0) { |
| 211 | + ans += cnt1[i] * cnt2[j] * cnt3[k]; |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | + return ans; |
| 217 | +} |
| 218 | + |
| 219 | +function bitCount(i: number): number { |
| 220 | + i = i - ((i >>> 1) & 0x55555555); |
| 221 | + i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); |
| 222 | + i = (i + (i >>> 4)) & 0x0f0f0f0f; |
| 223 | + i = i + (i >>> 8); |
| 224 | + i = i + (i >>> 16); |
| 225 | + return i & 0x3f; |
| 226 | +} |
| 227 | +``` |
| 228 | + |
| 229 | +<!-- tabs:end --> |
| 230 | + |
| 231 | +<!-- solution:end --> |
| 232 | + |
| 233 | +<!-- problem:end --> |
0 commit comments