Determine whether it is possible to choose X numbers that are powers of 2 and add up to N.
Determine whether it is possible to choose X numbers that are powers of 2 and add up to N.
| # | User | Rating |
|---|---|---|
| 1 | Kevin114514 | 3722 |
| 2 | Benq | 3676 |
| 3 | jiangly | 3640 |
| 4 | turmax | 3450 |
| 5 | tourist | 3408 |
| 6 | strapple | 3360 |
| 7 | Um_nik | 3354 |
| 8 | squareOf105 | 3348 |
| 9 | maroonrk | 3196 |
| 10 | idontwannadothis…e | 3191 |
| # | User | Contrib. |
|---|---|---|
| 1 | maroonrk | 153 |
| 2 | Qingyu | 152 |
| 3 | nik_exists | 151 |
| 4 | AmShZ | 147 |
| 4 | maspy | 147 |
| 6 | Um_nik | 142 |
| 7 | Errichto | 139 |
| 8 | adamant | 136 |
| 9 | BledDest | 135 |
| 10 | AksLolCoding | 132 |
| Name |
|---|



You can check how many bits are set in the number N. if its equal to x then it is possible
I don't think it will work. let's take N = 9 and X = 3. The answer according to you is false, but (4, 4, 1) will work.
Answer is yes iff sbits(N) is <=X and X<=N.
sbits(x) refers to the number of 1s in the binary representation of x
Case 0: X>N -> NO
Quite obviously, even if we were to use X 1's, we would exceed N, so this is impossible.
Case 1: sbits(N) == x -> YES
It is obvious this is yes, because you just choose each bit that is on, and you get the answer.
Case 2: sbits(N) <=X -> YES iff Case 0 is false
For each bit that we have on in N, notice that we can split it into two other powers of two until we reach 1. ie. 8 = 4+4 , 4 = 2+2 ... This process can be repeated until we reach X bits ( given that X<= N )