Codeforces Round #774 (Div. 2)


比赛链接:

https://codeforces.com/contest/1646

C. Factorials and Powers of Two

题目大意:

\(n\) 最少能由几个不同的 powerful 数组成,定义 \(2^d\) 和 d! 都是 powerful 数。

思路:

一个数肯定能由几个 \(2^d\) 组成,而 \(n\) 的数据范围是 1e12,所以我们可以暴力跑出所有 d! ,然后用 \(dfs\) 找所有小于 \(n\) 的组合,将剩下的数用二进制表示就可以了。

代码:

#include 
using namespace std;
#define all(x) (x).begin(), (x).end()
#define LL long long
#define pb push_back
LL T = 1, n, ans;
vector  num;
void init(){
	LL k = 1;
	for (LL i = 1; i <= 15; i++){
		k = k * i;
		num.pb(k);
	}
}
void dfs(LL now, int p, LL k){
	if ( p >= num.size() || now > n ) return;
	ans = min(ans, k + __builtin_popcountll(n - now) );
	dfs(now + num[p], p + 1, k + 1);
	dfs(now, p + 1, k);
	
}
void solve(){
	scanf("%lld", &n);
	ans = __builtin_popcountll(n);
	dfs(0, 0, 0);
	cout << ans << "\n";
}
int main(){
	init();
	cin >> T;
	while(T--)
		solve();
	return 0;
}