-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bien_doi_so_tu_nhien.cpp
50 lines (47 loc) · 1.01 KB
/
Bien_doi_so_tu_nhien.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
#include<bits/stdc++.h>
#define ll long long
using namespace std;
typedef pair<ll,ll> ii;
int main(){
int t;
cin>>t;
while(t--){
ll n;
cin>>n;
deque<ii> dq;
dq.push_back({n,0});
set<ll> st;
st.insert(n);
while(dq.size()){
ii u = dq.front();
ll k = u.first;
dq.pop_front();
if(k==1){
cout<<u.second<<endl;
break;
}
else if(k==2){
cout<<u.second+1<<endl;
break;
}
else{
for(ll i=sqrt(k);i>1;i--){
if(k%i==0&&st.find(k/i)==st.end()){
dq.push_back({k/i,u.second+1});
st.insert(k/i);
}
}
if(st.find(k-1)==st.end()){
dq.push_back({k-1,u.second+1});
st.insert(k-1);
}
}
}
}
}
/*
3
17
50
100
*/