CF1646C.Factorials and Powers of Two

普及/提高-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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!=1⋅2⋅…⋅dd!=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.

一个数被称为“强数”,如果它是 2 的幂或一个阶乘数。换言之,若存在一个非负整数 dd,使得 m=2dm = 2^d 或 m=d!m = d!(其中 d!=1⋅2⋅…⋅dd! = 1 \cdot 2 \cdot \ldots \cdot d;特别地,0!=10! = 1),则称该数 mm 为强数。例如,11、44 和 66 是强数,因为 1=1!1 = 1!,4=224 = 2^2,且 6=3!6 = 3!;但 77、1010 和 1818 不是强数。

给定一个正整数 nn,求最小的整数 kk,使得 nn 可表示为 kk 个互不相同的强数之和;若不存在这样的 kk,请说明这一点。

输入格式

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

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

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1001 \le t \le 100)。随后是测试用例的描述。

每个测试用例仅由一行组成,包含一个整数 nn(1≤n≤10121\le n\le 10^{12})。

输出格式

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.

对于每个测试用例,请在单独的一行上输出答案。

如果 nn 无法表示为若干互不相同的强数(powerful numbers)之和,则输出 −1-1。

否则,输出一个正整数——即满足条件的最小可能的 kk 值。

输入输出样例

  • 输入#1

    4
    7
    11
    240
    17179869184

    输出#1

    2
    3
    4
    1

说明/提示

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.

在第一个测试用例中,77 可以表示为 7=1+67=1+6,其中 11 和 66 均为“强数”(powerful number)。由于 77 本身不是强数,因此本例中 kk 的最小可能值为 k=2k=2。

在第二个测试用例中,1111 可表示为三个强数之和,例如 11=1+4+611=1+4+6。可以证明:不存在将 1111 表示为至多两个强数之和的方法。

在第三个测试用例中,240240 可表示为 240=24+32+64+120240=24+32+64+120。注意 240=120+120240=120+120 不是合法表示,因为所用的强数必须互不相同。

在第四个测试用例中,17179869184=23417179869184=2^{34},因此 1717986918417179869184 本身是一个强数,此时 kk 的最小值为 k=1k=1。

输入解题思路,AI测评打分。不知道怎么写?

首页