Блог пользователя tbquan08hanoi

Автор tbquan08hanoi, 3 месяца назад, По-английски

Problem: A pile has $$$n$$$ stones and 2 players: Player A go first. Each move, the player will take $$$k$$$ stones from the pile, where $$$k$$$ is a prime number. Whoever can't make a move first is lost. Problem has multiple test. Constraints: $$$1 <= t <= 100$$$: Number of tests $$$2 <= n <= 3 \cdot 10^6$$$: The number of stones in the pile Output: A if A wins, B if B wins.

(Idk if it is a nim problem or not, that's the reason of the question mark) (Also, when I do $$$O((n)^2)$$$, I do precalculation: $$$res[0]=res[1]=0$$$(base case), $$$res[i]=1$$$ only if exist a prime $$$p$$$ where $$$res[i-p]=0$$$, else $$$res[i]=0$$$)

  • Проголосовать: нравится
  • +1
  • Проголосовать: не нравится

»
3 месяца назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

its not a nim problem though (get it?)

»
3 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Try applying sprague grundy theorem, You'll notice a pattern in it and after noticing it, you can come up to an O(1) solution.

You can study it from here How to calculate Grundy values?.

Pattern looks somewhat like this g(x) = x % 4

»
3 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
If you have learned anything from the recent EDU round you would first

Some more possible variations of Nim to brighten your day

  • Divisor Nim
  • Non-divisor Nim (coprime and non-coprime numbers allowed)
  • Non-prime Nim, as opposed to presented variation
  • Non-coprime nim (opposite of last EDU round)