<p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code> where the bitwise <code>AND</code> of the elements of the subarray equals <code>k</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,1], k = 1</span></p>

<p><strong>Output:</strong> <span class="example-io">6</span></p>

<p><strong>Explanation:</strong></p>

<p>All subarrays contain only 1&#39;s.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,2], k = 1</span></p>

<p><strong>Output:</strong> <span class="example-io">3</span></p>

<p><strong>Explanation:</strong></p>

<p>Subarrays having an <code>AND</code> value of 1 are: <code>[<u><strong>1</strong></u>,1,2]</code>, <code>[1,<u><strong>1</strong></u>,2]</code>, <code>[<u><strong>1,1</strong></u>,2]</code>.</p>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3], k = 2</span></p>

<p><strong>Output:</strong> <span class="example-io">2</span></p>

<p><strong>Explanation:</strong></p>

<p>Subarrays having an <code>AND</code> value of 2 are: <code>[1,<b><u>2</u></b>,3]</code>, <code>[1,<u><strong>2,3</strong></u>]</code>.</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= nums[i], k &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def countSubarrays(self, nums: List[int], k: int) -> int:
        max_bit = 32
        def add(A, n):
            for i,d in enumerate(bin(n)[2:][::-1]):
                A[i][0] += d == '0'
                A[i][1] += d == '1'
            i += 1
            while i < max_bit:
                A[i][0] += 1
                i += 1
        def remove(A, n):
            for i,d in enumerate(bin(n)[2:][::-1]):
                A[i][0] -= d == '0'
                A[i][1] -= d == '1'
            i += 1
            while i < max_bit:
                A[i][0] -= 1
                i += 1
        def calc(A):
            i = 0
            res = 0
            for no0,no1 in A:
                if no0 == 0 and no1 > 0:
                    res = res|(1<<i)
                i += 1
            return res

        def F(nums):
            N = len(nums)
            res = 0
            j = 0
            A = [[0]*2 for _ in range(max_bit)]
            for i in range(N):
                add(A, nums[i])
                # print(j,i,A,calc(A))
                while j <= i and calc(A) == k:
                    remove(A, nums[j])
                    j += 1
                res += j
                # print(j,i,A,calc(A))
            return res

        A = []
        i = 0
        while i < len(nums):
            if nums[i]&k == k:
                B = [nums[i]]
                j = i+1
                while j < len(nums) and nums[j]&k == k:
                    B.append(nums[j])
                    j += 1
                A.append(B)
                i = j
            else:
                i += 1
        # print(A)
        res = 0
        for row in A:
            res += F(row)
        return res

4203