Codeforces #774 Problem C

算法考察:二进制枚举+阶乘分解+数学。

注:如过你不了解什么是二进制枚举这种基础算法,也可以自行百度自学。

C. Factorials and Powers of Two

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Copy

4
7
11
240
17179869184

output

Copy

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,和一个集和   Set   { x   |  x 是2的非负整数幂 或 x 是 从 0 到 任意 非负整数的阶乘           } 

其中 n < 1e 12;

问   n 是否能用 集合 set 中的   若干不同元素表示  如果能 ,最少用几个元素 表示 ?输出 这个最小值, 否则 输出 -1

 很显然的是 一定存在 一组 解 使其成立,因为 这个集合 包含了 2 的所有 非负整数幂  十进制数一定可以转化为一个二进制数,但这并不一定是最优解,那么如何得到最优解呢?

我么注意到   1e12< 14 !

那么 我们暴力枚举十四个阶乘数  扫一遍求其最优解,考虑到 14 是一个 很小的数字  我们可以采用二进制枚举 ,在2e 14 ==1e4 时间复杂度下求解 一个样例。

代码中用到了 __builtin_popcount() 函数 ,这个函数的返回值是int 型的(注意不要CE了,别问我为什么知道)。

意思为 求某个二进制数 n 有多少位为1.具体可以自行百度。类似于bitset 函数。

 AC 代码:

​

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

void solve (){
   ll n;
   cin>>n;
   ll fac[15];
   fac[0]=1;  
   
   for(ll i=1;i<=14;i++)
     fac[i]=fac[i-1]*i;//阶乘拆分
     int  ans=100;
     for(ll s=0;s<(1<<14);s++)// 枚举阶乘数的选择,s 代表选择方案
     {
	 	ll m=n;
	 	for(ll i=0;i<14;i++)
	 	{
		   if(s>>i&1 )  m-=fac[i+1];减去选择过的阶乘数
		
		}
	      if(m>=0)  ans=min(ans,__builtin_popcountll(s)+__builtin_popcountll(m) ) ;
	   	//更新答案
	 	
	 }
	
     cout<<ans<<endl;	
	
	
}

int main(){
	   ios::sync_with_stdio(false);
	   cin.tie(0);
	   int t;
	   cin>>t;
	   while(t--)
		{
		   solve();

		} 
	
	
	return 0;
}

​

​

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

litian355

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值