Two players are playing a game starting with a number n. In each turn, a player can make any one of the subsequent moves:
- Divide n by any of its odd divisors greater than 1. Divisors of a number include the number itself.
- Subtract 1 from n if n > k where k < n.
Player 1 makes the primary move, print “yes” if player 1 wins otherwise print “no” if both play optimally. The player who is unable to make a move loses the game.
Input: n = 12, k = 1
Player 1 first move = 12 / 3 = 4
Player 2 first move = 4 – 1 = 3
Player 1 second move = 3 / 3 = 1
Player 2 second move can be done and hence he looses.
Input: n = 1, k = 1
Player 1 first move is not possible because n = k and hence player 1 looses.
Approach: The idea is to analyze the problem for the following 3 cases:
- When integer n is odd, player 1 can divide n by itself, since it is odd and hence n / n = 1, and player 2 loses. Note that here n = 1 is an exception.
- When integer n is even and has no odd divisors greater than 1 then n is of the form 2x. Player 1 is bound to subtract it by 1 making n odd. So if x > 1, player 2 wins. Note that for x = 1, n – 1 is equal to 1, so Player 1 wins.
- When integer n is even and has odd divisors, the task remains to check if n is divisible by 4 then player 1 can divide n by its largest odd factor after which n becomes of the form 2x where x > 1, so again player 1 wins.
- Otherwise, n must be of form 2 * p, where p is odd. If p is prime, player 1 loses since he can either reduce n by 1 or divide it by p both of which would be losing for him. If p is not prime then p must be of the form p1 * p2 where p1 is prime and p2 is any odd number > 1, for which player 1 can win by dividing n by p2.
Below is the implementation of the above approach:
Time Complexity: O(sqrt(n))
Auxiliary Space: O(1)
- Number of wins for each player in a series of Rock-Paper-Scissor game
- Two player game in which a player can remove all occurrences of a number
- Find probability that a player wins when probabilities of hitting the target are given
- Game of N stones where each player can remove 1, 3 or 4
- Find the player who will win the Coin game
- Optimal Strategy for the Divisor game using Dynamic Programming
- Largest Divisor for each element in an array other than 1 and the number itself
- Largest Divisor of a Number not divisible by a perfect square
- Sum of largest divisor of numbers upto N not divisible by given prime number P
- Check which player visits more number of Nodes
- Game Theory (Normal-form Game) | Set 7 (Graphical Method [M X 2] Game)
- Game Theory (Normal-form Game) | Set 6 (Graphical Method [2 X N] Game)
- Game Theory (Normal form game) | Set 2 (Game with Pure Strategy)
- Game Theory (Normal-form game) | Set 3 (Game with Mixed Strategy)
- Check if the game is valid or not
- Game Theory (Normal-form Game) | Set 5 (Dominance Property-Mixed Strategy)
- Game Theory (Normal-form Game) | Set 4 (Dominance Property-Pure Strategy)
- Game Theory (Normal - form game) | Set 1 (Introduction)
- Combinatorial Game Theory | Set 2 (Game of Nim)
- Find the minimum possible health of the winning player
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.