We need to find n such that in the set {n+1, n+2, ..., 2n} exactly m numbers have exactly k ones in binary representation.
Given m up to 1e18, k up to 64. Need n up to 1e18.
We need to find n with that property. It may be multiple, output any.
We need to design algorithm.
Observation: Consider interval [n+1, 2n]. This is a range of length n. It's known that for any positive integer n, the numbers from n+1 to 2n inclusive are exactly the numbers whose binary representation is something? Not exactly.
We need count of integers in [L,R] with exactly k ones. Let f(x) = number of integers in [1,x] with exactly k ones. Then count in [n+1,2n] = f(2n) - f(n). Since positive n, but careful: n+1 to 2n inclusive. So count = f(2n) - f(n). Because f counts from 1 to x inclusive. So we need f(2n) - f(n) = m.
Thus we need to find n such that difference equals m.
Define g(n) = f(2n) - f(n). We need to find n with g(n)=m, n <= 1e18.
We need to compute f(x) efficiently, maybe using combinatorial sum over bits. Since k up to 64, we can compute f(x) by standard method: count numbers <= x with exactly k ones by considering binary length.
Given that m can be up to 1e18, we need to find n. Possibly we can binary search on n? But g(n) is not monotonic? Let's check monotonicity.
f(2n)-f(n) might be non-decreasing? Consider n increasing, the interval [n+1,2n] shifts and expands. Typically, the number of numbers with exactly k ones in that interval might not be monotonic overall, but maybe it is monotonic for large n? Let's test small values.
k=1: we need numbers with exactly one 1 in binary (powers of two). In [n+1, 2n], how many powers of two? For n=1: [2,2] includes 2 (power of 2) -> count=1. For n=2: [3,4] includes 4 -> count=1. For n=3: [4,6] includes 4 -> count=1. For n=4: [5,8] includes 8? 8 is >2n=8? Actually 2n=8, inclusive includes 8, yes. Also maybe 4? 4 is <5, so not included. So count=1. For n=5: [6,10] includes 8? 8 is in [6,10] yes. count=1. So for k=1, g(n) seems often 1, but occasionally more? Let's test n=7: [8,14] includes 8? 8 is in [8,14] yes. Only 8? Also 4? no; 2? no. So count=1. n=8: [9,16] includes 16? 16 is in [9,16] yes, also 8? no. So count=1. So g(n)=1 for all n? Actually check n=0? not allowed. For n=1: [2,2] -> 2 (10) has one 1, count=1. For n=2: [3,4] -> 3 (11) has two ones, 4 (100) has one => count=1. n=3: [4,6] -> 4 (100) has one, 5 (101) has two, 6 (110) two -> count=1. n=4: [5,8] -> 5:2,6:2,7:3,8:1 -> count=1. n=15: [16,30] -> powers of two in that range: 16? yes (10000) and maybe 32? >30, so count 1. So seems g(n)=1 for all n? Wait what about n=0? not allowed. But could there be n where there are 0? For n=0? Then [1,0] empty. For positive n, there is always at least one power of two in [n+1, 2n]? Actually known result: For any positive integer n, there is always a power of two between n+1 and 2n inclusive? Let's examine: For n=1, between 2 and 2 includes 2. For n=2, between 3 and 4 includes 4. For n=3, between 4 and 6 includes 4. For n=5, between 6 and 10 includes 8. Indeed, by Bertrand's postulate for powers of two? Actually known: For any integer n>0, there exists a power of two in (n,2n]. Yes because 2^{floor(log2 n)+1} <= 2n and > n. So there is exactly one power of two? Could there be two? For n=3, powers: 2,4,8. 2 <=3, 4 in (3,6], 8 >6. Only one. For n=7, 2^3=8 in (7,14], 16 >14, so one. So exactly one power of two lies in (n,2n] for n>=1? Let's check n=1: (1,2] includes 2, ok. n=2: (2,4] includes 4? But 2 is not >2, so (2,4] includes 4 only. n=4: (4,8] includes 8? But 4 not >4, 8 is included? Since interval is (4,8] = {5,6,7,8}. 8 is included. So yes, exactly one power of two: the next power of two greater than n (i.e., 2^{ceil(log2(n+1))}) but actually the smallest power of two > n is <=2n? For n>0, 2^{⌈log2(n)⌉} <= 2n? Because if n is between 2^{t-1}+1 and 2^t, then next power is 2^t, and 2^t <= 2n? Since n > 2^{t-1}, then 2n > 2^t, so 2^t <=2n? Actually 2^t <= 2n? n > 2^{t-1} => 2n > 2^t, so 2^t could be less than 2n, but we need <=2n. For n = 2^{t-1}+1, 2n = 2^t +2, so 2^t <=2n. For n = 2^t? Then next power is 2^{t+1}, but is 2^{t+1} <= 2n = 2^{t+1}? Yes equal, so included. So indeed there is exactly one power of two in (n, 2n] for n>=1? Check n=2^{t}: interval (2^t, 2^{t+1}] includes 2^{t+1} which is a power of two. Also any other? Possibly 2^t? not > n. So exactly one. So g(n)=1 for all n when k=1. So for m=1, any n works? But sample input: m=1,k=1 output 1. So that matches.
Now for k>1, g(n) varies.
We need to find n such that g(n)=m.
Observation: f(2n)-f(n) is the count of numbers in (n,2n] with exactly k ones. So g(n)=|{x in N: n < x <= 2n, popcount(x)=k}|.
Given m up to 1e18, we need to find n.
We can think of solving equation f(2n)-f(n)=m.
f(x) can be computed for given x and k quickly using combinatorial DP: iterate bits of x from most significant to least, count numbers with exactly k ones that are <= x. That's O(log x * k) which is fine for k up to 64 and x up to 1e18 (log2~60). So computing g(n) for a given n is O(log n * k) ~ O(64*64) manageable.
But we need to find n given m. The range of n up to 1e18 is huge. Could we binary search? g(n) might not be monotonic; let's test for small k=2.
Compute g(n) for n from 1 to maybe 20:
k=2: count numbers with exactly 2 ones in (n,2n].
Let's list numbers with two ones (binary): 3 (11), 5 (101), 6 (110), 9 (1001), 10 (1010), 12 (1100), 17 (10001), 18 (10010), 20 (10100), 24 (11000), etc.
Compute g(1): (1,2] => numbers: 2 -> popcount=1 -> count 0? 2 has one 1, not 2. So g(1)=0? Wait 2 is included because n+1=2? Actually interval is [n+1, 2n] inclusive. For n=1: [2,2] => only 2, popcount=1 -> 0. So g(1)=0.
n=2: [3,4] => 3 (pop2) and 4 (pop1) => 1.
n=3: [4,6] => 4(1),5(2),6(2) => 2? Actually 5 and 6 have two ones => count=2.
n=4: [5,8] => 5(2),6(2),7(3),8(1) => 2.
n=5: [6,10] => 6(2),7(3),8(1),9(2),10(2) => 6,9,10: three numbers? 6,9,10 => popcount(6)=2, 9=1001 ->2, 10=1010->2 => count=3.
n=6: [7,12] => 7(3),8(1),9(2),10(2),11(3),12(2) => 9,10,12 => count=3.
n=7: [8,14] => 8(1),9(2),10(2),11(3),12(2),13(3),14(3) => 9,10,12 => count=3? Actually 12 (1100) pop2, 9 (1001) pop2, 10(1010) pop2 => 3.
n=8: [9,16] => 9(2),10(2),11(3),12(2),13(3),14(3),15(4),16(1) => 9,10,12 => count=3.
n=9: [10,18] => 10(2),11(3),12(2),13(3),14(3),15(4),16(1),17(2),18(2) => 10,12,17,18 => count=4.
n=10: [11,20] => 11(3),12(2),13(3),14(3),15(4),16(1),17(2),18(2),19(3),20(2) => 12,17,18,20 => count=4.
n=11: [12,22] => 12(2),13(3),14(3),15(4),16(1),17(2),18(2),19(3),20(2),21(3),22(3) => 12,17,18,20 => count=4? Also? 13? no; 14? no; So count=4.
n=12: [13,24] => 13(3),14(3),15(4),16(1),17(2),18(2),19(3),20(2),21(3),22(3),23(4),24(2) => 17,18,20,24 => count=4? plus maybe? 24 has two ones? 11000 -> yes 2 ones. So count=4? Let's list: 17,18,20,24 => 4. Also? 13,14,15,16,19,21,22,23 not. So 4.
n=13: [14,26] => 14(3),15(4),16(1),17(2),18(2),19(3),20(2),21(3),22(3),23(4),24(2),25(3),26(3) => 17,18,20,24 => count=4.
n=14: [15,28] => 15(4),16(1),17(2),18(2),19(3),20(2),21(3),22(3),23(4),24(2),25(3),26(3),27(4),28(3) => 17,18,20,24 => count=4.
n=15: [16,30] => 16(1),17(2),18(2),19(3),20(2),21(3),22(3),23(4),24(2),25(3),26(3),27(4),28(3),29(4),30(4) => 17,18,20,24 => count=4? Also? 30 has popcount 4? 11110? Actually 30=11110 popcount=4, so not. So 4.
n=16: [17,32] => 17(2),18(2),19(3),20(2),21(3),22(3),23(4),24(2),25(3),26(3),27(4),28(3),29(4),30(4),31(5),32(1) => 17,18,20,24 => count=4.
n=17: [18,34] => 18(2),19(3),20(2),21(3),22(3),23(4),24(2),25(3),26(3),27(4),28(3),29(4),30(4),31(5),32(1),33(2),34(2) => add 33 (100001 pop2), 34 (100010 pop2) => So count=6? Let's list all with pop2: 18,20,24,33,34. Also maybe? 17? No not included because start at 18. 22? pop3, 25? pop3, 26? pop3, etc. So 18,20,24,33,34 => that's 5? Wait 18 included, 20,24,33,34. Also maybe 21? pop3. So 5. But check 18 (10010) pop2 yes, 20 (10100) pop2, 24 (11000) pop2, 33 (100001) pop2, 34 (100010) pop2. That's 5. But also 36? >34. So g(17)=5. So g(n) increased from 4 to 5 at n=17.
We see g(n) is not strictly monotonic: it was 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,... Actually from n=9 to 16 it stayed at 4, then increased to 5 at 17.
Seems g(n) is non-decreasing? Let's check earlier: n=1:0, n=2:1 (increase), n=3:2 (increase), n=4:2 (equal), n=5:3 (increase), n=6:3 (equal), n=7:3 (equal), n=8:3 (equal), n=9:4 (increase), n=10:4 (equal), n=11:4 (equal), n=12:4 (equal), n=13:4 (equal), n=14:4 (equal), n=15:4 (equal), n=16:4 (equal), n=17:5 (increase). So it seems g(n) is non-decreasing. Could it ever decrease? Let's test n=18: [19,36] => numbers with pop2: 20? Actually 19 to 36 inclusive. Which numbers with exactly two ones in that range? Starting from 19: 19 (10011) pop3? Actually 19=10011 popcount=3, 20 (10100) pop2, 21 (10101) pop3, 22 (10110) pop3, 23 (10111) pop4, 24 (11000) pop2, 25 (11001) pop3, 26 (11010) pop3, 27 (11011) pop4, 28 (11100) pop3, 29 (11101) pop4, 30 (11110) pop4, 31 (11111) pop5, 32 (100000) pop1, 33 (100001) pop2, 34 (100010) pop2, 35 (100011) pop3, 36 (100100) pop2. So count: 20,24,33,34,36 -> also maybe? 40? out. Also check 18? not included. So total 5? That is same as 5? n=17 gave 5; n=18 also 5. So non-decreasing so far.
Let's test a possible decrease: Consider n=31? But I suspect g(n) is non-decreasing. Is that always true? For increasing n, the interval shifts right and expands. The set of numbers with popcount k is sparse, but as n grows, you may lose some numbers at the left but gain more at the right. Intuitively, since the interval length is n, which grows, the count should generally increase or stay same. However, there might be cases where the interval shifts and loses a cluster of such numbers while gaining fewer? But because interval length increases, it's plausible that count is non-decreasing. Let's try to prove:
We want to show: for n>=1, g(n+1) >= g(n). That is f(2(n+1))-f(n+1) >= f(2n)-f(n).
Alternatively, consider the intervals: A_n = (n, 2n], A_{n+1} = (n+1, 2n+2].
We can compare sets: A_{n+1} = ( (n,2n] minus {n+1} ) union {2n+1, 2n+2}? Actually careful: A_n = {x: n < x <= 2n}. A_{n+1} = {x: n+1 < x <= 2n+2}. So A_{n+1} = (A_n \ {x = n+1?})? Since n+1 is in A_n? Because n < n+1 <= 2n? For n>=1, yes n+1 <= 2n? That's equivalent to 1 <= n, true for n>=1. So n+1 ∈ A_n. Also numbers 2n+1, 2n+2 are in A_{n+1} but not in A_n. Also any numbers between? The set A_n includes numbers up to 2n. A_{n+1} includes numbers from n+2 to 2n, plus 2n+1,2n+2. So indeed:
A_{n+1} = (A_n \ {n+1}) ∪ {2n+1, 2n+2}.
Therefore, g(n+1) = g(n) - (popcount(n+1)==k? 1:0) + (popcount(2n+1)==k? 1:0) + (popcount(2n+2)==k? 1:0).
So g(n+1) - g(n) = - I(popcount(n+1)=k) + I(popcount(2n+1)=k) + I(popcount(2n+2)=k).
We need to determine if this is always >=0? Not necessarily; it could be negative if I(popcount(n+1)=k) is 1 and both of the added terms are 0. So g could decrease. For example, if n+1 has popcount k but neither 2n+1 nor 2n+2 have popcount k, then g(n+1)=g(n)-1, decrease.
Does that happen? Let's test for small k. For k=2, we saw g didn't decrease. Could it decrease for some k? Possibly. Let's try find n where n+1 has popcount k, but 2n+1,2n+2 do not. For k=2, try n+1=3 (pop2). n=2: g(2)=1, n+1=3 pop2, then 2n+1=5 pop2, 2n+2=6 pop2. So three terms: -1 +1+1 = +1, increase. For n+1=5 (pop2) n=4: g(4)=2, n+1=5 pop2, 2n+1=9 pop2, 2n+2=10 pop2 => -1+1+1=+1. For n+1=6 (pop2) n=5: n+1=6 pop2, 2n+1=11 pop3, 2n+2=12 pop2 => -1+0+1=0, so no decrease. For n+1=9 (pop2) n=8: n+1=9 pop2, 2n+1=17 pop2, 2n+2=18 pop2 => -1+1+1=+1. For n+1=10 (pop2) n=9: n+1=10 pop2, 2n+1=19 pop3, 2n+2=20 pop2 => -1+0+1=0. For n+1=12 (pop2) n=11: n+1=12 pop2, 2n+1=23 pop4, 2n+2=24 pop2 => -1+0+1=0. For n+1=17 (pop2) n=16: n+1=17 pop2, 2n+1=33 pop2, 2n+2=34 pop2 => -1+1+1=+1. For n+1=18 (pop2) n=17: n+1=18 pop2, 2n+1=35 pop3, 2n+2=36 pop2 => -1+0+1=0. So no decrease observed for k=2.
But can we construct a case where both 2n+1 and 2n+2 have popcount not equal to k while n+1 has popcount k? Possibly for some k > 1. Let's try k=3. We'll check small n manually maybe find a decrease.
We need n such that popcount(n+1)=3, popcount(2n+1) !=3, popcount(2n+2) !=3. For n+1 small, let's test n+1=7 (111) pop3, n=6. Then 2n+1=13 (1101) pop3? 13 binary 1101 popcount=3, so that's 3 -> would give +1. n+1=11 (1011) pop3, n=10: 2n+1=21 (10101) pop3? 21=10101 popcount=3, gives +? Actually 21 popcount=3 -> +1, 2n+2=22 (10110) popcount=3? 10110 has three 1s? 1+0+1+1+0=3, yes 3. So -1+1+1=+1. n+1=13 (1101) pop3, n=12: 2n+1=25 (11001) pop3? 11001 popcount=3 -> +1, 2n+2=26 (11010) pop3? 11010 popcount=3? 1+1+0+1+0=3, yes. So +1. n+1=14 (1110) pop3, n=13: 2n+1=27 (11011) pop4? Actually 27=11011 popcount=4? 1+1+0+1+1=4, not 3. 2n+2=28 (11100) pop3? 11100 popcount=3? 1+1+1+0+0=3, yes. So -1+0+1=0. n+1=19 (10011) pop3, n=18: 2n+1=37 (100101) pop3? 37=100101 popcount=3? 1+0+0+1+0+1=3, yes. 2n+2=38 (100110) pop3? 1+0+0+1+1+0=3, yes. So +1. n+1=21 (10101) pop3, n=20: 2n+1=41 (101001) pop3? 101001 popcount=3? 1+0+1+0+0+1=3, yes. 2n+2=42 (101010) pop3? 101010 popcount=3? 1+0+1+0+1+0=3, yes. So +1. n+1=22 (10110) pop3, n=21: 2n+1=43 (101011) pop4? 101011 popcount=4? 1+0+1+0+1+1=4, not 3. 2n+2=44 (101100) pop3? 101100 popcount=3? 1+0+1+1+0+0=3, yes. So -1+0+1=0. n+1=25 (11001) pop3, n=24: 2n+1=49 (110001) pop3? 110001 popcount=3? 1+1+0+0+0+1=3, yes. 2n+2=50 (110010) pop3? 110010 popcount=3? 1+1+0+0+1+0=3, yes. So +1.
It seems often at least one of 2n+1 or 2n+2 has popcount k when n+1 does. Could there be case where both are not k? For k=3, we need numbers such that both 2n+1 and 2n+2 have popcount !=3. When does that happen? Let's try n+1=28 (11100) pop3? Actually 28=11100 popcount=3? 1+1+1+0+0=3, yes. n=27: 2n+1=55 (110111) pop5? 55=110111 popcount=5? 1+1+0+1+1+1=5, not 3. 2n+2=56 (111000) pop3? 111000 popcount=3? 1+1+1+0+0+0=3, yes. So still one.
n+1=35 (100011) pop3? 35=100011 popcount=3? 1+0+0+0+1+1=3, yes. n=34: 2n+1=69 (1000101) pop3? 1000101 popcount=3? 1+0+0+0+1+0+1=3, yes. 2n+2=70 (1000110) pop3? 1000110 popcount=3? 1+0+0+0+1+1+0=3, yes. So both are 3? Actually both are 3? 69=1000101 -> three 1s; 70=1000110 -> three 1s. So both.
We need both not 3. Could be n+1=??? Might be possible, but not sure.
But is it proven that g(n) is non-decreasing? Let's check for k=1, we know g(n)=1 constant (non-decreasing). For k=2, we saw non-decreasing. However, I recall a known result: The function f(n) = number of integers in (n,2n] with exactly k ones is non-decreasing in n. Is that a known property? Possibly related to the fact that as n increases, you always gain at least as many as you lose. The recurrence we derived: g(n+1) - g(n) = -I(pop(n+1)=k) + I(pop(2n+1)=k) + I(pop(2n+2)=k). So if we can show that I(pop(2n+1)=k) + I(pop(2n+2)=k) >= I(pop(n+1)=k) for all n and k, then g is non-decreasing. Is that always true? Let's test potential counterexample: we need pop(n+1)=k but both pop(2n+1) and pop(2n+2) are not k. Let's attempt to search programmatically in mind for small k and n. But we can reason about binary representation.
Let x = n+1. Then 2n+1 = 2(n+1)-1 = 2x-1. And 2n+2 = 2x. So the condition becomes: if popcount(x) = k, then is it always true that at least one of popcount(2x-1) or popcount(2x) equals k? Or equivalently, for any positive integer x, popcount(x)=k implies popcount(2x-1)=k or popcount(2x)=k. Is that true? Let's examine.
2x is just x shifted left by 1, so popcount(2x) = popcount(x). So popcount(2x) = k exactly when popcount(x)=k. So actually popcount(2x) = popcount(x). Because multiplying by 2 adds a trailing zero, doesn't change number of ones. So if popcount(x)=k, then popcount(2x)=k automatically! Wait, but 2x = 2(n+1) = 2n+2, which is in the added set. So I(pop(2n+2)=k) = I(pop(2x)=k) = I(pop(x)=k) = 1. Therefore, for any n, if pop(n+1)=k, then automatically pop(2n+2)=k. That's a key insight! Because 2n+2 = 2(n+1) and popcount is invariant under left shift (multiplying by power of two). Right! So in the recurrence, I(pop(2n+2)=k) always equals I(pop(n+1)=k). So the difference becomes:
g(n+1)-g(n) = -I(pop(n+1)=k) + I(pop(2n+1)=k) + I(pop(2n+2)=k)
= -I(pop(x)=k) + I(pop(2x-1)=k) + I(pop(2x)=k)
= -I(pop(x)=k) + I(pop(2x-1)=k) + I(pop(x)=k)
= I(pop(2x-1)=k).
Therefore, g(n+1) - g(n) = I(pop(2(n+1)-1)=k) = I(pop(2x-1)=k) where x=n+1.
So the difference is always either 0 or 1, and it equals 1 precisely when popcount(2x-1)=k, i.e., popcount(2n+1)=k.
That's great! Because we have simplified:
g(n+1) = g(n) + (1 if popcount(2n+1) = k else 0).
And note: g(1) = f(2)-f(1). Since f(1) counts numbers <=1 with exactly k ones. For k>=1, f(1) = 1 if k==1 else 0. f(2) counts numbers <=2 with exactly k ones. So g(1) can be computed accordingly.
Thus g(n) is a non-decreasing function (in fact, it never decreases, it increases by either 0 or 1 each step). So it's a step function that increments exactly when 2n+1 has popcount k.
Therefore, g(n) is monotonic increasing (non-decreasing) and the increments are at most 1 per step. So g(n) takes all integer values from its minimum up to its maximum (or maybe some gaps if increments are zero for long intervals, but since increments are only 0 or 1, the range of g(n) is a set of consecutive integers? Not necessarily: if it increases by 1 at some n, then all values from g(1) up to some max are attained because it never decreases, but if it stays constant for many steps, it still eventually increases, so the set of values attained is an interval of integers from min to max, provided it eventually reaches each integer? Actually since it can only increase by 1 each step, as n increases by 1, g either stays same or increments by 1. Therefore, the set of values taken by g(n) for n >= 1 is exactly all integers from g(1) up to limsup, as long as g eventually increases to each value. Since it's monotonic and increments are by exactly 1 at some steps, it will cover every integer between its initial value and its eventual supremum, as long as it goes to infinity? But does g(n) go to infinity as n->∞? Probably yes, because the number of numbers with popcount k in (n,2n] grows roughly like something like (log n)^{k-1}/(k-1)! * something? Actually count of numbers with exactly k ones up to N is ~ (log2 N)^{k-1}/((k-1)! * ln 2?) times N? Wait the total count of numbers with exactly k ones up to N is approximately (log N)^{k-1}/( (k-1)! ) times something? Actually the proportion of numbers with exactly k ones among numbers up to N is about (C(log N, k-1))/2^{log N}? That's not right. Let's recall: The number of integers in [0, N] with popcount exactly k is O(log N)^{k-1} * something? Actually it's known that the number of integers with exactly k ones up to N is roughly (log_2 N)^{k-1}/(k-1)! * (N / log 2?) No, that seems off. Because for fixed k, the set of numbers with exactly k ones is sparse; count ~ (log N)^k / k! maybe? Let's derive: For a given length L (bits), there are C(L, k) numbers with exactly k ones and leading bit 1 (since numbers of length L bits, i.e., in [2^{L-1}, 2^L - 1]) have exactly k ones, including the leading 1, so number of such numbers is C(L-1, k-1). Summing over L up to floor(log2 N)+1 gives sum_{L=1}^{Lmax} C(L-1, k-1) = C(Lmax, k) maybe? Actually sum_{i=0}^{M} C(i, k-1) = C(M+1, k). So total numbers up to N (if N is 2^L -1) is about C(L, k). Since L ~ log2 N, that's polynomial in log N, not linear in N. So indeed for fixed k, count of numbers with exactly k ones up to N is Theta((log N)^k). So f(N) is about (log_2 N)^k / k!. So then g(n) = f(2n)-f(n) is about (log_2 (2n))^k / k! - (log_2 n)^k / k! = ( (log2 n + 1)^k - (log2 n)^k ) / k! ~ k (log2 n)^{k-1} / k! = (log2 n)^{k-1} / (k-1)!. So g(n) grows like (log n)^{k-1}. So as n→∞, g(n)→∞, albeit slowly.
Since g(n) is non-decreasing and increments by at most 1, and tends to infinity, it must eventually cover all integers from its starting value upward. That is, for any m >= g(1), there exists some n such that g(n)=m. However, m can be 0 up to 1e18; but g(1) might be >0 for some k? Let's compute g(1) for various k: n=1: interval [2,2] only number 2. popcount(2)=1. So for k=1, g(1)=1. For k>1, g(1)=0. So g(1)=0 for k>=2. For k=1, g(1)=1. And m can be 0? For k=1, m is given; but sample m=1,k=1 yields n=1. If m=0 for k=1, would there be n? Since g(n)=1 for all n, no. But problem statement says "Find such positive integer n, that among numbers n+1,...,2n there are exactly m numbers ...". It doesn't guarantee existence for all m,k? But the problem says "Sasha guaranteed that there is an answer that doesn't exceed 1e18." So for given input m,k, there exists at least one n <= 1e18 satisfying. So we can assume existence.
Now we have monotonic g(n). So we can binary search for n such that g(n)=m. Since g is non-decreasing, we can find smallest n with g(n) >= m, then check if equality holds. Because g takes all integer values from some point? But does g skip any values? Since g increments by 0 or 1, it's non-decreasing and can stay constant for several n then increase by 1. Thus it will attain each integer value between its minimum and its eventual maximum, as long as it eventually increases to cover all. Because if it stays at value v for some range, then at next increase it becomes v+1, so v+1 is attained. So all integers from initial to final are attained. Since g goes to infinity, it attains all integers >= g(1). For k>=2, g(1)=0, so all m >=0 are attainable? But does g(n) eventually cover every nonnegative integer? Possibly yes, because increments occur when 2n+1 has popcount k. But could there be a gap where some integer M is never attained? That would require that after some n, g jumps from v to v+2 without ever being v+1, but increments are only 0 or 1, so impossible. So the set of values is all integers from min onward.
Wait: Starting from n=1, g(1)=0 (k>=2). Then as n increases, g(n) may remain 0 for a while, then eventually becomes 1, then 2, etc. Since increments are at most 1, the set of values is contiguous. So for any m >=0, there exists n such that g(n)=m. But is it true for all m up to 1e18? g(n) grows like (log n)^{k-1}. To reach m = 1e18, we would need n such that (log2 n)^{k-1} ~ 1e18 * (k-1)!. For k=2, (log2 n) ~ 1e18, so n ~ 2^{1e18}, which is astronomically huge, far beyond 1e18. So not all m are attainable with n <= 1e18. But problem guarantees that for given input there is a solution n <= 1e18. So we need to find n up to 1e18 only. So m may be up to 1e18 but only those that can be achieved within that bound.
Thus we need to find n such that g(n)=m, with n <= 1e18.
Since g is monotonic, we can binary search for n. But we need to be able to compute g(n) quickly for any n up to 1e18. That is feasible using f(2n)-f(n) computed via digit DP.
But binary search over n from 1 to 1e18 (approx 2^60) requires about 60 iterations. Each iteration computing f(x) for x up to 2e18 (since 2n up to 2e18) but we can still handle with 64-bit integers. For each f(x) we need to count numbers <= x with exactly k ones. That can be done in O(L * k) where L ~ 60, k up to 64. That's fine. So overall time acceptable.
But is g(n) strictly monotonic? It is non-decreasing, but it may have plateaus. So binary search for equality may need careful: if we want n such that g(n)=m, we can find the smallest n with g(n) >= m (let's call n_low). Then if g(n_low) == m, output n_low (or any n with that value). Since we can output any n, we can output n_low. But need to ensure existence: since m is attainable, there is at least one n with g(n)=m. The smallest such n will have g(n)=m? Not necessarily: the smallest n with g(n) >= m could be > smallest n with g(n)=m if there is a plateau? Actually if g jumps from m-1 to m at some n0, then the smallest n with g(n) >= m is exactly n0 and g(n0)=m. However, if there is a plateau where g(n) stays at m for a range, the smallest n with g(n) >= m is the first n where g becomes m, so equality holds. If g never equals m (which we assume not), then no. So with existence guaranteed, binary search for the first n with g(n) >= m yields a valid answer (since g(n) will be exactly m at that n, else if it jumps from less than m to greater than m, that would be a jump by more than 1, which cannot happen because increments are at most 1. So the only way g(n) could be > m at the first n with >= m is if g jumps from < m to > m, which would be an increment of at least 2, impossible. Therefore, the first n with g(n) >= m will have g(n) = m. So binary search works.
But we must be careful about boundaries: the smallest possible n is 1. Largest n we consider maybe up to 1e18. But we need to guarantee that for the given m (which is guaranteed to be achievable with n <= 1e18), the binary search within [1, 1e18] will find it. However, if m is achievable but the smallest n achieving it might be > 1e18? The problem says there is an answer <=1e18. So at least one n <=1e18 satisfies g(n)=m. So the set of n with g(n)=m within [1,1e18] is non-empty. But the binary search for the smallest n with g(n)>=m might return the smallest n overall (which might be <=1e18 if the smallest such n is <=1e18). However, it's possible that the smallest n with g(n)>=m is >1e18, but there exists some larger n (still <=1e18?) that also satisfies? Actually if the smallest n achieving g(n)>=m is >1e18, then no n <=1e18 can have g(n)>=m? But we know there is n <=1e18 with g(n)=m. That would require that the smallest n with g(n)>=m is <= that n, so it must be <=1e18 as well. Because if the smallest n with g(n)>=m is >1e18, then for all n<=1e18, g(n) < m, contradiction to existence of n<=1e18 with g(n)=m. So the smallest n with g(n)>=m must be <=1e18. So binary search over [1,1e18] will find it.
Thus algorithm:
Given m,k.
Define function count(x) = number of positive integers ≤ x with exactly k ones. (We'll treat 0 separately, but since x>=1, we can include 0? Actually numbers from 1 to x, but 0 has 0 ones, not k usually, so we can ignore.)
Then g(n) = count(2n) - count(n).
Binary search for smallest n such that g(n) >= m. Then output that n.
We need to compute count(x) efficiently.
Implementation details:
We'll work with 64-bit integers (unsigned long long). Since x can be up to 2e18, which fits in 64-bit (max ~9.22e18). So OK.
count(x): typical DP: let bits be binary representation of x, from most significant to least. Let len = number of bits (maybe 0-index). We'll iterate position i from 0 to len-1 (or from most significant). Keep variable "taken" count of ones placed so far. For each bit, if current bit is 1, we can consider setting this bit to 0 and then fill the remaining positions arbitrarily with exactly (k - taken) ones (provided taken <= k). The number of ways is C(remaining bits, need). Then we set this bit to 1 (since actual bit is 1) and increment taken and continue. At the end, after processing all bits, if taken == k, then include the number x itself (since it is <= x). Alternatively, we can incorporate at the end.
Edge cases: k may be 0? But problem says 1 ≤ k ≤ 64. So k>=1. So we don't need k=0.
Also, if k > number of bits of x, then count(x)=0 (since cannot have that many ones). So early return.
C(n, r) need to be computed for n up to maybe 60 (bits). r up to 64. But we might need n up to 64? Actually bits of numbers up to 2e18 is at most 61 (since 2^61 ≈ 2.3e18, 2^62≈4.6e18, so 2e18 < 2^61? Actually 2^60 ≈ 1.15e18, 2^61≈2.3e18, so max bits length for 2e18 is 61. So n <= 61. But k can be up to 64. So for k>61, count(x) will be 0 for all x up to 2e18. But wait, if k=64, then we need numbers with exactly 64 ones; the smallest such number is 2^64 -1, which is about 1.84e19, >2e18. So count(x) =0 for x <=2e18. Then g(n)=0 for all n <=1e18. So if m=0, we can output any n (like 1). If m>0, there is no solution. But problem guarantees existence, so for k>something, m must be 0 maybe? But they say there is an answer <=1e18. If k>61, then g(n) might be 0 for all n, so only m=0 possible. The constraints allow m up to 1e18, but with guarantee existence. So for k>61, m must be 0. That's fine.
We must also handle k up to 64, but binomial coefficients for n up to 64 are within 2^64? Actually C(64,32) is huge ~1.8e18, fits in 64-bit? C(64,32) = 1832624140942590534 ~1.83e18, fits within 2^63-1? 9e18, yes. But our n max is 61, so even safer. However, we might need C(60, r) which fits in 64-bit. So we can precompute binomial table up to 64 using Pascal's triangle with 64-bit, careful for overflow: but values for n=64, r=32 still within 2^64-1? Actually 2^64-1 is about 1.84e19, and C(64,32) is about 1.83e18, okay. But we only need up to n=61, safer. So we can precompute C[n][r] for n up to 64.
But also count function may need to handle numbers up to 2e18, which have up to 61 bits. So n_max for binomial = 60 (remaining bits after current position). Actually we need up to 60 maybe.
Implementation plan:
-
Precompute combinations C[0..64][0..64] using long long. Use simple DP: C[i][0]=C[i][i]=1, C[i][j]=C[i-1][j-1]+C[i-1][j] (check for overflow but safe within 64-bit for i<=64).
-
Function count(x, k):
if k == 0? not needed but could handle.
if k > 60? Actually if k > number of bits of x (which we can compute as floor(log2(x)) +1) then return 0.
Convert x to binary bits array (most significant first). Let len = bits.size().
Initialize ans = 0, ones = 0.
For i from 0 to len-1:
if bits[i] == 1:
// If we set this bit to 0, then remaining positions = len - i - 1.
// We need to select (k - ones) ones among these remaining positions.
// If ones <= k and (k - ones) <= remaining bits, then add C[remaining][need].
int remaining = len - i - 1;
int need = k - ones;
if need >= 0 && need <= remaining:
ans += C[remaining][need];
// Then set this bit to 1
ones++;
// else bit is 0: nothing to do (cannot set to 1 because that would exceed x)
After loop, if ones == k, ans++ (include x itself).
return ans.
Note: x is positive, so ignore 0.
Now g(n) = count(2n, k) - count(n, k). Since 2n may overflow beyond 1e18? n up to 1e18, 2n up to 2e18, still within 64-bit (max unsigned 2^64-1 ~1.8e19). Use unsigned long long or __int128 for safety? 2e18 < 9.22e18 (max signed 64-bit), so fits in signed 64-bit as well. So use long long (signed). But careful: 21e18 = 2e18 < 2^63-1 ≈9.22e18, safe.
Now binary search:
Set low = 1, high = 1e18 (maybe we can set high as something like 1e18, but we need to ensure that for n=high, g(n) >= m if solution exists? Not necessary, we just search for first n with g(n) >= m, but we need to bound the search. Since we know there exists some n <= 1e18 with g(n) = m, the smallest n with g(n) >= m is <= that n, so it's <=1e18. So we can set high = 1e18. But we also need to ensure that g(high) >= m to have a valid point where condition holds. However, the binary search for first n with g(n) >= m works if we know that for some n in [low, high] g(n) >= m. Since solution exists, the smallest such n is within range, so at that n g(n) = m >= m. So g(high) might be less than m if high is 1e18? Actually we need to guarantee that at high, g(high) >= m, otherwise the binary search might not find a point. But we can set high to the maximum possible n (1e18) but we cannot guarantee that g(1e18) >= m for all m that have a solution. For example, suppose m is very large but still there exists n <= 1e18 with g(n)=m. Since g is non-decreasing, if there is some n0 <= 1e18 with g(n0)=m, then for any n >= n0, g(n) >= m (since non-decreasing). Therefore, for high = 1e18 >= n0, we have g(high) >= g(n0) = m. So indeed, if solution exists with n <= 1e18, then g(1e18) >= m. So high = 1e18 works. Good.
But wait: what if the smallest n with g(n)=m is >1e18? But problem says there exists n <= 1e18 with g(n)=m. That means there is some n0 <= 1e18 with g(n0)=m. So the smallest n with g(n) >= m is <= n0, hence <= 1e18. Also g(1e18) >= g(n0) = m. So yes.
Thus binary search on [1, 1e18] inclusive.
Implementation of binary search:
We want smallest n such that g(n) >= m.
while low < high:
mid = (low + high) // 2
if g(mid) >= m:
high = mid
else:
low = mid + 1
After loop, low is candidate. Compute g(low). If g(low) == m, output low. Else? Should not happen due to monotonicity and existence.
But we must be careful about overflow in 2mid when mid near 1e18: mid up to 1e18, 2mid up to 2e18, safe.
Now complexity: binary search about 60 iterations, each computing count twice (for n and 2n). Each count is O(lenk) ~ 6064 = 3840 operations, times 120 ~ 460k, fine.
But we must also consider that k up to 64, but if k > 60, count always 0 for numbers up to 2e18. So g(n)=0 always. Then for m=0, any n works. Our binary search will check g(mid) >= 0, which is always true (since 0>=0). So low will become 1 (since g(1)>=0) and binary search will return 1. That works. But we need to ensure that for m>0 and k>60, there is no solution, but problem guarantees existence, so such input won't occur.
Thus algorithm seems straightforward.
But we must confirm monotonicity thoroughly: derived g(n+1)-g(n) = I(popcount(2n+1)=k). Since 2n+1 = 2(n+1)-1, popcount(2x-1) maybe not always 0/1, but it's indeed 0 or 1. So g is non-decreasing, increments by 0 or 1. Great.
Edge Cases: k=0? Not allowed. m=0? Possibly. For k>=2, g(1)=0, so m=0 works (n=1). For k=1, g(1)=1, so m=0 might not be achievable? But maybe for some n? Let's check k=1: we argued g(n)=1 for all n? Actually is it always 1? Let's test n=0? Not allowed. For n=1: [2,2] count=1. n=2: [3,4] count=1. n=3: [4,6] includes 4 only? 4 popcount=1, also maybe 8? no. So yes 1. Could there be n where count is 0? For n=1, it's 1. So for k=1, g(n)=1 for all n>=1. So m=0 not achievable. But problem guarantee existence for given input, so if k=1 and m=0, it wouldn't be given (or maybe there is n? Let's double-check: Could there be n such that no power of two lies in (n,2n]? No, as argued always exactly one. So g(n) always 1. So m=0 not possible. So input won't have m=0 with k=1 given guarantee. But constraints say 0 ≤ m ≤ 1e18, so it's allowed but may not have solution; but they guarantee existence, so such inputs won't appear. So we are fine.
Now need to implement count function carefully.
Potential Pitfall: For large k (e.g., 60), binomial coefficients up to C(60,30) fit in 64-bit? C(60,30) is about 1.18e17, well within. So precomputation up to 64 is safe.
But we need to compute count for x up to 2e18, which has up to 61 bits. For n=61, C(60,30) still fine.
Implementation details:
-
Use vector bits; while x>0: bits.push_back(x&1) then reverse.
-
For count: iterate i from 0 to len-1, with remaining = len - i - 1.
Edge Cases: x=0? not used.
-
When k > len, we can early return 0. But careful: if k == len? For x having len bits, maximum ones possible is len (all bits 1). So if k <= len, still possible.
-
At the end, if ones == k, ans++ includes x itself.
Now about binomial: we can compute using iterative or precompute with 64-bit.
Now test with given examples:
Example 1: m=1,k=1.
Binary search: compute g(1)=? count(2)= count numbers <=2 with pop1: numbers: 1 (1) and 2 (10) both pop1, so count(2)=2; count(1)= numbers<=1 with pop1: 1 ->1. So g(1)=1. Already >=1, binary search finds n=1.
Example 2: m=3,k=2.
We computed earlier: g(5)=3 (n=5). Let's compute count(10) - count(5). Let's compute count(10): numbers up to 10 with exactly 2 ones: list: 3(11),5(101),6(110),9(1001),10(1010). That's 5. count(5): numbers up to 5 with exactly 2 ones: 3,5. That's 2. So g(5)=3. Works.
Now binary search should find n=5.
But is there a smaller n with g(n)>=3? g(4)=2, g(3)=2, g(2)=1, g(1)=0. So smallest with >=3 is 5. Good.
Now we need to ensure binary search works for large m. Since m up to 1e18, but g(n) grows slowly for larger k, but as guaranteed existence with n <=1e18, binary search will still be okay.
But there might be an issue: computing g(n) for n near 1e18 with large k might involve binomial coefficients up to C(60, something). That's fine.
Now verify the recurrence g(n+1) = g(n) + (popcount(2n+1)==k). This suggests that g(n) is essentially the prefix sum of indicators for odd numbers (2n+1). Since g(1)=f(2)-f(1). For n=1, g(1) = I(popcount(2)=k)? Actually not exactly, but we can compute directly.
Now confirm for k=2, we can compute g(1)=0, g(2)=1 (since 2*1+1=3 popcount=2? 3 pop2 -> yes). So increment from 0 to 1 at n=1? Actually g(2) = g(1) + I(popcount(3)=2) = 0 + 1 =1 matches.
Our binary search method works.
Potential overflow: When computing 2mid, use long long; but mid could be up to 5e17, 2mid up to 1e18, fine. But careful: mid up to 1e18, 2*mid = 2e18, still < 2^63-1 (9.22e18), safe.
Now count function for x up to 2e18: we need to get bits. Could use builtin functions to get highest bit.
Implementation in C++:
-
Precompute C[65][65] as long long.
-
count(x,k):
if (k == 0) return 1? not needed.
if (x == 0) return 0.
vector bits; or use for loop extracting bits.
Alternative: Use while (x) store bits.
Pseudo:
long long count(long long x, int k) {
if (x < (1LL << (k-1)))? Not reliable. Better compute bits length.
vector<int> b;
long long temp = x;
while (temp) {
b.push_back(temp & 1);
temp >>= 1;
}
reverse(b.begin(), b.end());
int len = b.size();
if (k > len) return 0;
long long ans = 0;
int ones = 0;
for (int i = 0; i < len; i++) {
if (b[i] == 1) {
int rem = len - i - 1;
int need = k - ones;
if (need >= 0 && need <= rem) {
ans += C[rem][need];
}
ones++;
}
// else: bit 0 -> cannot set to 1 without exceeding, so skip
if (ones > k) break; // early break
}
if (ones == k) ans++;
return ans;
}
But careful: The numbers we count are positive integers from 1 to x inclusive. Does this include 0? Not, but it's okay because we start from 1. However, the combinatorial count we add when we set current bit to 0 and fill rest includes numbers that may have leading zeros? In the standard method for numbers from 0 to x inclusive, the approach counts numbers with exactly k ones, including 0. But we need numbers from 1 to x. Since 0 has popcount 0, not k>=1, it doesn't affect. So it's fine.
Edge case: x=0, not used.
Now g(n) = count(2*n,k) - count(n,k). Works.
Now we need to ensure binary search loop correct with boundaries. Since n must be positive integer, low=1, high=1e18. But we must consider that for k large, g(1e18) could be 0. If m=0, then g(1e18) >= 0 true, binary search will converge to 1? Let's see: We search for smallest n with g(n) >= m (0). Since g(1) = 0 for k>=2, condition holds at n=1, so low will become 1. For k=1, g(1)=1, but m=0 won't be given. So it's fine.
But if m>0 and k large such that g(1e18) < m, then there is no solution, but guarantee says there is, so that won't happen.
Now about time: 60 * 2 * 60 * 64 ~ 460k operations, plus binomial precomputation trivial.
Potential optimizations: Use 128-bit for intermediate in binomial? Not needed.
Now we should test with some random cases to ensure correctness.
But before coding, verify monotonicity thoroughly with derived recurrence to ensure no oversight: g(n)=|{x: n<x<=2n, pop(x)=k}|. Then A_n = (n,2n]. Then A_{n+1} = (n+1, 2n+2]. Write A_{n+1} = (A_n \ {n+1}) ∪ {2n+1,2n+2}. So g(n+1)=g(n) - I(pop(n+1)=k) + I(pop(2n+1)=k) + I(pop(2n+2)=k). As argued, pop(2n+2)=pop(2*(n+1))=pop(n+1) because left shift adds trailing zero, doesn't change popcount. So I(pop(2n+2)=k) = I(pop(n+1)=k). So g(n+1)-g(n) = I(pop(2n+1)=k). Yes.
Thus monotonic non-decreasing, increment only when popcount(2n+1)=k.
Now we also note that g(1) = f(2)-f(1). But we can compute directly.
Now ensure that count(2n) - count(n) returns numbers in (n,2n] with popcount k, i.e., excludes n if popcount(n)=k? Indeed count(2n) includes numbers <=2n, count(n) includes numbers <= n, so subtract gives numbers with n < x <= 2n. So correct.
Now the binary search might be slightly heavy if we recompute binomial each call but we have precomputed.
Now think about possible alternative: Since g(n) increases slowly, maybe we can compute n directly by constructing? But binary search is simpler.
Now we must handle case where k=64 and m=0: output any n, say 1. Our binary search: g(1) = count(2)-count(1). For k=64, both counts are 0, so g(1)=0, condition g(1)>=0 true, binary search will eventually yield 1. That's fine.
But for k=64, m>0 impossible guarantee.
Now one more nuance: In count function, we use C[rem][need]. But if need == 0, C[rem][0]=1. That counts the number where we set all remaining bits to 0. That is valid. So ans includes numbers where after setting current bit to 0, the rest are all zero. However, that could lead to a number with leading zeros, but since we are considering numbers with a fixed length (the length of x), we must ensure that we don't count numbers with fewer bits as having leading zeros? Standard method: When we are processing bits of x from MSB to LSB, the representation is fixed length (len). Numbers with fewer bits are represented with leading zeros to length len, but that would potentially change popcount? Actually the standard combinatorial method for counting numbers from 0 to x inclusive with exactly k ones in binary representation works by considering the binary representation of x and counting numbers that are less than x with same length? There's a known method: iterate bits, when encountering a 1, we can set this bit to 0 and choose any configuration for the lower bits, resulting in numbers that are strictly less than x and have exactly k ones. This method automatically accounts for numbers with fewer bits because they are represented with leading zeros, but those numbers would have a different most significant bit (0) and thus would have been counted in earlier iterations? Actually the method with fixed length len (the length of x) works as follows: we consider numbers with binary representation of length up to len. For numbers with length less than len, they are considered to have leading zeros to pad to len. However, this may count numbers with leading zeros that still have exactly k ones, but those numbers correspond to the same integer value as the shorter representation. And the counting method correctly counts them because when we set the most significant bit (the actual first 1 of x) to 0, we allow all lower bits to be any combination, and that includes numbers with leading zeros. So the method is correct. But we must ensure we are not double-counting or missing numbers. The typical algorithm for counting numbers from 0 to x inclusive with exactly k ones is indeed:
ans = 0
ones = 0
for i in 0..len-1:
if bit[i] == 1:
// we can set this bit to 0, and choose any bits for the rest, provided total ones = k.
// number of ways: C[len-i-1, k-ones] if k-ones >=0.
// This counts numbers where this position is 0, and the remaining positions (including possibly leading zeros) produce numbers less than x.
// It's correct because all numbers with this bit 0 and others arbitrary are less than x (since at this first differing bit, we set 0 vs 1 in x).
// After that, we set this bit to 1 and continue.
// else bit=0: nothing to do.
At the end, if ones==k: ans++ (x itself)
This counts numbers from 0 to x inclusive. Since we consider from 1, but 0 has popcount 0, not k>=1, it doesn't affect. So it's fine.
Thus implementation correct.
Now we need to ensure binomial coefficients up to n=60 are correct; for n up to 60, C(60,30) fits 64-bit. We'll compute using Pascal up to 64.
Now test with some values manually via code simulation mentally.
Check for n=5, k=2:
count(10): bits of 10 = 1010 len=4.
Process:
i=0 bit=1: ones=0, rem=3, need=2-0=2, if need <=3, ans += C(3,2)=3. (These correspond to numbers with first bit 0? Wait first bit is the most significant; setting it to 0 would yield numbers with length less than 4? Actually setting MSB to 0 and then choosing 2 ones among the remaining 3 bits yields numbers of form 0xxx with exactly 2 ones. Those numbers are from 0 to 7? But they are less than 10? Since MSB=0 ensures number < 10. So counts numbers: among 0-7, which have exactly 2 ones? Let's list 0-7: binary 0000 (0), 0001(1),0010(2),0011(3 pop2),0100(4),0101(5 pop2),0110(6 pop2),0111(7 pop3). So numbers with exactly 2 ones: 3 (0011),5 (0101),6 (0110). That's 3 numbers, matches C(3,2)=3. But note these include 0? popcount 0 not counted. And includes 3,5,6. Good.
Then set ones=1.
i=1 bit=0: nothing.
i=2 bit=1: ones=1, rem=1, need=2-1=1, ans+= C(1,1)=1. This corresponds to fixing bits before: first bit 1 (as in x), second bit 0 (since x has 0 and we are at a 1? Actually at i=2, we are at the third bit (0-index). The bits so far: we followed x exactly for bits 0 and 1: bit0=1, bit1=0. At i=2, bit2=1 in x. So we consider setting this bit to 0, while keeping previous bits as in x (1,0). Then the remaining bits after i=2 are 1 (the LSB). By setting this bit to 0, we get numbers less than x (since at this position we set 0 vs 1). The remaining bits can be set arbitrarily to achieve total popcount k. need=1, so we need 1 one among the 1 remaining bit. So there is 1 number: that is bits: 1,0,0,1? Wait we set current to 0, and need 1 one in last bit, so set last bit to 1. So number = 1 0 0 1 = 9? Actually bits: MSB=1, next=0, next=0 (since we set to 0), LSB=1 -> 1001 = 9. Indeed 9 < 10 and has popcount 2. So added 1.
Then set ones=2.
i=3 bit=0: but ones already 2 > k? Actually ones=2 equals k, but we still process? At i=3, bit=0, no action. However, after loop, ones==k (2), ans++ includes x=1010=10 which has popcount 2? Actually 10 popcount=2? 1010 has two 1s, yes. So ans becomes 3+1+1=5. That matches count(10)=5. Good.
count(5): bits 101 len=3.
i=0 bit=1: ones=0, rem=2, need=2 -> C(2,2)=1 (numbers: 011? But that's 3? Actually numbers with first bit 0 and two ones among remaining 2 bits: those are binary 011 (3) which is <=5? Yes less than 5, pop2). ans=1.
ones=1.
i=1 bit=0: nothing.
i=2 bit=1: ones=1, rem=0, need=1? Actually rem=0 because after i=2 there are no bits. need = k - ones = 1. But need=1 > rem (0) so no addition.
Set ones=2.
After loop, ones==2 => ans++ (include 5). So ans=2. Good.
Thus g(5)=5-2=3.
Now binary search should find 5.
Now consider large values: Suppose k=2, m=10^18? Impossible with n <=1e18. But guarantee ensures not.
Now think about possibility of multiple answers: we output any.
Now about numeric precision: When computing 2*mid, we may need to avoid overflow if we use signed 64-bit but it's safe as earlier.
Now we need to implement main function.
But wait: There's a catch: For n=1e18, 2n = 2e18. But our count function uses loops over bits; the number of bits for 2e18 is about 61. So fine.
Now, we must ensure that our count function is efficient for many calls.
Now check for edge case: k=1, n=1e18. g(n)=1 always. So binary search for m=1 will return n=1? Actually smallest n with g(n)>=1 is 1, because g(1)=1. So output 1. That's fine. But what if m=2 for k=1? Not possible because g(n) never reaches 2. But guarantee says there is answer, so such input won't appear.
Now also consider k=0? Not allowed.
Now we should think about potential off-by-one: In count, we included numbers from 0 to x inclusive. For x >=1, it's fine. For x=1, count(1) for k=1: bits: "1", len=1. Process: i=0 bit=1: ones=0, rem=0, need=1 -> C(0,1) invalid, so no addition. Then set ones=1. After loop, ones==k, ans++ => ans=1. That's correct (number 1). For k=2, count(1)=0.
Now for x=2, count(2) for k=1: bits "10", len=2. i=0 bit=1: ones=0, rem=1, need=1 -> C(1,1)=1 (this counts numbers with first bit 0 and 1 one among remaining 1 bit: that is "01" = 1). ans=1. Then set ones=1. i=1 bit=0: no addition. After loop, ones=1 ==k, ans++ include 2 => ans=2. So count(2)=2. Works.
Now test for x=0 (not used).
Now test for large k: k=60, x=2e18 (approx 2^61- something). Then count may be something, but binomial C(60,30) etc.
Now final step: implement solve() reading m,k, precompute C, binary search.
Potential performance improvement: We can also compute g(n) directly using the recurrence? But not needed.
Now we must handle 64-bit integers carefully: use unsigned long long or long long. I'll use unsigned long long for binomial and for counts, but careful with signedness when subtracting? g(n)=count(2n)-count(n) should be non-negative, fine.
Now the binary search boundaries: low=1, high=1e18. But we need to ensure that for n=1e18, g(n) computed correctly. Since 2n might be 2e18, count(2e18) uses bits length maybe 61; C[60][...] precomputed.
Now we must be mindful that 1e18 is exactly 10^18, which fits in 64-bit.
Now implement.
But before finalizing, let's double-check the recurrence g(n+1)-g(n)=I(popcount(2n+1)=k) and its implication for binary search. Since g increments by at most 1, the smallest n with g(n)>=m indeed has g(n)=m. So binary search works.
But is it possible that g(n) stays constant for a long interval and then jumps by 1, but m is such that it is between the constant values? Since values are integer, and the function takes each integer value in sequence, it will eventually reach m. So if m is attainable, it's exactly at the point where it first reaches that value.
Now, we need to ensure that when m=0 and k>=2, g(1)=0 so answer is 1. That's fine.
Now we should test with some random small values to ensure that our count function and binary search give correct answer. Since we cannot run code here, we'll reason manually for few.
For k=2, we computed g(1)=0, g(2)=1, g(3)=2, g(4)=2, g(5)=3, g(6)=3, g(7)=3, g(8)=3, g(9)=4, g(10)=4, etc. So m=2 yields n=3 (smallest n with g>=2 is 3). Check: n=3 gives [4,6] includes 5 and 6 both pop2? Actually 4 pop1,5 pop2,6 pop2 => count 2, correct. m=3 yields n=5. m=4 yields n=9. So binary search would output those.
Now for k=3, we can compute small g manually perhaps to see pattern: n=1: [2,2] only 2 pop1 ->0. n=2: [3,4] 3 pop2,4 pop1 ->0. n=3: [4,6] 4 pop1,5 pop2,6 pop2 ->0. n=4: [5,8] 5 pop2,6 pop2,7 pop3,8 pop1 -> count 1? 7 has pop3 =>1. So g(4)=1. n=5: [6,10] numbers:6(2),7(3),8(1),9(2),10(2) -> only 7 pop3 =>1. n=6: [7,12] 7(3),8,9,10,11(3),12(2) => 7 and 11 =>2? 11 pop3? 11=1011 pop3 yes. So g(6)=2. n=7: [8,14] 8(1),9(2),10(2),11(3),12(2),13(3),14(3) => 11,13,14? 13=1101 pop3, 14=1110 pop3 -> so 11,13,14 =3? Also maybe? 12 pop2, etc. So g(7)=3? Let's count: 11,13,14 -> three numbers with pop3. So g(7)=3. n=8: [9,16] 9(2),10(2),11(3),12(2),13(3),14(3),15(4),16(1) => 11,13,14 =>3. n=9: [10,18] includes 10(2),11(3),12(2),13(3),14(3),15(4),16(1),17(2),18(2) => 11,13,14 =>3? Actually also maybe? 14 is pop3, 11,13 -> that's 3. So g(9)=3? Wait we might have missed something: 18? 2 ones. So still 3. n=10: [11,20] 11(3),12(2),13(3),14(3),15(4),16(1),17(2),18(2),19(3),20(2) => 11,13,14,19 => 4? 19=10011 pop3? 19=10011 -> popcount=3? 1+0+0+1+1=3, yes. So g(10)=4. So pattern: g(4)=1, g(5)=1, g(6)=2, g(7)=3, g(8)=3, g(9)=3, g(10)=4. So increments occur at certain n. It seems monotonic.
Now test our binary search: for m=3, smallest n with g>=3 is 7? Actually g(6)=2, g(7)=3, so n=7. So answer would be 7. But is there any smaller n with g=3? No. So if input m=3,k=3, output 7.
Now check that our count function yields g(7)=3? Compute count(14) and count(7). Let's quickly compute count(14): numbers up to 14 with pop3. List: 7 (111), 11 (1011), 13 (1101), 14 (1110). Also maybe? 15 >14, so 4 numbers. So count(14)=4. count(7): numbers up to 7 with pop3: 7 only? 7=111 pop3, also maybe? 6? no. So count(7)=1. g=3. Works.
Thus algorithm seems correct.
Now let's consider possible pitfalls with large k and small n: count(2n) might be 0, count(n) might be 0, so g=0.
Now we must ensure that for k=64, m=0, we output n=1.
Now we also need to check the bound for binary search: high=1e18. But what if the smallest n with g(n)>=m is 0? n must be positive (1 ≤ n). So low=1.
Now we need to be careful about integer types: when computing 2mid, use long long; but mid is long long. Use mid2LL.
Now implement.
But before coding, think about potential alternative approach: maybe we can compute n directly via constructing based on binary representation? But binary search is simpler.
Now, we should also note that for large k (like 60), g(n) is very small for n up to 1e18. So binary search will still work.
Now test with an extreme: k=2, m=1e12 (some large number). We need to find n such that g(n)=1e12. g(n) ≈ log2(n) for k=2? Actually from earlier, g(n) ~ log2(n) (since (log n)^{k-1} / (k-1)! and k=2 gives (log n)^{1}/1! = log2(n). Wait more precisely: f(N) ~ (log2 N)^(k)/k!? Actually number of integers with exactly k ones up to N is roughly (log2 N choose k) if N is power of two? More accurately, sum_{i=0}^{L} C(i-1, k-1) for numbers with length i. That sum equals C(L, k). So if N=2^L, f(N)=C(L, k). Then g(n) with n=2^{L-1}? Not exactly. But we can approximate: For n=2^{L}, 2n=2^{L+1}. Then g(2^{L}) = f(2^{L+1}) - f(2^{L}) = C(L+1, k) - C(L, k) = C(L, k-1). Because C(L+1, k) = C(L, k) + C(L, k-1). So g(2^{L}) = C(L, k-1). For k=2, g(2^{L}) = C(L,1) = L. So g(n) ~ log2(n). So to get g=1e12, we need L ~1e12, i.e., n ~ 2^{1e12}, astronomically huge, not within 1e18. So such m won't be given. So fine.
Now we need to precompute binomial up to maybe 64. Since g(2^L)=C(L, k-1). For L up to 60, k up to 64, C(L, k-1) could be zero if k-1 > L. So for k large, g is 0 for many n.
Now, ensure that the function count works correctly for k > len, returns 0.
Now, we need to watch out for potential bug when need < 0: we check need >= 0.
Now, about early break: if ones > k, we can break because no further numbers can have exactly k ones. This is an optimization.
Now, after loops, we include x itself only if ones == k.
Now, test count for x=0: not called.
Now, implement.
Now we must be cautious about the binary search condition: we want smallest n with g(n) >= m. We need to compute g(mid) possibly zero for many. Use function g(n) = count(2*n) - count(n). Since count may return up to maybe C(61,30) ~ 2e17? Actually maximum count for x up to 2e18 with k around 30 could be huge? Let's estimate: f(2e18) ~ sum_{i=1}^{61} C(i-1, k-1). For k=30, that sum is C(61,30) maybe? Actually sum_{i=0}^{61} C(i, k-1) = C(62, k). But numbers from 1 to 2^L roughly C(L, k). So for L=61, C(61,30) ~ 2.5e17? Actually C(61,30) is about 1.5e17? Let's compute roughly: C(60,30) ≈ 1.18e17. So C(61,30) ≈ C(60,30)*61/31 ≈ 1.18e17 * 1.967 ≈ 2.32e17. So f(2e18) could be up to around 2e17, which fits in 64-bit (9e18). So safe.
Thus g(n) could be up to maybe 2e17, still within 64-bit.
Now, implement in C++.
One more nuance: The input m can be up to 1e18, but g(n) may not reach that high; but as guaranteed existence, it's okay.
Now we should consider whether any overflow could happen when computing count(2n) for n=1e18? 2n=2e18, which is less than 2^61 (2.305e18), so bits length 61. So C(60, something) fits.
Now, time complexity fine.
Now, we might also consider using __builtin_popcountll for checking popcount for recurrence, but not needed.
Now let's test some edge cases mentally:
Case 1: m=0, k=2. Should output 1 (since g(1)=0). Binary search: low=1, high=1e18. Compute g(1)=0 >=0 => high=1 => loop ends low=1. Output 1. Good.
Case 2: m=0, k=1. This is not guaranteed existence, but if it appears, our binary search would find n=1 because g(1)=1 >=0, so output 1, but g(1)=1 !=0. However, problem states there is an answer, so such input won't appear. But to be safe, we could check after binary search that g(low)==m; if not, maybe continue searching? But guarantee says existence, so not needed. Still, we could assert.
Case 3: m=1, k=1 -> output 1.
Case 4: m=100, k=3. Binary search will find n.
Now, check the recurrence to confirm that g(n) can be computed correctly for large n.
Now, potential bug: In count function, we treat numbers from 0 inclusive, but we may inadvertently count 0 when need == 0 and we set all bits to 0 after a 0 bit? Actually the method counts numbers with leading zeros correctly. But 0 is counted when we consider numbers where all bits are 0 and ones=0. But since k>=1, need is at least 1, so 0 never gets counted. So safe.
Now, what about numbers with leading zeros that have exactly k ones? They represent numbers less than 2^{len-1}. That's fine.
Now, confirm with example: x=2 (10) for k=1: we counted "01" = 1, which is valid.
Now, test for x=1 for k=1: counted 1 correctly.
Now, test for x=3 (11) for k=2: bits "11", len=2. i=0 bit=1: ones=0, rem=1, need=2 -> C(1,2) invalid, so none. Then ones=1. i=1 bit=1: ones=1, rem=0, need=1 -> C(0,1) invalid. After loop, ones==2 => ans++ (include 3). So count(3)=1, correct.
Now test for x=7 (111) for k=3: bits "111", len=3. i=0 bit=1: ones=0, rem=2, need=3? need=3, rem=2 -> none. ones=1. i=1 bit=1: ones=1, rem=1, need=2 -> none. ones=2. i=2 bit=1: ones=2, rem=0, need=1 -> none. After loop, ones==3 => ans++ => 1. So count(7)=1, correct.
Now, test for x=8 (1000) for k=1: bits len=4. i=0 bit=1: ones=0, rem=3, need=1 -> C(3,1)=3 (numbers: 0 1?? Actually numbers with MSB=0 and exactly one 1 among 3 lower bits: those are 0010? Wait we need to consider: These numbers are 0xxx with exactly one 1. They are: 0001 (1), 0010 (2), 0100 (4). These are <=8 and pop1. So ans=3. Then set ones=1. i=1 bit=0: no addition. i=2 bit=0: no. i=3 bit=0: no. After loop, ones==1 => ans++ include 8. So total 4. Actually numbers <=8 with pop1: 1,2,4,8 -> 4. So correct.
Now all good.
Now we need to think about potential performance issue: The count function uses binomial coefficients; we need to precompute them up to 64. But careful: C[rem][need] where rem can be up to 60, need up to k (<=64). If need > rem, we skip. So C[rem][need] must be defined for rem up to 60, need up to 64? Actually we only access if need <= rem, so need <= rem <= 60. So we only need C up to 60. So safe.
Now, final algorithm:
- Read m and k.
- Precompute binomial C up to 64.
- Define function count(x, k).
- Define function g(n) = count(2*n, k) - count(n, k).
- Binary search for smallest n in [1, 1e18] such that g(n) >= m.
- Print that n.
Now ensure that for large m, g(1e18) is at least m. Since guarantee, it holds.
But we must ensure that our binary search high is inclusive. We'll use while (low < high) and compute mid = (low+high)/2. At the end low == high.
Now we should consider that for k=1, g(n)=1 for all n, so for m=1, binary search returns 1. For m=2, g(1)=1<2, and g(1e18)=1<2, so binary search will never find g(mid) >= 2, and loop will end with low=high=1e18? Actually with while low<high, if condition never true, high never gets lowered? Let's simulate: Initially low=1, high=1e18. We compute mid, check g(mid) >= m? For m=2, all g(mid)=1, so condition false, so low = mid+1. Eventually low will exceed high? Actually the loop while low<high will continue; eventually low may become > high? Let's think: In standard binary search for first true, we require that there is at least one true in the range. If no true, the algorithm may converge to high (the rightmost) or something. Typically, we set the invariant: low is the largest known false, high is the smallest known true, and we require that high is initially a known true. If we cannot guarantee a true, we must check after loop. Since we have guarantee that a true exists (since there is solution, g(1e18) >= m), we can set high = 1e18 and ensure that g(high) >= m. So that's fine.
But what if m is huge and g(1e18) is exactly >= m? Actually it's guaranteed that there exists n <= 1e18 with g(n)=m. Since g is non-decreasing and g(1e18) >= g(n) = m, so g(1e18) >= m. So high is a true. Good.
Now for k=1, if m=2, there is no n with g(n)>=2, and such input wouldn't be given. So no issue.
Thus binary search safe.
Now confirm that 1e18 is a valid upper bound for n. Since n must be <= 1e18 as per output constraint, but we are allowed to output any n up to that. But we are searching up to 1e18. However, what if the smallest n with g(n)=m is greater than 1e18? But they guarantee existence of some n <= 1e18, but not necessarily that the smallest n is <=1e18. Could the smallest n be >1e18 but there is another n (maybe larger) that is still <=1e18? That's impossible because if the smallest n with g(n)=m is >1e18, then all n < that are < m, and any n > that (including those <=1e18?) would have to be greater than smallest, so also >1e18? Actually if the smallest is >1e18, then all n <= 1e18 are < smallest, thus have g(n) < m. So there is no n <= 1e18 with g(n)=m. Contradiction to guarantee. So smallest n with g(n)=m must be <= 1e18. So binary search will find it.
Now final steps: implement.
Now we should double-check the count function for x up to 2e18: bits length might be 61. For len=61, the loop runs 61 iterations. Each iteration we may add a binomial. So fine.
Now, I'll also think about a potential optimization: use combination function with precomputed factorial? Not needed.
Now, one more nuance: The problem statement says "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". Note that the interval is inclusive of 2n. Our derivation uses (n,2n] which matches. Good.
Now we should test with given examples.
Now let's think about extreme cases: k=64, m=0 -> output 1. Our binary search: g(1)=count(2)-count(1). count(2) for k=64: 0, count(1)=0, g(1)=0. So condition g(1)>=0 true, so high will eventually become 1? Actually while low<high: low=1, high=1e18. mid ~ 5e17, g(mid)=0 >=0 true? Actually m=0, condition is g(mid) >= 0, which is always true because g(mid) >=0. So for any mid, condition true, so we set high=mid. Eventually high will go down to 1. So output 1. Works.
Now for k=64, m=1 -> no solution, but not given.
Now consider k=63, m small maybe? For n up to 1e18, g(n) might be 0 or maybe 1? The smallest number with 63 ones is 2^63-1 = 9.22e18 -1 ~ 9.22e18, which is > 2e18 (max 2n). So count(x) for any x<=2e18 is 0. So g(n)=0 for all n. So only m=0 possible. So guarantee ensures m=0.
Now, for k=62: smallest number with 62 ones: it's like 2^62 - 1? Actually 2^62 -1 has 62 ones, its value is about 4.6e18. That's >2e18. Also numbers with 62 ones could be other combinations like 2^63 - 2^0? That is 2^63 -1 minus something? Actually any number with 62 ones has at least value something like the smallest is having the highest bits 1 except maybe? The smallest number with exactly 62 ones is (2^62 - 1) which is about 4.6e18, still > 2e18. But could there be a number with 62 ones but smaller? For a given number of ones, the smallest value is when the ones are placed in the lowest bits: i.e., number = 2^{62} - 1 (all ones in the lowest 62 bits). That is 2^62 -1 ≈ 4.6e18. So any number with 62 ones is at least 2^62 -1 > 2e18. So for x <= 2e18, count(x)=0. So again g=0. So for k>=62? Let's compute threshold: The smallest number with exactly k ones is 2^k - 1? Actually not exactly: The smallest positive integer with exactly k ones is when the ones are the least significant k bits: i.e., (1<<k)-1 = 2^k - 1. But that number has exactly k ones and its value is 2^k - 1. However, there might be numbers with k ones but larger bits set to 0, but the minimal is indeed 2^k - 1. For k=61, 2^61 - 1 ≈ 2.3e18 -1, which is about 2.3e18. That's > 2e18? 2e18 is 2.0e18. 2^61 = 2.305e18, so 2^61 -1 ≈ 2.305e18 -1 = 2.304e18, which is >2e18. But wait, 2^61 is 2.305e18, so 2^61 -1 = 2.305e18 -1 = 2.304e18, which is > 2e18. So for k=61, the smallest number with exactly 61 ones is 2^61 - 1 ≈ 2.3e18, still > 2e18. But could there be a number with 61 ones that is less than 2e18? Possibly if the ones are not all in the lowest bits; e.g., a number with bits: one 1 in the high position and 60 ones in lower bits? That would be larger than 2^60? Actually the smallest number with exactly 61 ones is achieved by putting the 61 ones in the lowest 61 bits, giving value 2^61-1, because any arrangement with a 0 in a lower bit and a 1 in a higher bit would increase the value? Let's think: For a given count of ones, the minimal value is indeed when they occupy the least significant positions. So the minimal is 2^k - 1. So for k=61, minimal is 2^61-1 > 2e18. So for all n <= 1e18, 2n <= 2e18, so no number with 61 ones appears in [n+1,2n]. So g(n)=0 for all n. So for k>=61, g(n)=0 always. Actually check k=60: smallest = 2^60 - 1 = 1.1529215e18 - 1 ≈ 1.1529e18. That's less than 2e18. So numbers with 60 ones can appear in the interval. So for k>=61, only m=0 possible.
Thus we could early handle: if k>=61, then if m==0 output 1 else impossible but guarantee says exists so m must be 0. We'll just rely on binary search: it will return n=1 for m=0 because g(1)=0, condition true. For m>0, binary search would eventually set high to 1e18 but g(1e18)=0 < m, so no true; but since guarantee, such case won't occur. However, to be safe, we might include a check after binary search: if g(low) != m, then maybe search for larger n? But it won't happen due to guarantee.
But we need to ensure that for k>=61, our count function for x up to 2e18 returns 0. Since smallest number with k ones is 2^k -1 > 2e18, count will correctly return 0. So g(n)=0 for all n. So binary search for m=0: since condition g(mid)>=0 always true, high will move down to 1, output 1. Good.
Now, a nuance: In binary search, when condition always true, we set high=mid each time. This will converge to low=1? Let's simulate: low=1, high=1e18. mid = (1+1e18)/2 ≈ 5e17. Condition true, so high=mid. Now low=1, high=5e17. Next mid ~ 2.5e17, condition true, high=mid. Continue until high eventually becomes 1? Actually if condition always true, high will keep decreasing, low remains 1. When high becomes 1, loop condition low<high? low=1, high=1, exit. Then low=1. So output 1. That's correct.
Now, if condition never true (i.e., g(mid) < m for all mid), then low will increase. But guarantee ensures not.
Now, we also need to ensure that when m is large but still within range, binary search works.
Now, we should also consider the case when k=0? Not allowed.
Now, we should double-check that our count function returns correct values for numbers up to 2e18. Implementation details: For converting x to bits, we might use a for loop to extract bits without storing vector, but easier with vector.
Now, potential overflow: When computing C[rem][need], we precomputed with long long. For rem up to 60, need up to 60, it's safe.
Now, we could also precompute all binomials using combination function with 64-bit.
Now, we should also consider that in count function, we might need to handle the case where k == 0? not needed.
Now, we can also implement count using a simpler iterative method: For numbers with exactly k ones up to x, we can iterate over positions where the most significant one could be? But our method is fine.
Now, one more test: k=2, n=2^L. Let's test n=8 (2^3). Then g(8)=? Should be C(3,1)=3? But earlier we computed g(8)=3? Actually we had g(8)=3 for k=2, yes. And L=3, C(3,1)=3. Good.
Now, we should also consider that for large k close to 60, binomial values might be large but still within 64-bit. For L=60, C(60,30) fits. So fine.
Now, we'll implement.
Now, we need to read m and k as long long and int.
Now, write code:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
ll C[65][65];
void precompute() {
for (int i=0; i<=64; i++) {
C[i][0] = C[i][i] = 1;
for (int j=1; j<i; j++)
C[i][j] = C[i-1][j-1] + C[i-1][j];
}
}
ll count(ll x, int k) {
if (x <= 0) return 0;
// convert to binary
vector bits;
ll t = x;
while (t) {
bits.push_back(t & 1);
t >>= 1;
}
reverse(bits.begin(), bits.end());
int len = bits.size();
if (k > len) return 0;
ll ans = 0;
int ones = 0;
for (int i = 0; i < len; i++) {
if (bits[i] == 1) {
int rem = len - i - 1;
int need = k - ones;
if (need >= 0 && need <= rem) {
ans += C[rem][need];
}
ones++;
if (ones > k) break;
}
}
if (ones == k) ans++;
return ans;
}
ll g(ll n, int k) {
return count(2*n, k) - count(n, k);
}
int main() {
precompute();
ll m;
int k;
cin >> m >> k;
ll low = 1, high = 1000000000000000000LL; // 1e18
// ensure that high is true: g(high) >= m? But we trust existence.
while (low < high) {
ll mid = (low + high) / 2;
if (g(mid, k) >= m) {
high = mid;
} else {
low = mid + 1;
}
}
cout << low << endl;
return 0;
}
Now we need to confirm that g(high) >= m initially. But we haven't checked. Since guarantee existence, it's okay. But to be safe, we could check g(high) < m, then we could increase high? But high is max bound 1e18, and guarantee says there is some n <= 1e18 with g(n)=m, so g(1e18) >= m. So it's fine.
Now consider possible overflow in 2n within g(n) when n near 1e18? 21e18 = 2e18 < 9e18, safe. So we can compute 2*n as ll.
Now test with provided examples:
Input: 1 1 => output 1.
Input: 3 2 => output 5.
Now we might want to test with some other values manually using mental or maybe quickly reason with code? Not possible here but trust.
Now, potential bug: In count, we used C[rem][need] where rem up to 60, need up to k (<=64). But we only access if need <= rem, so need <= rem <= 60, so need <= 60. So C[60][...] defined. But we precomputed C up to 64, so safe.
Now, what about when k is 64 and we call count(2*n, 64) for n small? len max 61, k=64 > len so early return 0. Good.
Now, the problem states k between 1 and 64 inclusive. So okay.
Now, we need to consider that m can be 0. In binary search, condition g(mid) >= 0 always true, so we will get low=1. That's fine.
Now, check if there's any n such that g(n) = m but n=1 maybe not the smallest? For m=0, output 1 is valid. Good.
Now, we should also check for k=1, m=1: binary search returns 1. Good.
Now, final step: produce final answer.
But before finalizing, let's think about potential alternative: Could there be any tricky case where g(n) is not monotonic due to some subtlety? We derived recurrence based on set manipulation and the fact popcount(2n+2)=popcount(n+1). This seems solid. However, we must ensure that the transformation from A_n to A_{n+1} is correct:
A_n = {x | n < x <= 2n}
A_{n+1} = {x | n+1 < x <= 2n+2}
Now, elements of A_n: they are integers from n+1 to 2n inclusive.
A_{n+1}: from n+2 to 2n+2 inclusive.
So A_{n+1} = (A_n \ {n+1}) ∪ {2n+1, 2n+2}.
Yes, because A_n includes n+1; removing it gives numbers from n+2 to 2n; then add 2n+1 and 2n+2. So correct.
Now, g(n+1) = g(n) - I(pop(n+1)=k) + I(pop(2n+1)=k) + I(pop(2n+2)=k).
Now pop(2n+2) = pop(2*(n+1)) = pop(n+1). Because multiplying by 2 adds a zero bit at the end, popcount unchanged. So indeed I(pop(2n+2)=k) = I(pop(n+1)=k). So they cancel, leaving g(n+1) = g(n) + I(pop(2n+1)=k). Good.
Thus monotonic.
Now, check edge: n+1 might be a power of two? No matter.
Now, one more thing: The interval is from n+1 to 2n inclusive. If n=0? not allowed.
Now, everything seems correct.
Now, we should also consider if we need to use 64-bit for m, which is up to 1e18, so use long long.
Now, final answer.
Now, I'll produce final solution explanation.
But I realize: In the recurrence we used, we assumed that g(n) = f(2n)-f(n). That counts numbers in (n,2n] with popcount k. However, is f(2n) counting numbers from 1 to 2n inclusive? Yes. So f(2n)-f(n) includes numbers from n+1 to 2n inclusive. That's correct. But we must ensure that f(2n) and f(n) are computed correctly for large numbers. We are.
Now, check with n=1 for k=2: f(2)= count up to 2 with pop2: numbers: 3? no; So f(2)=0; f(1)=0; g=0. Good.
Now, the recurrence shows g(2)=g(1)+I(pop(3)=2)=0+1=1. Let's compute f(4)-f(2). f(4): numbers <=4 with pop2: 3 only? Actually 3 (11) pop2, also maybe? 2? no. So f(4)=1; f(2)=0; g(2)=1. Good.
Now, g(3)=g(2)+I(pop(5)=2)=1+1=2. f(6)-f(3): f(6): numbers <=6 with pop2: 3,5,6? 6=110 pop2, yes; so f(6)=3; f(3): numbers <=3 with pop2: 3 only? So f(3)=1; difference 2. Good.
So recurrence holds.
Now, we can also compute g(n) quickly by using prefix sums of I(pop(2i+1)=k). That is g(n) = g(1) + sum_{i=1}^{n-1} I(pop(2i+1)=k). But not needed.
Now, the binary search approach is fine.
Now, one more nuance: In count function, we used vector and reversed. That might be a bit slower but fine. Could also use a loop over bits from most significant to least without storing, by first finding highest bit.
But it's okay.
Now, let's think about potential integer overflow in computing 2n for n up to 1e18: 21e18 = 2e18, which is less than 2^63-1 ≈ 9.22e18. So safe.
Now, finalize.
Now, I'll produce final answer with explanation.
But before final output, we should test with a few more cases manually to be thorough:
Case: m=0, k=2. Output 1. Check: numbers from 2 to 2: only 2 (pop1) -> count=0. So correct.
Case: m=1, k=2. Should output 2 (since g(2)=1). Let's verify: n=2 => [3,4] -> 3 pop2, so 1. Good.
Case: m=2, k=2. Output 3? g(3)=2, correct.
Case: m=4, k=2. Output 9? g(9)=4? earlier we had g(9)=4. Yes.
Now, for k=3, m=2 -> output 6? g(6)=2? earlier we had g(6)=2? Actually we computed g(6)=2. So output 6.
Now, for k=3, m=3 -> output 7? g(7)=3. Yes.
Now, for k=3, m=4 -> output 10? g(10)=4. Yes.
Now, everything consistent.
Now, confirm that g(n) is monotonic: It seems so.
Thus solution is to binary search.
Now, we should also consider that for large m and k, n might be near 1e18. Binary search will find it.
Now, possible optimization: we could compute g(n) directly using combinatorial formulas for powers of two, but not necessary.
Now, we must be cautious about the case where m is huge and k is small, but guarantee ensures n exists within 1e18. For k=2, max g(1e18) is about log2(1e18) ≈ 60. So m cannot exceed ~60 for k=2 if solution exists. So input won't have m=1e18 for k=2. So the input values are consistent with existence.
Now, final code.
But wait: The problem says "Sasha knows that you are afraid of large numbers, so she guaranteed that there is an answer that doesn't exceed 10^18." So they guarantee that for given m,k there exists some n <= 1e18. But they do not guarantee that g(n) is monotonic? But we proved it is. So binary search works.
Now, check if any special cases break monotonicity: For n=0, not considered. So all good.
Now, I'll produce final answer.
But before concluding, let's test with a random case using reasoning: For k=4, we can compute g(n) for small n to ensure monotonicity.
But I think it's fine.
Now, let's also think about the count function for x up to 2e18. Since we're using long long for binomial, it's fine.
Now, consider using 128-bit if needed? Not.
Now, let's also consider potential performance of count: loops over bits and uses C. That's fine.
Now, finalize.
One more nuance: The problem might have multiple answers, we output any. Our binary search outputs the smallest n. That's acceptable.
Now, is there any scenario where the smallest n with g(n)>=m does not have g(n)=m? As argued, because increments are at most 1, if the first n with g(n)>=m had g(n) > m, that would mean that at previous n, g was < m and then increased by at least 2 at this n. But increments are at most 1, so impossible. So g(n)=m.
Now, finalize.
Now, I'll write the final answer with explanation.
But before final, check for potential off-by-one: In binary search we use inclusive range [1, 1e18]. The while (low < high) with mid = (low+high)/2 floor, and when condition true we set high = mid, else low = mid+1. This yields the first true. So if g(1) >= m, then result is 1.
Now, test with m=0: g(1)=0 >=0 true, so high becomes 1, result 1. Good.
Now, test with m=1, k=2: g(1)=0 <1, so low becomes mid+1 etc. Should converge to 2.
Now, need to ensure that we use long long for mid, etc.
Now, final code is ready.
Now, as an additional check, we can verify that the recurrence holds for all n, but not needed.
Now, I'll produce final answer.
But I realize that the function count as described counts numbers from 0 to x inclusive. For x=0 it returns 0. For x>0, it's fine. So g(n) = count(2n) - count(n). For n=1, count(2) includes numbers 0? Actually 0 not counted because k>=1. So fine.
Now, one more test: For k=1, count(2) we computed as 2 (numbers 1 and 2). That's correct. So g(1)=count(2)-count(1)=2-1=1.
Now, for n=2, count(4)= numbers <=4 with pop1: 1,2,4? Actually 1,2,4 -> 3. count(2)=2, so g(2)=1. Good.
Now, for n=3, count(6)= numbers <=6 with pop1: 1,2,4 -> 3? Wait also 8? >6, so 3. count(3)= numbers <=3 with pop1: 1,2 ->2. So g(3)=1. Actually earlier we thought g(3)=1 for k=1? Yes we said g(3)=1, consistent. So all good.
Now, final code.
Now, as final answer, I'll produce the solution explanation and code.
Now, note that the problem may require reading input from stdin and output to stdout.
Now, I'll write final answer. ```cpp
#include
#include
#include
using namespace std;
using ll = long long;
// Binomial coefficients C[n][r] for n up to 64
ll C[65][65];
void precompute_comb() {
for (int i = 0; i <= 64; ++i) {
C[i][0] = C[i][i] = 1;
for (int j = 1; j < i; ++j)
C[i][j] = C[i-1][j-1] + C[i-1][j];
}
}
// count numbers in [1, x] with exactly k ones
ll count(ll x, int k) {
if (x == 0 || k == 0) return 0; // k >= 1, so 0 not counted
// binary representation (most significant first)
vector bits;
ll t = x;
while (t) {
bits.push_back(t & 1);
t >>= 1;
}
reverse(bits.begin(), bits.end());
int len = bits.size();
if (k > len) return 0;
ll ans = 0;
int ones = 0;
for (int i = 0; i < len; ++i) {
if (bits[i] == 1) {
int rem = len - i - 1;
int need = k - ones;
if (0 <= need && need <= rem)
ans += C[rem][need];
++ones;
if (ones > k) break;
}
}
if (ones == k) ++ans; // include x itself
return ans;
}
// g(n) = number of integers in (n, 2n] with exactly k ones
ll g(ll n, int k) {
return count(2 * n, k) - count(n, k);
}
int main() {
precompute_comb();
ll m;
int k;
cin >> m >> k;
ll low = 1, high = 1000000000000000000LL; // 10^18
// binary search for the smallest n with g(n) >= m
while (low < high) {
ll mid = (low + high) / 2;
if (g(mid, k) >= m)
high = mid;
else
low = mid + 1;
}
// due to monotonicity and existence, g(low) == m
cout << low << endl;
return 0;
}