One day, after a difficult lecture a diligent student Sasha saw a graffitied desk in the classroom. She came closer and read: "Find such positive integer n, that among numbers n + 1, n + 2, ..., 2·n there are exactly m numbers which binary representation contains exactly k digits one".

The girl got interested in the task and she asked you to help her solve it. Sasha knows that you are afraid of large numbers, so she guaranteed that there is an answer that doesn't exceed 10^18.


-----Input-----

The first line contains two space-separated integers, m and k (0 ≤ m ≤ 10^18; 1 ≤ k ≤ 64).


-----Output-----

Print the required number n (1 ≤ n ≤ 10^18). If there are multiple answers, print any of them.


-----Examples-----
Input
1 1

Output
1

Input
3 2

Output
5

Difficulty: interview

Starter code:

Solutions:
def nck(n, k, cache = {}):
    if k > n or k < 0: return 0
    if k == 0 or k == n: return 1
    if k*2 > n: k = n-k
    if (n, k) in cache: return cache[(n, k)]

    z = cache[(n, k)] = nck(n-1, k-1) + nck(n-1, k)
    return z

def bits(n):
    b = 0
    while n:
        if n&1: b += 1
        n >>= 1
    return b

def count(n, k):
    z, b, c = 0, 63, 0
    for b in reversed(range(64)):
        if (n>>b)&1:
            z += nck(b, k-c)
            c += 1
        if not k: break
    return z + (bits(n) == k)

def solve(m, k):
    lo, hi = 1, 10**18
    while lo < hi:
        mi = (lo+hi)//2
        if count(2*mi, k) - count(mi, k) < m:
            lo = mi+1
        else:
            hi = mi
    return hi

m, k = [int(x) for x in input().split()]
print(solve(m, k))

def dfs(n, k, cache = {}):
    # if number of bits is bigger than the number's bits of the number's bits is less than 0
    if k > n or k < 0: return 0
    # if num bits is 0 or num bits is equivalent to the number's bits
    if k == 0 or k == n: return 1
    if k*2 > n: k = n-k
    # Check is already calculated
    if (n, k) in cache: return cache[(n, k)]
    # Use dfs addition for case where certain bit is 1 or certain bit is 0
    z = cache[(n, k)] = dfs(n-1, k-1) + dfs(n-1, k)
    return z

def bits(n):
    b = 0
    while n:
        if n&1: b += 1
        n >>= 1
    return b

def count(n, k):
    z, b, c = 0, 63, 0
    for b in reversed(range(64)):
        # Taking n and checking if bit is 1 or not
        if (n>>b)&1:
            z += dfs(b, k-c)
            c += 1
        if not k: break
    return z + (bits(n) == k)

def solve(m, k):
    # Binary Search for number 1-10^18
    low, high = 1, 10**18
    while low < high:
        mid = (low+high)//2
        if count(2*mid, k) - count(mid, k) < m:
            low = mid+1
        else:
            high = mid
    return high

m, k = [int(x) for x in input().split()]
print(solve(m, k))

def dfs(n, k, cache = {}):
    # if number of bits is bigger than the number's bits of the number's bits is less than 0
    if k > n or k < 0: return 0
    # if num bits is 0 or num bits is equivalent to the number's bits
    if k == 0 or k == n: return 1
    # 
    # if k*2 > n: k = n-k
    # Check is already calculated
    if (n, k) in cache: return cache[(n, k)]
    # Use dfs addition for case where certain bit is 1 or certain bit is 0
    z = cache[(n, k)] = dfs(n-1, k-1) + dfs(n-1, k)
    return z

def bits(n):
    b = 0
    while n:
        if n&1: b += 1
        n >>= 1
    return b

def count(n, k):
    z, b, c = 0, 63, 0
    for b in reversed(range(64)):
        # Taking n and checking if bit is 1 or not
        if (n>>b)&1:
            z += dfs(b, k-c)
            c += 1
        if not k: break
    return z + (bits(n) == k)

def solve(m, k):
    # Binary Search for number 1-10^18
    low, high = 1, 10**18
    while low < high:
        mid = (low+high)//2
        if count(2*mid, k) - count(mid, k) < m:
            low = mid+1
        else:
            high = mid
    return high

m, k = [int(x) for x in input().split()]
print(solve(m, k))

def dfs(n, k, cache = {}):
    # if number of bits is bigger than the number's bits of the number's bits is less than 0
    if k > n or k < 0: return 0
    # if num bits is 0 or num bits is equivalent to the number's bits
    if k == 0 or k == n: return 1
    # This optimization is not necessary but flips the 0s and the 1s
    # if k*2 > n: k = n-k
    # Check is already calculated
    if (n, k) in cache: return cache[(n, k)]
    # Use dfs addition for case where certain bit is 1 or certain bit is 0
    z = cache[(n, k)] = dfs(n-1, k-1) + dfs(n-1, k)
    return z

def bits(n):
    b = 0
    while n:
        if n&1: b += 1
        n >>= 1
    return b

def count(n, k):
    z, b, c = 0, 63, 0
    for b in reversed(range(64)):
        # Taking n and checking if certain bit is 1 or not
        # This sums for every mod power of 2 that exists to account for every case
        if (n>>b)&1:
            # calculates by subtracting for bits not accounted for
            z += dfs(b, k-c)
            c += 1
        # if not k: break
    return z + (bits(n) == k)

def solve(m, k):
    # Binary Search for number 1-10^18
    low, high = 1, 10**18
    while low < high:
        mid = (low+high)//2
        if count(2*mid, k) - count(mid, k) < m:
            low = mid+1
        else:
            high = mid
    return high

m, k = [int(x) for x in input().split()]
print(solve(m, k))

def dfs(n, k, cache = {}):
    # if number of bits is bigger than the number's bits of the number's bits is less than 0
    if k > n or k < 0: return 0
    # if num bits is 0 or num bits is equivalent to the number's bits
    if k == 0 or k == n: return 1
    # This optimization is not necessary but flips the 0s and the 1s
    # if k*2 > n: k = n-k
    # Check is already calculated
    if (n, k) in cache: return cache[(n, k)]
    # Use dfs addition for case where certain bit is 1 or certain bit is 0
    z = cache[(n, k)] = dfs(n-1, k-1) + dfs(n-1, k)
    return z

def bits(n):
    # counts number of 1s in the number
    b = 0
    while n:
        if n & 1: b += 1
        n >>= 1
    return b

def count(n, k):
    z, b, c = 0, 63, 0
    for b in reversed(range(64)):
        # Taking n and checking if certain bit is 1 or not
        # This sums for every mod power of 2 that exists to account for every case
        if (n>>b)&1:
            # calculates by subtracting for bits not accounted for
            z += dfs(b, k-c)
            c += 1
        # Unnecessary code
        # if not k: break
    # if original number has same number of 1s as digits required, add 1
    return z + (bits(n) == k)

def solve(m, k):
    # Binary Search for number 1-10^18
    low, high = 1, 10**18
    while low < high:
        mid = (low+high)//2
        if count(2*mid, k) - count(mid, k) < m:
            low = mid+1
        else:
            high = mid
    return high

m, k = [int(x) for x in input().split()]
print(solve(m, k))

from math import factorial as f
def C(n, m):
    if n < m: return 0
    return f(n) // ( f(n - m ) * f(m) )

m, k = list(map(int, input().split()))
ans = 1
for bit in reversed(list(range(65))):
    if k == 0:
        break
    if C(bit, k - 1) < m:
        ans += ( 1 << bit )
        m -= C(bit, k - 1)
        k -= 1
print(ans)


comb = [[0 for i in range(67)] for j in range(67)]

for i in range(67):
    comb[i][0], comb[i][i] = 1, 1
    for j in range(1, i):
        comb[i][j] = comb[i - 1][j - 1] + comb[i - 1][j]

def calc(x):
    cnt   = 0
    digit = []
    while (x > 0):
        digit.append(x % 2)
        x  //= 2
        cnt += 1
    ans, one = 0, 0
    for i in reversed(list(range(cnt))):
        if (digit[i] == 1):
            if (k - one >= 0):
                ans += comb[i][k - one]
            one += 1
    return ans

m, k = list(map(int, input().split()))

lcur, rcur = 0, 2 ** 64
while (lcur + 2 <= rcur):
    mid = (lcur + rcur) // 2
    if (calc(mid * 2) - calc(mid) < m):
        lcur = mid
    else:
        rcur = mid

print(rcur)



MX_BIT = 64
C = [[int(0) for i in range(MX_BIT)] for j in range(MX_BIT)]

def ck(x, i):
	return (x>>i) & 1
def tot_bits(x):
	x = bin(x)[2:]
	return len(x)
def mkt():
	C[0][0] = 1
	for i in range (1, MX_BIT):
		for j in range (i+1):
			C[i][j] = C[i-1][j] + (C[i-1][j-1] if j else 0)
def solve(x, k):
	a = 0
	for i in reversed(list(range(MX_BIT))):
		if ck(x, i) != 0:
			a += C[i][k]
			k -= 1
		if k == 0:
			break
	return a
mkt()
m, k = list(input().split())
m = int(m)
k = int(k)
l = 1
r = 1e18
if not m:
    l = 1
else:
    while l < r:
    	mid = int((l + r) // 2)
    	if (solve(2*mid, k) - solve(mid, k)) < m :
    		l = mid + 1
    	else:
    		r = mid
print(l)


from math import factorial as f
def C(n, m):
    if n < m: return 0
    return f(n) // ( f(n - m ) * f(m) )
 
m, k = map(int, input().split())
ans = 1
for bit in reversed(range(65)):
    if k == 0:
        break
    if C(bit, k - 1) < m:
        ans += ( 1 << bit )
        m -= C(bit, k - 1)
        k -= 1
print(ans)


import math

m, k = list(map(int, input().strip(' ').split(' ')))


def solve(x):
    ans = 0
    tot = 0
    for i in reversed(list(range(int(math.log2(x)+1)))):
        if x & (1 << i):
            ans += math.comb(i, k-tot)
            tot += 1
            if tot > k:
                return ans
    return ans


def judge(x):
    return solve(x*2)-solve(x) >= m


l, r = 1, 2
while not judge(r):
    l, r = r, r*2
ans = -1
while l <= r:
    mid = (l+r) >> 1
    if judge(mid):
        ans, r = mid, mid-1
    else:
        l = mid+1
print(ans)