Under construction

Still being built. Things may change

Oh no, XOR again?

Interview

Time limit1sMemory limit512 MB

Summary
Count the integers x from A to B inclusive where x XOR N has exactly K ones in its binary form.
Level

Medium5 of 10

Topics
Dynamic programming, Bit manipulation
Solved
No attempts yet

Problem

Another XOR problem came up again, and it is as tedious as ever!

You are given integers NN, KK, AA, and BB. Among the integers from AA to BB inclusive, count how many of them, when XORed with NN, have exactly KK ones in their binary representation.

Input

The first line contains NN, KK, AA, and BB, in that order, separated by spaces.

Output

Print the answer.

Constraints

  • 1≤N≤2601 \le N \le 2^{60}
  • 0≤K≤600 \le K \le 60
  • 1≤A≤B≤2601 \le A \le B \le 2^{60}

Hint

Bitwise XOR is an operation that can be applied to two non-negative integers. For non-negative integers AA and BB, write each in binary, then for each bit position put 0 if the two bits are equal and 1 if they differ. The result is A⊕BA \oplus B.

For example, take A=10A=10 and B=6B=6. In binary, they are 101021010_2 and 011020110_2. The first and second bits from the right are equal, so they become 0. The third and fourth bits differ, so they become 1. Therefore A⊕BA \oplus B is 11002=121100_2 = 12.

Examples1

  1. Example 1

    Input
    10 2 1 20
    
    Expected output
    6