Dynamic Programming (1) --- From a simple example

1, a simple example from

Alice and Bob play a game, they take turns. Alice upper hand start.

Initially, there is a number N on the board. Each player's turn, the player needs to do the following:

    selected as an x, satisfying 0 <x <N, and N% x == 0.
    Replacement digital x N on the board - with N.

If the player can not perform these operations, it will lose the game.

Only Alice returns True only when the victory in the game, otherwise false. Suppose that two players are in the best state to participate in the game.

Example Input: N = 2,

Examples of output: true.

Explain: N is 2, the number 2 is only about 1, after a selection Alice, Bob's input is not an operation.

 

Problem-solving ideas:

  For entering a new algorithm pit Meng, the first reaction is to see the problem, but also the people's natural reaction is carried out in a manner solving the subject described. A lot of people have this doubt, when I got a question of time, how do you know in the process of learning algorithm which algorithm to use? Indeed, when a question of time to get a lot of people are dazed, everyone's first reaction is described in accordance with the subject of violent solved. I think violence is a good way to solve, at least a lot of good ideas is in the process of solving the violence comes slowly improved. When more done enough questions when reading the casual working we need to know what algorithm, like the college entrance examination, see question number you know what to test.

  Back to this question, first of all as described in casual working, reproduce casual working requirements, violent solved. Analysis stem that can be known, if the input number N = 1, alias digital sente Bureau, at this time can not operate alias, alias failure, return flase. You can get the following conclusions:

  When it came alias Bureau, if N = 1, then the alias defeated, return flase, when the opponent bureau, N = 1 the alias wins, returns true.

  In code design, you need to have the following parameters: N ---- characterize the value function of the inputs, whether isAlias ​​---- alias is characterized by the Bureau, because we have to come to the situation alias of victory, so the function return value , this parameter is required for reference.

  Casual working it clear that, instead of using Nx N, which is typical of recursive thinking, therefore, the use of recursive function design. The stop condition is a function of N = 1, if this time isAlias ​​= true, then return Flase, true otherwise.

  According to the above conclusion, we can have the following code

Boolean Gt ( int N, Boolean isAlias) {
         / * analysis can know, when alias turn operation, if one is to get, and fails, if the opponent Bureau, the opponent has won an alias * / 
        IF (isAlias && == N. 1 )
             return  to false ;
         IF (! && N == isAlias. 1 )
             return  to true ;
         / * find all alias can be chosen or Bob X * / 
        List <Integer> = Vector new new the LinkedList <> () ;
         for ( int I =. 1; I <N; ++ I) {
             IF (% I N == 0 )  
                vector.add (I);
        } 
        / *Analysis, the value of x lot, but no matter what choice x, a N corresponds to only one output, which means that for all x, the result is the same as its been, therefore, only need to calculate it again * / 
        return Gt ( vector.get-N (0) ,! isAlias); 
    }

After further analysis, for a N, regardless alias just get to choose which x, and only a single output, therefore, no need to find all of x, because the number about to N> 1,1 N are, therefore, there is the following code:

Boolean Gt ( int N, Boolean isAlias) {
         / * analysis can know, when alias turn operation, if one is to get, and fails, if the opponent Bureau, the opponent has won an alias * / 
        IF (isAlias && N == 1 )
             return  false ;
         IF (isAlias N == 1 &&! )
             return  to true ; 

        / * analysis, the value of x lot, but no matter what choice x, a N corresponds to only one output, that is for all x, which is the result obtained is the same, therefore, only need to calculate again * / 
        return Gt (N-,!. 1 isAlias); 
    }

  To focus on, and after we prune, we find that magic, f (n) and f (n-1) had a relationship (the code above last sentence), think about it this time, dynamic programming, the core is not that recurrence equation to find it? At this point, we cock, know that f (n) and f (n-1) there is a link, as long as we find out what kind of contact, you can know the recurrence equation, then we can find out Solutions of the prior N-1, N solution introduction. Therefore, at this time our main work from a full simulation of the operation into a casual working to solve the recurrence equation. From the above code, we can see, f (n, isalias) = ​​f (n-1,! Isalias), this equation contains what deep recursive relationship? Equation on the left represents the input to get the right alias, equations characterizing the bob to get input. Obviously, this is a zero-sum game, or is a Dead is not your game.

  Then back to the problem body --alias, the alias if the input is N, and, alias total of m x can be selected, referred to as X, each x is apparent to X, (Nx of) the input bob, then can be :! f (N) = f (Nx). This sentence is more popular saying: for any number of about N x, if f (Nx) = flase, will have f (N) = true, and vice versa. Based on this conclusion or recurrence equation, we can give solution Dynamic Programming:

Boolean Gt ( int N) {
         Boolean [] = ANS new new  Boolean [N +. 1 ];
         / * solutions of N-1 first obtains the front, i = N, the solution will solve the N * / 
        for ( int I =. 1 ; I <= N; ++ I) {
             for ( int J =. 1; J <I; ++ J) {
                 / * for any x belongs to X (X is defined by the known casual working) f (n) =! F (NX) * / 
                IF (! I% J == 0 && ANS [I - J]) { 
                    ANS [I] = to true ;
                     BREAK ; 
                } 
            } 
        }
        return years [N]; 
    }

  More than can be said to get a solution to this problem of dynamic programming, dynamic programming ideas above also applies to the general dynamic programming problem, namely to find the recurrence equation, or the state transition equation, then write code according to the state transition equation, we find that, dynamic programming problem can be solved with a recursive way switch, because our brains recursive manner and ways of thinking is very similar, so when you think a problem can be recursively to do, you should think about whether you can use dynamic programming solving.

  For solving the above introductions dynamic programming to this problem, but the algorithm needs time to ask yourself do, the problem could be further optimized. On this question, we know that, for any x belongs to X, the result is the same, therefore, in the above code, we use the break to a premature end, but then I thought, 1 is N> about number 1, So, why not just use it 1, the above problem is transformed into:

Boolean Gt ( int N) {
         Boolean [] = ANS new new  Boolean [N +. 1 ];
         / * first solution is obtained. 1 front-N * / 
        for ( int I =. 1; I <= N; ++ I) {
             IF (ANS [I. 1-! ]) 
                ANS [I] = to true ; 
        } 
        return ANS [N]; 
    }

  Adult words are translated

  

! Years [N] = years [N-1];

  We know that when N = 1 is false; recursive obtained when N = 2, is true, N = 3 is false, N = 4 time is true. . . . Where N is odd found, the return value is false, when N is an even number, the return value is true. Thus the following super simple code

boolean Gt(int N){
        return (N&0x01) == 0;
    }

  I do not think anyone from the casual working in a sweep in the past, will be able to get above most simple algorithm, the algorithm must be excellent improvements comes slowly, never miss a flash of inspiration, but also never satisfied to existing algorithms. Practical, step-by-step iterations, tuning, will get really good algorithm.

  

  

 

Guess you like

Origin www.cnblogs.com/establish/p/11599042.html