fact = []
i = 2
x = 1
while x < 1e13:
fact.append(x)
x *= i
i += 1
l = len(fact) def main():
t = int(input())
allans = []
for _ in range(t):
n = int(input())
ans = 1000
if bin(n).count('1') == 1: ans = 1
for mask in range(1 << l):
total = 0
for i in range(l):
if (mask & (1 << i)) > 0:
total += fact[i]
if total <= n:
cnts = bin(mask).count('1') + bin((n - total)).count('1')
ans = min(ans, cnts)
else:
break
allans.append(ans)
multiLineArrayPrint(allans)
return
import sys
input=sys.stdin.buffer.readline
def oneLineArrayPrint(arr):
print(' '.join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print('\n'.join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
def makeArr(defaultValFactory,dimensionArr): dv=defaultValFactory;da=dimensionArr
if len(da)==1:return [dv() for _ in range(da[0])]
else:return [makeArr(dv,da[1:]) for _ in range(da[0])]
def queryInteractive(a, b, c):
print('? {} {} {}'.format(a, b, c))
sys.stdout.flush()
return int(input())
def answerInteractive(x1, x2):
print('! {} {}'.format(x1, x2))
sys.stdout.flush()
inf=float('inf')
from math import gcd,floor,ceil
import math
for _abc in range(1):
main()
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll fac[15];
ll minimum;
ll counter = 0;
void generate(ll n, ll largest)
{
if(largest == 0)
{
if(n < 0) return;
int b = 0;
for(ll i = 1; i <= n; i=(i<<1)) if(n&i){b++;}
counter+=b;
minimum = min(minimum, counter);
counter-=b;
}
else
{
if(n < 0) return;
generate(n, largest - 1);
counter++;
generate(n - fac[largest], largest - 1);
counter--;
}
}
ll solve()
{
ll n; cin >> n;
ll largest_fac = (upper_bound(fac, fac + 15, n) - fac) - 1;
minimum = LLONG_MAX;
generate(n, largest_fac);
return minimum;
}
int main()
{
fac[1] = 1;
for(ll i = 2; i < 15; i++)
{
fac[i] = fac[i-1] * i;
}
int t; cin >> t; while(t--) cout << solve() << "\n";
}
1302. Deepest Leaves Sum | 1209. Remove All Adjacent Duplicates in String II |
994. Rotting Oranges | 983. Minimum Cost For Tickets |
973. K Closest Points to Origin | 969. Pancake Sorting |
967. Numbers With Same Consecutive Differences | 957. Prison Cells After N Days |
946. Validate Stack Sequences | 921. Minimum Add to Make Parentheses Valid |
881. Boats to Save People | 497. Random Point in Non-overlapping Rectangles |
528. Random Pick with Weight | 470. Implement Rand10() Using Rand7() |
866. Prime Palindrome | 1516A - Tit for Tat |
622. Design Circular Queue | 814. Binary Tree Pruning |
791. Custom Sort String | 787. Cheapest Flights Within K Stops |
779. K-th Symbol in Grammar | 701. Insert into a Binary Search Tree |
429. N-ary Tree Level Order Traversal | 739. Daily Temperatures |
647. Palindromic Substrings | 583. Delete Operation for Two Strings |
518. Coin Change 2 | 516. Longest Palindromic Subsequence |
468. Validate IP Address | 450. Delete Node in a BST |