0PricingLogin
Competitive Programming Academy · Lesson

Nim and the Grundy Number

Solve impartial games with XOR.

Meet the Game of Nim

In Nim there are several piles of stones. On your turn you remove any number from one pile, and the player who takes the last stone wins. 🪨

The Magic Quantity: XOR

The whole game is decided by the XOR of all pile sizes. That single number tells you who is winning.

All lessons in this course

  1. Winning & Losing States in Games
  2. Nim and the Grundy Number
  3. Meet in the Middle
  4. Debug Fast: Stress Tests & Triage
← Back to Competitive Programming Academy