原题连接:https://codeforces.com/contest/1646/problem/C
题意:当一个数由阶乘或者2的幂次组成时,可以称它为强大的。现在给一个正整数n,找到最小的个数k使得n是由k个阶乘或者2的幂次相加得到的强大的数。
思路: 根据n的范围1<=n<=1e12,我们可以发现在此范围内的阶乘数量只有15个,所以我们可以先将它预处理一下,再二进制枚举选哪个,通过__builtin_popcountll函数取min即可。
#define _CRT_SECURE_NO_WARNINGS 1
#include <math.h>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#define FOR(a, b) for (int i = a; i <= b; i++)
#define ROF(a, b) for (int i = a; i >= b; i--)
#define FORj(a, b) for (int j = a; j <= b; j++)
#define ROFj(a, b) for (int j = a; j >= b; j--)
#define FORk(a, b) for (int k = a; k <= b; k++)
#define ROFk(a, b) for (int k = a; k >= b; k--)
#define mem(i, a) memset(i, a, sizeof(i))
#define ll long long
#define inf 0x3f3f3f3f
#define PII pair<int, int>
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define int long long
#define endl '\n'
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define pi acos(-1.0)
using namespace std;
const int maxn = 2e6 + 5;
int a[15]= {1,2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600,6227020800,87178291200,1307674368000};
void solve() {
int n,ans=inf;
cin>>n;
FOR(0,(1<<15)-1) {
int sum=0,cnt=0;
FORj(0,14) {
if(i&(1<<j)) {
sum+=a[j];
cnt++;
}
}
ans=min(ans,cnt+__builtin_popcountll(max(0ll,n-sum)));
}
cout<<ans<<endl;
}
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int _;
cin >> _;
while (_--)
solve();
return 0;
}