-
Notifications
You must be signed in to change notification settings - Fork 86
/
program.cpp
92 lines (89 loc) · 1.58 KB
/
program.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
void fast_io()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
}
ll dp[21];
long long int fac(long long int n)
{
if(n==1)return 1;
if(dp[n]!=0) return dp[n];
return dp[n] = n*fac(n-1);
}
long long int hp(long long int n)
{
long long int res = 0;
for (ll i=n; i>=1; i--)
{
// If i is a power of 2
if ((i & (i-1)) == 0)
{
res = i;
break;
}
}
return res;
}
int main()
{
fast_io();
ll t;
cin>>t;
vector<ll> v(21);
for(int i=1;i<21;i++)
v[i]=fac(i);
while(t--)
{
long long int n,ctr=0;
cin>>n;
bool flag=false;
long long int temp1,temp2;
while(flag!=true )
{
if(n==1)
{
ctr++;
flag=true;
break;
}
temp1=hp(n);
// cout<<"temp1"<<temp1<<endl;
if(temp1==n)
{
ctr++;
flag=true;
break;
}
ll i=1;
while(v[i]<=n)
{
i++;
}
temp2=v[i-1];
if(temp2==n)
{
ctr++;
flag=true;
break;
}
if(temp1>temp2)
n=n-temp1;
else
n=n-temp2;
ctr++;
}
cout<<ctr<<endl;
}
return 0;
}
// long long int rem=1;
// long long int i=1;
// while(rem<=n)
// {
// rem*=i;
// i++;
// }
// return rem/--i;