Leetcode 1025. Divisor game | Python

Time:2020-12-16

1025. Divisor game


Source: leetcode https://leetcode-cn.com/problems/divisor-game

subject


Alice and Bob play games together. They take turns. Alice started first.

At first, there was a number n on the blackboard. In each player’s turn, players need to do the following:

  • Choose any onex, satisfy0 < x < NAndN % x == 0
  • useN - xReplace the number on the blackboardN

If the player is unable to perform these operations, he will lose the game.

Only when Alice wins in the game does she returnTrue, otherwise returnFalse。 Suppose that both players are playing at their best.

Example 1:

Input: 2
Output: true
Explanation: Alice chooses 1, Bob can't operate.

Example 2:

Input: 3
Output: false
Explanation: Alice chooses 1, Bob chooses 1, and then Alice cannot operate.

Tips:

  • 1 <= N <= 1000

Thinking of problem solving


Idea: recursion

Here, we need to find out the law from the topic. Here are the rules of the game in the title:

  • Given the number n, take any number x (0 < x < n), and N% x = = 0;
  • When selected, replace the original n with N – X.

Here, according to the above rules of the game, we first list the results of different values of n (1 < = n < = 1000)

  • When n = 1, because the extracted number x must be greater than 0 and less than N, there is no number that satisfies the conditions, and Alice is determined to lose;
  • When n = 2, here Alice can only take 1, then n will change to 1. According to the above analysis, at this time Bob can not operate, and it is determined that Bob loses and Alice wins;
  • When n = 3, Alice can only select 1, and then n becomes 2. Here, according to the analysis of n = 2, Bob takes 1 and Alice loses;
  • When n = 4, Alice takes 1 or 2. If you take 1, according to the analysis of n = 3, here Bob can only take 1, Alice will take 1 again, Bob can not operate, as a loss. Alice wins;
  • When n = 5, Alice can only take 1, then according to the analysis of n = 4, Alice will eventually lose;
  • ……

Here, we can find that when n is odd, Alice will lose if she chooses first, while when n is even, Alice will win first. Try to useMathematical inductionIt is proved that this conclusion is feasible

  • When n = 1 and N = 2, it can be judged directly and the conclusion is tenable.
  • When n > 2, discuss the situation according to the situation (the title has explained that both players participate in the game in the best state). Assuming that the conclusion of N < = m is true, then when n = m + 1, the situation is as follows:

    • When m is an even number, M + 1 is an odd number. The value of X must be the divisor of n (that is, M + 1). Then x can only be odd. Then the new n = N-X (i.e. m + 1-x) is even. At this time, M + 1-x < = m, if n < = m, the first player will win. So here, no matter what number Alice takes first, the rest is even. At this time, Bob’s turn to choose, then Bob will win, and Alice will lose;
    • When m is odd, M + 1 is even. In this case, the value of X can be even or odd. If Alice chooses the odd number first, then the rest must be odd, and M + 1-x < = m, then it’s Bob’s turn to choose. According to the previous analysis, when n < = m, in the case of odd number, the first player must lose, so no matter how Bob chooses at this time, everything will lose, then Alice wins.

Therefore, as long as the given n value is odd or even, the player can be judged to win or lose.

The code is very simple, because the title is a game problem, mainly from the title to sum up the law.

code implementation


class Solution:
    def divisorGame(self, N: int) -> bool:
        return N % 2 == 0

Achieving results


Leetcode 1025. Divisor game | Python

Welcome to our attention


The official account [book collection]

Recommended Today

Deeply analyze the principle and practice of RSA key

1、 Preface After experiencing many dark moments in life, when you read this article, you will regret and even be angry: why didn’t you write this article earlier?! Your darkest moments include: 1. Your project needs to be connected with the bank, and the other party needs you to provide an encryption certificate. You have […]