Algorithm


D. Random Task
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 + 1n + 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 1018.

Input

The first line contains two space-separated integers, m and k (0 ≤ m ≤ 10181 ≤ k ≤ 64).

Output

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

Examples
input
Copy
1 1
output
Copy
1
input
Copy
3 2
output
Copy
5



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>

using namespace std;

long long m, k, c[65][65];

long long calc(long long x, int k) {
	long long ret = (__builtin_popcount(x) == k);
	for(int i = 63; i >= 0 && k > 0; --i)
		if(((x >> i) & 1) == 1)
			ret += c[i][k--];
	return ret;
}

long long can(long long mid) {
	return calc(mid * 2, k) - calc(mid, k);
}

int main() {
	for(int i = 0; i <= 64; ++i)
		for(int j = 0; j <= i; ++j)
			if(j == 0 || j == i)
				c[i][j] = 1;
			else
				c[i][j] = c[i - 1][j - 1] + c[i - 1][j];

	scanf("%lld %lld", &m, &k);

	if(m == 0) {
		puts("1");
		return 0;
	}

	long long l = 0, r = 2e18, mid, res;
	while(l <= r) {
		mid = (l + r) / 2;
		res = can(mid);
		if(res == m) {
			printf("%lld\n", mid);
			return 0;
		} else if(res > m)
			r = mid - 1;
		else
			l = mid + 1;
	}

  return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
1 1

Output

x
+
cmd
1
Advertisements

Demonstration


Codeforces Solution-Random Task-Solution in C, C++, Java, Python

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+