【二进制枚举】【CF Div2 C】
我去, 看了官方题解,用二进制枚举, 一共就最多1! ~ 15!, 用二进制来枚举, 我去我怎么没想到啊啊啊
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N = 2e5+10;
const LL MAXX = 1e12;//注意const后面是long long
LL jch[100];
int idx;
int bist_num(LL nn)
{
int res =0;
while(nn)
{
if(nn&1)res ++;
nn>>=1;
}
return res;
}
void init()
{
jch[1] = 1;
for(int i = 2; jch[i-1]<=MAXX; i++) jch[i] = jch[i-1]*(LL)i, idx=i+1;
}
int main()
{
init();
int t;
cin >> t;
while(t --)
{
LL n, res = 99;
cin >> n;
for(LL i = 0; i < (1<<idx); i ++)
{
LL now = 0, cout = 0;
for(int j = 0; j < idx; j ++)
if(i>>j&1)
now+=jch[j], cout ++;
if(now<=n)res = min(res, cout + bist_num(n-now));
}
cout <<res <<endl;
}
return 0;
}