-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbien_doi_so_tu_nhien.cpp
More file actions
48 lines (48 loc) · 1.04 KB
/
bien_doi_so_tu_nhien.cpp
File metadata and controls
48 lines (48 loc) · 1.04 KB
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
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<unordered_set>
using namespace std;
long long mod = 1e9 + 7;
void slove(){
int n;
cin >> n;
unordered_set<int> s;
queue< pair<int,int> > q;
q.push({n,0});
s.insert(n);
while(!q.empty()){
pair<int,int> p=q.front();
q.pop();
if(p.first==1){
cout << p.second << "\n";
return;
}
if(s.find(p.first-1)==s.end()&&p.first>1){
q.push({p.first-1,p.second+1});
s.insert(p.first-1);
}
for(int i=2;i<=sqrt(p.first);i++){
if(p.first%i==0){
if(s.find(p.first/i)==s.end()){
q.push({p.first/i,p.second+1});
s.insert(p.first/i);
}
}
}
}
}
int main(){
int t=1;
cin >> t;
while(t--){
slove();
}
return 0;
}