We need to solve the problem: given sum and limit, find a set S of distinct integers from 1 to limit inclusive such that the sum of lowbit(x) over x in S equals sum. We need to output the size and elements (any order) or -1 if impossible.
Constraints: sum, limit <= 1e5. Output size up to 1e5.
This is a classic problem: from Codeforces Round #... (maybe 221A?). The typical solution: lowbit(x) is the largest power of two dividing x. That is, lowbit(x) = x & -x. For each number from 1 to limit, its lowbit value is a power of two. We need to choose a subset with distinct numbers (all numbers distinct and <= limit) such that sum of lowbits equals sum.
Observation: For each power of two p, numbers that have lowbit = p are exactly those numbers that are multiples of p but not multiples of 2p. More precisely, numbers of the form p * (odd) where the odd part is odd and less than something? Actually if we define lowbit(x) = 2^k where k is the index of the least significant 1 (0-indexed from LSB). So numbers that have lowbit = 2^k are exactly those numbers that are congruent to 2^k mod 2^{k+1}, i.e., numbers of the form (2k+1)*2^k? Wait: numbers whose binary representation ends with exactly k zeros and then a 1. So x = (2m+1) * 2^k. So the set of numbers with lowbit = 2^k is infinite but limited to <= limit.
Thus we have for each power of two p = 1, 2, 4, 8, ... up to limit. For each p, there exist numbers <= limit with lowbit p: they are p, 3p, 5p, 7p, ... as long as they are <= limit. So the count of available numbers with lowbit p is: floor(limit / p) - floor(limit / (2p))? Actually numbers that are multiples of p but not multiples of 2p: count = floor(limit / p) - floor(limit / (2p)). Because multiples of 2p are those that have at least one more factor 2. So count = number of odd multiples of p: i.e., multiples of p whose factor for p's power is odd? Another way: numbers of form p*(odd) <= limit. So count = floor(limit/p) - floor(limit/(2p)). Because among multiples of p, those with even quotient (i.e., divisible by 2p) are excluded. So count = number of multiples of p where the multiplier is odd.
Thus we have for each p = 2^k, a supply of numbers with that lowbit value, each providing p to the sum. The sum we need is sum. We need to pick a multiset of numbers (but distinct) such that the sum of lowbits = sum. The lowbits are powers of two. So essentially we need to represent sum as a sum of powers of two (the lowbits of selected numbers) but with limited availability of each power.
But note: the lowbits themselves are powers of two. The sum is up to 1e5, limit up to 1e5. So we can treat it like we have coins of denominations powers of two, but each coin must be "assigned" to a distinct number that yields that lowbit. And there is a limited number of coins for each denomination: the count of numbers with that lowbit.
The problem reduces to: For each power of two p, we have at most cnt[p] available numbers each providing value p. We need to achieve sum S using some selection of numbers such that total sum of p's = sum. Also we need to output the actual numbers selected (distinct and <= limit). We can choose any numbers among those available.
We need to output any set S. If impossible, output -1.
We can solve greedily: Starting from the largest p (<= limit maybe up to largest power of two <= limit) and down to 1, we can use as many numbers as needed of that lowbit to cover the sum, but not exceeding the available count. Because lowbit values are powers of two, a greedy from highest to lowest works for the coin change problem if unlimited coins, but here we have limited numbers. Since we can only use up to cnt[p] coins of denomination p. The greedy from high to low would still work? Possibly, but not always? Actually for powers of two, greedy works even with limited counts? If we consider that each coin is power of two, any number can be represented in binary, which uses at most one coin of each power of two. So we would need at most one coin of each power of two? But wait, in coin change with powers of two, the binary representation uses each power at most once. However, we may need multiple coins of the same power if there are multiple numbers with the same lowbit. But note: if we use two numbers both with lowbit 2, we get 2+2=4. But we could also use one number with lowbit 4, which would give 4 using only one number. Using two of the same lowbit may be necessary if we don't have enough numbers with larger lowbits? Actually, because the sum we need is sum; its binary representation tells us exactly how many of each power of two we need if we restrict to using each power at most once? But we can use more than one, but then we could combine them into higher powers if we had larger lowbits? The underlying constraint is: we have limited availability. It might be optimal to use as many high denominations as possible to minimize the number of numbers, but we don't care about count except that it must be <= 1e5 (and we need to output any). So greedy from highest to lowest using as many as possible? Actually we need to achieve exact sum. If we take the binary representation of sum, we would need exactly one of each power if we treat it as using each power at most once. However, we may have to use multiple of a power if we cannot cover the bits using higher powers due to limited availability. For example, sum = 6, limit=5. Let's check: lowbits: numbers <=5: 1: lowbit1=1; 2:2; 3:1; 4:4; 5:1. Available: p=1 count: numbers {1,3,5} =>3; p=2 count: {2} =>1; p=4 count: {4}=>1; p=8>5 none. Sum=6. Binary of 6 = 4+2. So we could use numbers 4 and 2: gives 4+2=6, that works. So we used each power at most once. But what about a case where sum=3, limit=2? Available: p=1: {1}? Actually numbers:1 lowbit1, 2 lowbit2. p=1 count 1, p=2 count 1. Sum=3 => binary: 2+1, we can use 2 and 1. Works.
But consider sum=5, limit=4. Numbers:1,2,3,4. lowbits: 1->1,2->2,3->1,4->4. Available: p=1: 2 numbers (1,3); p=2:1 number (2); p=4:1 (4). Sum=5 binary:4+1. So we can pick 4 and 1 or 4 and 3. Works. But what about sum=7, limit=4? binary:4+2+1. We have 4,2,1 or 4,2,3 -> works. sum=8, limit=6? Available numbers up to 6: 1(l1),2(l2),3(l1),4(l4),5(l1),6(l2? lowbit of 6=2 because 6=110, lowbit=2). So counts: p=1: {1,3,5}->3; p=2:{2,6}->2; p=4:{4}->1; p=8:0. sum=8 binary: 8? But 8 not available because no number with lowbit 8 <=6. Could we combine? 4+2+2=8 using 4,2,6 -> yes! So we used two numbers with lowbit 2. So we sometimes need multiple of same denomination.
Thus the problem becomes: given availability cnt[p] for each p=1,2,4,... up to highest power <= limit, can we represent sum S as sum_i (a_i * p_i) where 0 <= a_i <= cnt[p_i]? The p_i are powers of two. This is like bounded knapsack with powers of two, but we can solve greedily from large to small? Because of the property of powers of two, a greedy that for each power from highest down, we use as many as possible without exceeding the sum? Let's test.
Standard algorithm: For each power p from largest down to 1 (powers of two), let use = min(cnt[p], sum // p). Then subtract use * p from sum, add use to the count of numbers, and record which numbers we will pick later. Then go to next p. After all, if sum == 0, we succeeded. Otherwise impossible.
But is this greedy always optimal? Since denominations are powers of two, any representation can be transformed into one where for each power, the coefficient is either 0 or 1 if we combine two of them into a higher power. But limited by availability of higher powers. The greedy from high to low will use as many high powers as possible, which minimizes the total number of numbers used, but also ensures feasibility? For bounded coin change with powers of two, this greedy works because the coin system is canonical. Actually, canonical coin systems: For the unlimited version, greedy works if and only if the coin system is "canonical". For the set of denominations being powers of two, unlimited greedy works. For bounded, I think the greedy using as many high as possible still yields a solution if one exists because of the divisibility property. Let's test a potential counterexample: Suppose we have cnt[4]=0, cnt[2]=2, cnt[1]=0, sum=4. Greedy: start with p=4, use= min(0,1)=0. Then p=2, use = min(2,4//2=2) =2, subtract 4 => sum=0. Works. So greedy uses two 2's to make 4, which is valid. Another: cnt[4]=0, cnt[2]=1, cnt[1]=2, sum=4. Greedy: p=4 use 0; p=2 use min(1,2)=1 => subtract 2, sum=2; p=1 use min(2,2)=2 => sum=0. Works. Another: cnt[4]=1, cnt[2]=0, cnt[1]=2, sum=3. Greedy: p=4 use min(1,0)=0; p=2 use 0; p=1 use min(2,3)=2 => sum=1 >0 -> fails. But is there a solution? Using the 4 is too big, so we need three 1's but we have only two. So impossible. Greedy correctly fails. Another: cnt[4]=1, cnt[2]=2, cnt[1]=0, sum=6. Greedy: p=4 use min(1,1)=1, sum=2; p=2 use min(2,1)=1, sum=0; works (4+2). Another solution: 2+2+2? Not possible because we have only two 2's and one 4? Actually 2+2+2=6 but we have only two 2's, so 4+2 works. Greedy works.
What about cnt[4]=1, cnt[2]=0, cnt[1]=4, sum=5. Greedy: p=4 use min(1,1)=1, sum=1; p=2 use 0; p=1 use min(4,1)=1, sum=0 => uses numbers: 4 and 1, sum=5. Works. Another: cnt[4]=1, cnt[2]=1, cnt[1]=0, sum=5. Greedy: p=4 use 1, sum=1; p=2 use min(1,0)=0; p=1 use 0 => fails. Is there a solution? We have 4 and 2 only, sum=6 if we take both, too high; 4 alone gives 4; 2 alone gives 2; none gives 5. So impossible. Greedy correctly fails.
But consider cnt[8]=0, cnt[4]=2, cnt[2]=0, cnt[1]=1, sum=6. Greedy: p=8:0; p=4: use min(2,6//4=1) =1, sum=2; p=2: use min(0,1)=0; p=1: use min(1,2)=1, sum=1 -> fail. But could we use two 4's? That would be 8 >6; or one 4 and some 1's? we have only one 1, so 4+1=5, not 6. So impossible. Greedy fails correctly.
Consider cnt[8]=1, cnt[4]=0, cnt[2]=3, cnt[1]=0, sum=10. Greedy: p=8 use 1, sum=2; p=4 use 0; p=2 use min(3,1)=1, sum=0; works: 8+2=10. But also 2+2+2+2+2? we have only 3. So works.
Now a potential tricky: sum=12, cnt[8]=1, cnt[4]=1, cnt[2]=1, cnt[1]=4. Greedy: p=8 use1, sum=4; p=4 use1, sum=0; works: 8+4=12. Another representation using 4+4+4? we have only one 4. So fine.
But could there be a scenario where greedy fails even though a solution exists? For coin systems with bounded supply, greedy might fail even for canonical denominations if the greedy choice of as many high as possible prevents using lower denominations to fill the remainder because of limited supply of lower denominations. However, with powers of two, I think it's safe because each high denomination is a multiple of all lower ones, and we have the ability to replace a high denomination with two of the next lower denomination, etc. But because of supply limits, we might need to reserve some lower denominations for the remainder, so greedy may overcommit high denominations leaving insufficient lower ones. Let's attempt to construct a counterexample.
We want a case where greedy fails: Use as many of largest p as possible: use = min(cnt[p], sum // p). Suppose we have p=4, and we use all available p=4 (or up to sum//p), but then the remainder cannot be made because we don't have enough lower denominations, but there exists an alternative solution that uses fewer p=4 and more p=2, p=1 to make sum.
Because p=4 is equivalent to 2*2, but using two 2's instead of one 4 requires we have enough 2's. So if cnt[2] is limited, maybe greedy using many 4's might waste the possibility of using 2's. But since 4 is larger, using a 4 reduces need for 2's. Actually using a 4 consumes one number but provides 4 value. Using two 2's provides 4 as well but uses two numbers. In terms of value, 4 uses 1 number; 2+2 uses 2 numbers. If we are short on lower denominations (i.e., not enough 2's), using a 4 is better because it doesn't need 2's. So greedy using as many high as possible should be beneficial. However, consider scenario: sum = 6, we have cnt[4]=1, cnt[2]=1, cnt[1]=2. Greedy: p=4 use min(1,1)=1, sum=2; p=2 use min(1,1)=1, sum=0; works: 4+2=6. That's fine.
What if sum=5, cnt[4]=1, cnt[2]=0, cnt[1]=2. Greedy: p=4 use 1, sum=1; p=2 use 0; p=1 use min(2,1)=1, sum=0; works: 4+1=5. Alternate: if we used 1+1+1+1+1? not enough 1's.
What if sum=3, cnt[4]=1, cnt[2]=0, cnt[1]=2. Greedy: p=4 cannot use because 4>3; use 0; p=2 0; p=1 use min(2,3)=2, sum=1 -> fails. But is there a solution? We have only 1's: need three 1's but we have only two, so impossible. So fine.
Let's try to construct a more subtle case: Suppose sum=12, with denominations 8,4,2,1. cnt[8]=1, cnt[4]=2, cnt[2]=0, cnt[1]=4. Greedy: p=8 use1, sum=4; p=4 use min(2,1)=1, sum=0; works: 8+4=12. That's fine.
Now suppose cnt[8]=1, cnt[4]=0, cnt[2]=5, cnt[1]=0, sum=12. Greedy: p=8 use1, sum=4; p=4 use0; p=2 use min(5,2)=2, sum=0; works: 8+2+2=12. That uses two 2's. But we could also use six 2's? Not enough. So fine.
Now try sum=10, cnt[8]=1, cnt[4]=1, cnt[2]=0, cnt[1]=6. Greedy: p=8 use1, sum=2; p=4 cannot use because 4>2; p=2 0; p=1 use min(6,2)=2, sum=0; works: 8+1+1=10. Alternative: 4+1+1+1+1+1+1? That's 4+6=10 also uses 1's. Both use same high coin 8 but alternative uses 4 and six 1's, both okay. Greedy works.
What about sum=9, cnt[8]=1, cnt[4]=0, cnt[2]=0, cnt[1]=4. Greedy: p=8 use1, sum=1; p=1 use1, sum=0; works: 8+1=9. Good.
Now consider where greedy overcommits high and leaves insufficient low but alternative uses fewer high and more low. Because low denominations have limited supply, using fewer high might require more low, which could be problematic if low supply is limited. Actually if low supply is limited, using more high reduces need for low, so better. So if greedy fails due to insufficient low after using many high, maybe an alternative that uses fewer high would require even more low, which would be even more insufficient. But could there be a case where the greedy uses many high, but low supply is insufficient for the remainder, but alternative uses slightly fewer high, thereby leaving a larger remainder that can be satisfied by the low supply? Because using fewer high leaves larger remainder? Wait: using fewer high means you get less contribution from high, so remainder is larger. For example, suppose sum=10, and we have cnt[8]=2, cnt[2]=1, cnt[1]=0. Greedy: p=8 use min(2,1)=1? Actually sum//8=1 so use 1, remainder 2, then use p=2 use 1 => sum 0. Works. If we had cnt[8]=2, cnt[2]=0, cnt[1]=2? Greedy: use 1 of 8, remainder 2, but no 2's, then p=1: use min(2,2)=2, sum=0; works. So no issue.
Let's try to think systematically: Since all denominations are powers of two, the representation problem is essentially: we have a target sum S, and for each k we have a maximum number of 2^k coins, a_k. We need to find non-negative integers x_k <= a_k such that sum x_k * 2^k = S. This is like a bounded knapsack with powers of two, which can be solved by processing from low to high using binary decomposition and carrying over, or from high to low using greedy. Because of the base-2 nature, we can treat it as: For each bit from low to high, we can combine two coins of value 2^k into one coin of value 2^{k+1} if needed, but only up to available coins. This is similar to the concept of "binary addition" with limited resources. There is known solution: for each number from 1 to limit, compute lowbit, group by lowbit. Then greedily try to form sum using as many large as possible. Usually the solution accepted in Codeforces for this problem uses exactly that greedy and works. I recall a problem "Little Pony and Sort by Shift"? No, it's "Presents" maybe? Actually I recall a problem "Little Elephant and Function"? Hmm.
I think the intended solution is to greedily take numbers with high lowbit, using as many as needed. Because limit and sum <= 1e5, we can just iterate powers from highest to lowest (up to 2^17 perhaps). Then for each power p, we can select numbers that have lowbit = p, up to needed count. Since we need to output the actual numbers, we can store a list of numbers for each lowbit. Then for each p, we will take up to 'use' numbers from that list. So we need to know the actual numbers.
Thus algorithm:
- Precompute lowbit for each i from 1 to limit