-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThe Witches of HEgwarts.cpp
55 lines (55 loc) · 1.09 KB
/
The Witches of HEgwarts.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
#include<bits/stdc++.h>
#define fast ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define f(n) for(int i=0;i<n;i++)
#define ff(n) for(int j=0;j<n;j++)
#define pb(x) push_back(x)
#define mp make_pair
#define ll long long
#define mod 1000000007
using namespace std;
map <ll int,ll int> dp,vis;
int solve(ll int t)
{
queue <int> q;
q.push(t);
dp[t]=0;
vis[t]++;
while(!q.empty())
{
int n = q.front();
q.pop();
if(n==1)
return dp[1];
if(vis[n-1]==0)
{
vis[n-1]++;
q.push(n-1);
dp[n-1]=dp[n]+1;
}
if(vis[(n/2)]==0 && n%2==0)
{
vis[n/2]++;
q.push(n/2);
dp[n/2]=dp[n]+1;
}
if(vis[n/3]==0 && n%3==0)
{
vis[n/3]++;
q.push(n/3);
dp[n/3]=dp[n]+1;
}
}
}
int main()
{
ll int t,n;
cin>>t;
while(t--)
{
dp.clear();
vis.clear();
cin>>n;
cout<<solve(n)<<endl;
}
return 0;
}