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

Source: codeforces | Difficulty: HARD
Tags: Bit manipulation

Combinatorics

Sorting

Dynamic programming

Mathematics


Solutions:
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)


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)


def dfs(n, k, cache={}):
	if k > n or k < 0:
		return 0
	if k == 0 or k == n:
		return 1
	if (n, k) in cache:
		return cache[n, k]
	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)):
		if n >> b & 1:
			z += dfs(b, k - c)
			c += 1
		if not k:
			break
	return z + (bits(n) == k)

def solve(m, k):
	(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))


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(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 = 1e+18
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)


import sys

def b(n):
	c = 0
	while n:
		if n & 1:
			c += 1
		n //= 2
	return c
c = {}

def f(n, k):
	if (n, k) in c.keys():
		return c[n, k]
	if n == 1:
		return 1 if k == 1 else 0
	c[n, k] = f(n // 2, k) + f(n // 2, k - 1) + int(n & 1 and b(n // 2) == k - 1)
	return c[n, k]
(m, k) = map(int, input().split())
hi = int(1e+18)
lo = 1
while hi - lo >= 0:
	mid = (lo + hi) // 2
	val = f(mid, k)
	if val == m:
		print(mid)
		sys.exit()
	elif val < m:
		lo = mid + 1
	else:
		hi = mid - 1


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)


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))


N = 70
C = [[0 for _ in range(N)] for _ in range(N)]
for i in range(N):
	C[i][0] = C[i][i] = 1
	for j in range(1, i):
		C[i][j] = C[i - 1][j - 1] + C[i - 1][j]
(l, r) = (1, int(1e+19))
(m, k) = [int(x) for x in input().split(' ')]
k -= 1

def ok(x: int):
	s = bin(x)[2:]
	s = s[::-1]
	t = k
	ans = 0
	for i in range(len(s) - 1, -1, -1):
		if s[i] == '1':
			ans += C[i][t]
			t -= 1
			if t < 0:
				break
	return ans >= m
while l < r:
	mid = l + r >> 1
	if ok(mid):
		r = mid
	else:
		l = mid + 1
print(l)