数位DP+二分答案
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define MOD ((int)(1e9) + 7)
#define eps 1e-10
using namespace std;
typedef long long ll;
#define maxn 64
#define INF 1000000000000000000LL
ll C[maxn][maxn];
ll bit(ll mask, ll i) {
return (mask>>i)&1;
}
int bit_count(ll x) {
int cnt = 0;
while (x) {
cnt ++;
x &= (x-1);
}
return cnt;
}
ll dp(ll n, ll k) {
ll cnt = 0;
if (k == bit_count(n)) {
cnt ++;
}
for (int i = maxn-1; i >= 0 && k>=0; -- i) {
if (bit(n,i)) {
cnt += C[i][k--];
}
}
return cnt;
}
ll m, k;
int main() {
C[0][0] = 1;
for (int i = 1; i <= maxn-1; ++ i) {
for (int j = 0; j <= i; ++ j) {
C[i][j] = C[i-1][j] + ((j)?(C[i-1][j-1]):0);
}
}
while(cin >> m >> k) {
ll L = 1, R = INF, M = 1;
while (L < R) {
M = (R + L) / 2;
if (dp(2*M,k) - dp(M,k) < m) L = M+1; // [1,2*M] - [1,M] = [M+1,2*M]
else R = M;
}
cout << L << endl;
}
return 0;
}