Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using ll = long long;
- using ld = long double;
- #define pb push_back
- #define ff first
- #define ss second
- #define oo 1000000000
- mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
- const int mx=1e6+5;
- int main(){
- //ios::sync_with_stdio(false); cin.tie(0);
- int n;
- while(cin>>n){
- bool used[mx];
- int dis[mx];
- memset(used,0,sizeof(used));
- memset(dis,0,sizeof(dis));
- queue<int> q;
- q.push(n);
- dis[n]=0;
- used[n]=1;
- while(!q.empty()){
- int s=q.front(); q.pop();
- if(s==1){
- break;
- }
- if(s%2==0 && !used[s/2]){
- used[s/2]=1;
- q.push(s/2);
- dis[s/2]=dis[s]+1;
- }
- if(s%3==0 && !used[s/3]){
- used[s/3]=1;
- dis[s/3]=dis[s]+1;
- q.push(s/3);
- }
- if(!used[s-1]){
- used[s-1]=1;
- q.push(s-1);
- dis[s-1]=dis[s]+1;
- }
- }
- cout << dis[1] << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement