#include #include #include using namespace std; int N; int get1Nums(int n) { char S[100]; memset(S,'\0',100); sprintf(S,"%x",n); int num=0; char *p=S; while (*p!='\0'){ switch(*p){ case '1'://0001 case '2'://0010 case '4'://0100 case '8'://1000 num+=1; break; case '3'://0011 case '5'://0101 case '6'://0110 case '9'://1001 case 'a'://1010 case 'c'://1100 num+=2; break; case '7'://0111 case 'b'://1011 case 'd'://1101 case 'e'://1110 num+=3; break; case 'f': num+=4; break; } p++; } return num; } int GetMinCost(int n,vector &A,vector &visited,vector &cost) { if (cost[N]!=0){ return cost[N]; } bool left=false; bool right=false; visited[n]=true; int nRight=n+A[n]; if (nRight<=N){ if (!visited[nRight]){ visited[nRight]=true; cost[nRight]=cost[n]+1; GetMinCost(nRight,A,visited,cost); right=true; } } int nLeft=n-A[n]; if (nLeft>0){ if (!visited[nLeft]){ visited[nLeft]=true; cost[nLeft]=cost[n]+1; GetMinCost(nLeft,A,visited,cost); left=true; } } if (!left && !right){ return -1; } } int main(int argc, char* argv[]) { cin>>N; vector A(N+1); vector visited(N+1); vector cost(N+1,0); for (int i=1;i<=N;i++){ A[i]=get1Nums(i); } visited[1]=true; cost[1]=1; GetMinCost(1,A,visited,cost); if (cost[N]!=0){ cout<