Leetcode 1025. divisor Game

Title Description

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

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

  • Is 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 1:

Input: 2

Output: true

Explanation: Option 1 Alice, Bob can not operate.

Example 2:

Input: 3

Output: false

Explanation: Option 1 Alice, Bob also choose 1, then Alice can not operate.

Best means, is false, then the current number returned true figures by subtracting either a divisor for each other, otherwise the current number returned false.

solution

May wish to return to the digital value of x, then when x = 1, f (1) is false to f (x).

When x = 2, the divisor factor is 1, since f (1) = false, so that f (2) = true
if x = 3, the divisor factor is 1, since f (2) = true, so f (3) = false
when x = 4, the divisor factor is 1, 2, since f (3) = false, so that F (. 4) = to true
...

We observe that for the first few numbers, if x is odd, then f (x) = false; if x is even, then f (x) = true.

When x> 4, if x is odd, then x divisor is odd, i.e. x subtracted divisor are all even, because the even function is true, then f (x) = false;
when x> 4 , if x is even, because x-1 is an odd number, because of the odd function is false, then f (x) = true;

It can be seen, all of the odd function returns false, the function returns true even number.

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

Reproduced in: https: //www.jianshu.com/p/5ad590ed669f

Guess you like

Origin blog.csdn.net/weixin_34281477/article/details/91070665