Jump off the slate _ Cattle Network

Links: https://www.nowcoder.com/questionTerminal/4284c8f466814870bae7799a07d49ec8
Source: Cattle-off network

Xiao Yi came before a stone road, every stone on the next numbered from 1: 1,2,3 .......
this stone road in order to move forward according to specific rules: For Xiao Yi is currently located No. K slate, Xiao Yi only a single jump forward about a number K (without the 1 and K) step, skip i.e. K + X (X is a divisor of K and 1 non itself) of position. Xiao Yi is currently in the number N of the stone, he wanted to jump to number exactly slate M to go, Xiao Yi would like to know a minimum of jump a few times to get there.
For example:
N = 4, M = 24:
4-> 6-> 8-> 12-> of 18-> 24
then jumps Xiao Yi minimum of 5 times, 24 can jump from the stone slabs 4
Input Description:  input line, there are two integers N, M, separated by a space. (4 ≤ N ≤ 100000) ( N ≤ M ≤ 100000)
Description Output:  Output Xiao Yi jump requires a minimum number of steps, if the output does not reach -1

Ideas:

  1. res array is stored (step per unit length are approximate origin of the steps) of the desired minimum number of steps from the start position to the index, it is not up to the default (INT_MAX);
  2. Traversal starts from the starting point (i = N), if the current position is not reachable, directly into the next cycle, indicating that it is unavailable;
  3. Otherwise, away from the position i is calculated, array RES value accessible position corresponding to optional step which (vector v), traversing v, update step away from the position i (steps of IT), i.e., from the starting point to the position the current minimum number of steps it i + 1. (I + it original location up and unreachable both cases, a detailed discussion see the code comments)
  4. Finally, res determination value stored in the array with the least number of steps for each position from the position N to the [N ~ M] Room
#include <iostream>
#include <algorithm>
#include <climits>
using namespace std;

void func(int x, vector<int>& v) //求x除1和其本身的约数
{
    int ret = 0;
    for(int i = 2; i*i <= x; ++i)
    {
        if(x % i == 0)
        {
            v.push_back(i);
            if(x / i != i)
                v.push_back(x / i);
        }
    }
} 

Int main () 
{ 
    int N, M;
     the while (N && CIN CIN >> >> M) 
    { 
        Vector < int > RES (M + . 1 , INT_MAX); // record from the position N to (cur_index) required for step number, INT_MAX as unreachable, the initial value INT_MAX 
        RES [N] = 0 ; 
         for ( int I = N; I <M; ++ I) 
        { 
            IF (RES [I] == INT_MAX)
                 Continue ;
             // less, up to position i 
            Vector < int > V; 
            FUNC (i, V); // V i for a divisor, i.e., away from the position i, the optional step 
            for ( const Auto IT: V) 
            { 
                IF (! IT + i <= M && RES [IT + i] = INT_MAX) 
                     / / I it steps down from the position, the end does not exceed M;
                     // position i + it can reach the original, smaller update 
                    RES [i + it] = min (RES [i + it], RES [I] + . 1 ); 
                     // I away from the position, once it reaches the position of the steps may be i + it,
                     // i.e. to the position i + it from N, so an alternative is to take ret [i] +1 step, the original value reserved smaller 
                the else  IF (it + i <= M)
                     // position i + present it is not reachable, it is known from position i to go to a step it can be reached that position, and therefore is updated to ret [i] +1 
                    RES [+ I] = RES [I] + . 1;
            } 
        } 
        IF (RES [M] == INT_MAX) // each step are approximate unreachable 
            COUT << - . 1 << endl;
         the else                   // up 
            COUT << RES [M] << endl; 
    } 
    return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/dabai56/p/10971184.html