problems / 276

Alice's Power-of-Two Candy Boxes

easy bit-manipulation math 1000 ms · 256 MB

Alice's Power-of-Two Candy Boxes

Alice runs a tiny candy shop with a peculiar stock: she has exactly one sealed box of each size 1, 2, 4, 8, 16, ... candies, one box for every power of two. Boxes can't be opened, so whenever Bob asks for exactly n candies, Alice must hand him a set of whole boxes whose sizes add up to exactly n, using each box at most once. Alice has noticed that this is always possible, and that there is always exactly one way to do it.

Bob places q orders. For each order, tell Alice how many boxes she gives him and which ones.

Input

The first line contains an integer q (1 ≤ q ≤ 10^4), the number of orders.

Each of the next q lines contains one integer n (1 ≤ n ≤ 10^18), the number of candies Bob asks for.

Output

For each order print one line: first the number of boxes k, then the k box sizes in ascending order, all separated by single spaces.

Notes

In the first example, 1 is a single box, 6 = 2 + 4, 7 = 1 + 2 + 4 uses three boxes, 8 is again a single box, and 13 = 1 + 4 + 8.

In the second example, 10^18 needs boxes up to 2^59 = 576460752303423488, far beyond 32 bits, so use a 64-bit integer type (long long in C and C++, int64 in Go). The order 576460752303423487 = 2^59 − 1 needs every box from 1 up to 2^58: 59 boxes in all.

Example 1

Input

5
1
6
7
8
13

Output

1 1
2 2 4
3 1 2 4
1 8
3 1 4 8

Example 2

Input

3
1000000000000000000
576460752303423487
576460752303423488

Output

24 262144 2097152 4194304 16777216 33554432 67108864 536870912 2147483648 4294967296 8589934592 68719476736 137438953472 549755813888 2199023255552 4398046511104 17592186044416 35184372088832 140737488355328 9007199254740992 18014398509481984 36028797018963968 72057594037927936 288230376151711744 576460752303423488
59 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912 1073741824 2147483648 4294967296 8589934592 17179869184 34359738368 68719476736 137438953472 274877906944 549755813888 1099511627776 2199023255552 4398046511104 8796093022208 17592186044416 35184372088832 70368744177664 140737488355328 281474976710656 562949953421312 1125899906842624 2251799813685248 4503599627370496 9007199254740992 18014398509481984 36028797018963968 72057594037927936 144115188075855872 288230376151711744
1 576460752303423488

Hints · reveal one at a time if you're stuck

Hint 1

Write a few small orders in binary: 6 is 110, 13 is 1101. Which boxes does each one need?

Hint 2

Each 1 digit in the binary representation of n is exactly one box: the digit at position i (counting from 0 at the right) stands for the box of size 2^i. Taking the largest box that fits finds the same boxes, but in descending order, and n can need positions up to 59, beyond 32-bit integers.

report an issue Prefer your own editor? sooks prep 276 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.