We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<stdio.h>#include<math.h>#include<limits.h>#define MAX_N 1000001intdp[MAX_N];voidprecompute_min_moves(){dp[0]=0;dp[1]=1;for(inti=2;i<MAX_N;i++){// Option 1: Decrementdp[i]=1+dp[i-1];// Option 2: Factorization// We only need to find factors up to sqrt(i)for(intj=2;j*j<=i;j++){if(i%j==0){intlargest_factor=i/j;if(dp[i]>1+dp[largest_factor]){dp[i]=1+dp[largest_factor];}}}}}intmain(){precompute_min_moves();intQ;scanf("%d",&Q);while(Q--){intN;scanf("%d",&N);printf("%d\n",dp[N]);}return0;
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Down to Zero II
You are viewing a single comment's thread. Return to all comments →