cf #774 div.2 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\)能由几个不同的\(2^k\)表示:就看\(n\)的二进制中有多少个\(1\)


首先查看 \(14! = 87178291200 ; 15! = 1311969335296\)

因为n最大就是\(10^{12}\)所以最大含有\(14!\)

然后我们枚举是不是含有\(1!, 2!, ..., 14!\) ,假设选择了其中\(f\)个阶乘,其和为\(s\),那么剩下的数就是\(n-s\)\(n-s\)能表示成的\(2\)的幂的个数为\(n-s\)二进制中1的个数,此时结果就是$ f + get_bits(n-s)$


对于枚举枚举是不是含有\(1!, 2!, ..., 14!\) ,也采用二进制的方法!!!

       // 0 - 13位,每位代表一个情况
		for(int i = 0; i < (1<<14); i++)
		{
			for(int j = 0; j < 14; j++)
			{
				if(i & (1<<j)) xxx;
			}
		
		}		

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N = 1 << 15;

int t;

int get_bits(LL t)
{
	int res = 0;
	while(t)
	{
		if(t & 1) res++;
		
		t >>= 1;
	}
	return res;
}

int main()
{
	LL fact[16];
	fact[0] = 1;
	for(int i = 1; i < 14; i++) fact[i] = fact[i-1]*(i+1);
	
	scanf("%d", &t);
	
	while(t--)
	{
		LL n;
		scanf("%lld", &n);
		
		int res = 0x3f3f3f3f;	
		// 每个阶乘可选可不选,一共有2^14种可能,用位运算表示 
		// 0 - 13位 
		for(int i = 0; i < (1<<14); i++)
		{
			// 14位,看每位是不是1 
			int c = 0;
			LL s = 0; // 表示选择了某几个阶乘的和
			for(int j = 0; j < 14; j++)
			{
				if(i & (1<<j)) s += fact[j], c++;
			}
			
			if(s <= n) res = min(res, c + get_bits(n - s));
		}
		printf("%d\n", res);
	
	}
		
} 

时间复杂度

\(O(2^{14} )\)

参考文章

https://codeforces.com/blog/entry/100584

posted @ 2022-03-15 15:32  VanHope  阅读(48)  评论(0编辑  收藏  举报