CF1646C.Factorials and Powers of Two

传统题 时间 2000 ms 内存 256 MiB 8 尝试 3 已通过 1 标签

Factorials and Powers of Two

A number is called powerful if it is a power of two or a factorial. In other words, the number mm is powerful if there exists a non-negative integer dd such that m=2dm=2^d or m=d!m=d!, where d!=12dd!=1\cdot 2\cdot \ldots \cdot d (in particular, 0!=10! = 1). For example 11, 44, and 66 are powerful numbers, because 1=1!1=1!, 4=224=2^2, and 6=3!6=3! but 77, 1010, or 1818 are not.

You are given a positive integer nn. Find the minimum number kk such that nn can be represented as the sum of kk distinct powerful numbers, or say that there is no such kk.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t1001 \le t \le 100). Description of the test cases follows.

A test case consists of only one line, containing one integer nn (1n10121\le n\le 10^{12}).

Output

For each test case print the answer on a separate line.

If nn can not be represented as the sum of distinct powerful numbers, print 1-1.

Otherwise, print a single positive integer  — the minimum possible value of kk.

Note

In the first test case, 77 can be represented as 7=1+67=1+6, where 11 and 66 are powerful numbers. Because 77 is not a powerful number, we know that the minimum possible value of kk in this case is k=2k=2.

In the second test case, a possible way to represent 1111 as the sum of three powerful numbers is 11=1+4+611=1+4+6. We can show that there is no way to represent 1111 as the sum of two or less powerful numbers.

In the third test case, 240240 can be represented as 240=24+32+64+120240=24+32+64+120. Observe that 240=120+120240=120+120 is not a valid representation, because the powerful numbers have to be distinct.

In the fourth test case, 17179869184=23417179869184=2^{34}, so 1717986918417179869184 is a powerful number and the minimum kk in this case is k=1k=1.

Samples

4
7
11
240
17179869184
2
3
4
1

在线编程 IDE

建议全屏模式获得最佳体验