<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 __init__(self):
        self.N = 31

    def left_pad(self, x, n = 31):
        return '0'*(max(0, n-len(x))) + x

    def countSubarrays(self, nums: List[int], k: int) -> int:
        # for each index i in nums, if it is a 1, record the closest 0 to the right
        r = [[len(nums) for _ in range(self.N)] for _ in range(len(nums))] # closest 0 to the right of index i (if ind i is a 1)
        bs = [self.left_pad(bin(nums[i])[2:]) for i in range(len(nums))] # bin rep of elts in nums
        #print("bs: ", bs)
        for j in range(self.N):
            l = len(nums)
            for i in range(len(nums)-1, -1, -1):
                if bs[i][j] == '0':
                    l = i
                elif bs[i][j] == '1':
                    r[i][j] = l
        #print("r: ", r)
        # binary rep of k?
        #print(str(bin(k))[2:])
        #print("bs: ", bs)
        #print("r: ", r)
        bink = str(self.left_pad(bin(k)[2:]))
        #print("bink: ", bink)
        c = 0
        for k in range(len(nums)): # vary left endpoint of our interval
            a, b = k, len(nums)-1 # limits of right endpoint, inclusive
            for j in range(len(bink)):
                if bink[j]=='0':
                    if bs[k][j] == '0':
                        pass
                    elif bs[k][j] == '1':
                        a = max(a, r[k][j])
                elif bink[j]=='1':
                    if bs[k][j] == '0':
                        a = b + 1
                        break
                    elif bs[k][j] == '1':
                        b = min(b, r[k][j]-1)
            #print("k, a, b: ", k, a, b)
            if a <= b:
                c += b - a + 1
        return c

4015