-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdown_to_zero_11.cpp
More file actions
62 lines (59 loc) · 1.25 KB
/
down_to_zero_11.cpp
File metadata and controls
62 lines (59 loc) · 1.25 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
*
* Tag: Data Structure (Queue)
* Time: O(n)
* Space: O(n)
*/
#include <cmath>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1001010;
bool vis[N];
int n;
void init(){
memset(vis, 0, sizeof(vis));
}
void bfs(pair<int,int> &cur, queue<pair<int,int>> &q){
int m = cur.first;
for(int i = 2; i <= sqrt(m); ++ i){
if(m%i == 0){
int nxt = max(i, m/i);
if(!vis[nxt]){
vis[nxt] = 1;
q.push(make_pair(nxt, cur.second + 1));
}
}
}
if(m && !vis[m - 1]){
vis[m - 1] = 1;
q.push(make_pair(m - 1, cur.second + 1));
}
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int T;
scanf("%d",&T);
while(T --){
scanf("%d",&n);
init();
queue<pair<int,int>> q;
int ans = 0;
q.push(make_pair(n, 0));
while(!q.empty()){
pair<int,int> cur = q.front();
q.pop();
if(cur.first == 0){
ans = cur.second;
break;
}
bfs(cur, q);
}
printf("%d\n",ans);
}
return 0;
}