Lamp Corridor Toggles

medium bit-manipulation hashing prefix-sums arrays 1000 ms · 256 MB

Lamp Corridor Toggles

The maintenance corridor has 30 lamps, numbered 0 through 29, strung along the ceiling. All of them start switched off.

Along the corridor stand n switch panels, numbered 1 to n from one end to the other. Panel i is wired to a 30-bit mask s_i: pressing panel i once flips every lamp whose bit is set in s_i (on becomes off, off becomes on) and leaves every other lamp exactly as it was.

Pressing a sequence of panels flips each lamp once for every panel in the sequence that controls it, so the lamps' final state depends only on which panels were pressed — not on the order they were pressed in. In particular, if you press a whole run of panels, the result is the same no matter what order you press them in.

A stretch is a run of consecutive panels l, l+1, …, r (1 ≤ l ≤ r ≤ n). Starting from all lamps off, suppose every panel in a stretch gets pressed exactly once. Count the number of stretches [l, r] for which the lamps end up showing exactly the target pattern K — that is, lamp b is lit after pressing the stretch if and only if bit b of K is 1.

Input

The first line contains two integers n and K (1 ≤ n ≤ 2·10^5, 0 ≤ K < 2^30).

The second line contains n integers s_1, …, s_n (0 ≤ s_i < 2^30), the mask wired to each panel.

Output

Print a single integer: the number of stretches [l, r] whose combined toggles leave the lamps showing exactly pattern K. The answer may exceed the range of a 32-bit integer.

Notes

In the first example, pressing panels 1, 4, 2–3, or 1–4 each leave the lamps showing pattern 5; no other stretch does.

Example 1

Input

4 5
5 3 6 5

Output

4

Example 2

Input

3 0
0 0 0

Output

6

Hints · reveal one at a time if you're stuck

Hint 1

Pressing each panel in a stretch once flips a lamp once for each panel that controls it, so the order does not matter. What single value describes the lamp pattern of a stretch?

Hint 2

Use prefix XORs of the masks. The pattern of a stretch from l to r is the XOR of two prefix values, so for each r you need to know how many earlier prefix values equal the prefix at r XOR K.

Hint 3

Scan the panels from left to right and keep a hash map counting how often each prefix value has appeared so far, including the empty prefix before panel 1. Add the matching count for each r, and use a 64-bit total.

report an issue Prefer your own editor? sooks prep 91 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.