Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Math & Number Theory/Math / Number Theory

Technique

Combinatorial Game Theory (minimax, Nim, Sprague-Grundy)

Classify positions as winning/losing by DP over states, and combine independent games with Grundy numbers XORed together.

Learn first1D / Fibonacci-styleBit Tricks & Bitmasking
0%
mastery
0/10 solved
Practise nextStone Game
IdeaPracticePitfalls

Recognise it

The signal

two players alternate optimally, who wins

  1. 1Brute force

    Full game-tree search.

  2. 2The observation

    Exponential game tree: positions repeat and independent games combine by XOR of Grundy values.

  3. 3Why it works

    A position is losing iff every move leads to a winning position; grundy = mex of successors.

Reach for it when

  • +piles / independent subgames
  • +take from ends of an array

Not when

  • ×games with scores rather than win/lose (minimax interval DP)
  • ×non-impartial games (Sprague-Grundy does not apply)

Practice

Next

Stone Game

0/10 solved
  • Stone GameMedium
  • Can I WinMedium
  • Stick GameCSESMedium
  • Nim Game ICSESMedium
  • Stair GameCSESMedium
  • Mex Grid QueriesCSESMedium
  • Removal GameCSESMedium
  • Number GameCodeforces1400
  • Another GameCSESHard
  • Grundy's GameCSESHard

Pitfalls

Misconceptions

  • !assuming the first player always wins in symmetric-looking games

Looks like it, isn't

  • Cat and Mouse needs Games on Graphs (retrograde analysis)
    Why
    Positions repeat and draws exist, so memo DFS has no valid order. The editorial percolates results back from resolved states. source ↗
  • Chalkboard XOR Game needs Invariants & Parity Arguments
    Why
    The editorial's argument: Alice wins iff the XOR is 0 or n is even. No game search. source ↗
Go deepervariants and techniques · 2 combinations · resources

Variants and techniques

  • Games on Graphs (retrograde analysis)Interview · hardWhen a game's state graph has cycles, memoised DFS can loop forever. Start from the known terminal states and propagate backwards instead. A state is WIN if some move reaches a LOSE state, and LOSE once every move is known to reach a WIN state (count the remaining moves, like Kahn in-degrees). Whatever is left unresolved is a DRAW.

Combinations

  • Games on Graphs (retrograde analysis)+Topological Sort+BFS/Dijkstra on an Augmented State Graph

    chase game on a graph or grid with possible draws

    Games on Graphs (retrograde analysis):
    labels propagate back from terminal states
    Topological Sort:
    Kahn-style degree counters decide LOSE once every move is known to lose
    BFS/Dijkstra on an Augmented State Graph:
    states are (mouse, cat, turn)

    Cat and MouseCat and Mouse II

  • Combinatorial Game Theory (minimax, Nim, Sprague-Grundy)+Bitmask DP

    two players pick from a small pool (n <= 20) without replacement

    Combinatorial Game Theory (minimax, Nim, Sprague-Grundy):
    a state is winning if some move leads to a losing state
    Bitmask DP:
    the set of used numbers is the state

    Can I Win

Resources

  • Binary exponentiationcp-algorithms · article
  • Sieve of Eratosthenescp-algorithms · article
  • Modular inversecp-algorithms · article
  • DivisibilityUSACO Guide · course
  • CombinatoricsUSACO Guide · course
  • Math cheatsheetTech Interview Handbook · article
TodayReviewPatternsProblems