Find the player to reach at least N by multiplying with any value from given range
Last Updated :
07 Apr, 2022
Given an integer N, the task for two players A and B is to make the value of X ( initialized with 1) at least N by multiplying X with any number from the range [2, 9] in alternate turns. Assuming both players play optimally. the task is to find the player to obtain a value ? N first.
Examples:
Input: N = 12
Output: Player B
Explanation:
Initially, X = 1.
A multiplies X with 9. Therefore, X = 1 * 9 = 9.
In second turn, B multiplies X with 2. Therefore, X = 9*2 = 18
Therefore, B wins.
Input: N = 10
Output: Player B
Approach: The idea is to use the concept of combinatorial game theory. Find the positions from where, if a number is multiplied with X leads to victory and also the positions which lead to a loss. Below are the steps:
- In combinatorial game theory, let’s define that an N position is a position from which the next player to move wins if he plays optimally and P-position is a position where the next player to move always loses if his opponent plays optimally.
- The lowest position that can be reached up to N is, say res = ceil(N/9). Therefore, all the positions from [res, res + 1, res + 2, ….., N – 1] are N positions.
- The only positions that are forced to move to [res, res + 1, …, (N – 1)] are those that when multiplied by 2, and they lie in that interval, which is given by Y = ceil(res/2),
- Therefore, [Y, Y + 1, …, (res – 1)] are all the P-positions.
- After repeating the above steps until the interval containing 1 is found if 1 is an N-position then A wins, else B wins.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
char Winner( int N)
{
bool player = true ;
while (N > 1)
{
int den = (player) ? 9 : 2;
int X = N/den, Y = N%den;
N = (Y)? X + 1: X;
player = !player;
}
if (player)
return 'B' ;
else
return 'A' ;
}
int main()
{
int N = 10;
cout << Winner(N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static char Winner( int N)
{
boolean player = true ;
while (N > 1 )
{
int den = (player) ? 9 : 2 ;
int X = N / den;
int Y = N % den;
N = (Y > 0 ) ? X + 1 : X;
player = !player;
}
if (player)
return 'B' ;
else
return 'A' ;
}
public static void main(String[] args)
{
int N = 10 ;
System.out.print(Winner(N));
}
}
|
Python3
def Winner(N):
player = True
while N > 1 :
X, Y = divmod (N, ( 9 if player else 2 ))
N = X + 1 if Y else X
player = not player
if player:
return 'B'
else :
return 'A'
N = 10
print (Winner(N))
|
C#
using System;
class GFG
{
static char Winner( int N)
{
bool player = true ;
while (N > 1)
{
int den = (player) ? 9 : 2;
int X = N / den;
int Y = N % den;
N = (Y > 0) ? X + 1 : X;
player = !player;
}
if (player)
return 'B' ;
else
return 'A' ;
}
static public void Main()
{
int N = 10;
Console.WriteLine(Winner(N));
}
}
|
Javascript
<script>
function Winner(N)
{
var player = Boolean( true );
while (N > 1)
{
var den = (player) ? 9 : 2;
var X = parseInt(N/den), Y = parseInt(N%den);
N = (Y)? X + 1: X;
player = !player;
}
if (player)
document.write( 'B' );
else
document.write( 'A' );
}
var N = 10;
Winner(N);
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...