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=2d or m=d!m=d!, where d!=1⋅2⋅…⋅dd!=1⋅2⋅…⋅d (in particular, 0!=10!=1). For example 11, 44, and 66 are powerful numbers, because 1=1!1=1!, 4=224=22, 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 (1≤t≤1001≤t≤100). Description of the test cases follows.

A test case consists of only one line, containing one integer nn (1≤n≤10121≤n≤1012).

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.

Example

input

4
7
11
240
17179869184

output

2
3
4
1

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=234, so 1717986918417179869184 is a powerful number and the minimum kk in this case is k=1k=1.

题目大意: 给一个数n,问能不能用 2的k阶幂,和k的阶乘组成。可以用多个上述数字组成,但是不能用重复的组成。

补充:

 这个题解就是暴力枚举。一个数有两种数组成,我们把由阶乘组成方式确定了,那么剩余的就一定是全由二进制组成,如果组成不了,那这种情况就不满足情况输出-1,-1。(当然是没有不能组成的情况。)

那问题就转换成,怎么枚举由K的阶乘组成的数呢?

我们可以枚举所有小于n的阶乘组成,n - 阶乘得到的数一定就是由2的k阶幂组成的数。

举例枚举阶乘的前几种方式:

1!

2!

2! + 1!

3!

3! + 1!

3! + 2! + 1!

 我们发现,这有点像二进制一样。

00       1!

10        2!

11        2! + 1!

100     3!

101    3! + 1!

111     3! + 2! + 1!

二进制对应的位置(从右数)如果是1的话, 假设位置是k,那当前枚举的阶乘里组成元素就有k!

于是我们可以从1枚举到+∞,每次又具体遍历当前数的二进制数。

 这种 选与不选 可以用0 1 代替。假如101,就代表,1和3被选取(从右数位置。)

然后十进制数的每次+1,都会使得二进制发生变化,产生一个新的枚举种类。(学过一点点数电都知道。)。并且保证最后枚举的二进制种类是完备的。

至于为什么枚举1 到 2^14,只是随便是设置的一个界限,只要不超时,尽管往大设置都可以。

只要循环里满足cnt < sum即可。

#include<bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i <= b; ++i)
#define per(i, a, b) for(int i = a; i >= b; --i)
const int N = 2e5 + 10;
using namespace std;
#define int long long

int p[200010];

void preprocess(){
	p[1] = 1;
	for(int i = 2;p[i-1] < 1e12;i++){
		p[i] = p[i-1]*i;
	}
}

int find(int x){
	int cnt = 0;
	while(x){
		if(x%2)cnt++;
		x /= 2;
	}
	return cnt;
}

void solve(){
	int n; cin >> n;
	int ans = find(n);
	rep(i,1,1LL << 14){
		int sum = 0,cnt = 0;
		rep(j,0,14){// 遍历当前数的二进制数
			if(i >> j & 1){
				sum += p[j+1];cnt++;
			}
		}
		if(sum <= n){
			ans = min(ans,cnt + find(n-sum));
		}
	}
	cout << ans << endl;
}

signed main(){
	preprocess();
    int t; cin >> t;
    while(t--) solve();
    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值