We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Algorithms
- Game Theory
- The Prime Game
- Discussions
The Prime Game
The Prime Game
Sort by
recency
|
22 Discussions
|
Please Login in order to post a comment
Here is The Prime Game problem solution in Python Java C++ and C programming - https://programs.programmingoneonone.com/2021/07/hackerrank-prime-game-problem-solution.html
IMPORTANT
There is an issue with the C++ starter code.
The function you are asked to complete takes
vector<int>
as input but the question stipulates that A[i] is less than 10^18, so int will not do, long is needed. - Change the function input tovector<long>
, - Change the code in the main function to read longs using stol() instead of stoi()This question is well worded (in my opinion) and really rewarding otherwise. If you are stuck, research NIM, the Sprague Grundy function, and XOR.
Always check the specifications :P
This problem is worded very poorly.
For instance:
Manasa plays the first move against Sandy. Who will win if both of them play optimally?
Whats optimal? Removing the most balls from a bucket? Removing the ideal number of balls to give the player the best chance of winning?
The example input moves includes both cases......
[Understood and Could Explain] :
A Nim game of
n
buckets anda[1], a[2], ..., a[n]
balls in each bucket cannot be simply divided inton
Nim games with 1 bucket anda[1], a[2], ..., a[n]
balls in each game. It's not correct to simply XOR all the losses/victories of each bucket to determine the winner of the whole game. i.e. Player who "wins" on even numbers of buckets may not lose the whole game.For example, consider a game with two buckets and
[2, 7]
balls respectively. Player 1 "wins" on both buckets but doesn't lose the game. He/She should not take one whole bucket and let the opponent take the other. Instead, the player should take 5 balls from the second bucket, leaving a situation of[2, 2]
to the opponent and then win the game.This is found by finding the grundy/nimber numbers (which was explained by other comments below) and
XOR
-ing them. The first few grundy numbers forrange(11)
were[0, 0, 1, 1, 2, 2, 3, 3, 4, 0, 0]
. A situation of[2, 7]
balls has grundy numbers of[1, 3]
for each bucket, and a grundy number of1 xor 3 == 2
for the whole game. So the player can remove 5 balls from the second bucket, making the remaining situation to have a grundy number of 0, therefore forcing the opponent to lose the game.Also try
[7, 8]
. The situation has a grundy number of3 xor 4 == 7
, which does not exist in the grundy numbers list. But the first player can remove 2 balls from the second bucket to change its grundy number from 4 to 3 and wins the whole game.[Don't Understand, Need Help] :
{2, 3, 5, 7, 11, 13}
is finite and not the whole set of prime numbers, it's expected to see the grundy numbers to loop with a much longer period, not a short one like this (9).For the follow input:
1 10 84 87 78 16 94 36 87 93 50 22
My solution ended with Sandy as the winner, but the test indicates that should be Manasa.
Am I missing something?
Here is my log moves, as the test says to be optimized: