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 · CP 2000

Gaussian Elimination (mod p / reals) & Matrix-Tree Theorem

Solve n linear equations, or compute a rank or determinant, in O(n3) by row reduction. Kirchhoff's theorem counts spanning trees as any cofactor of the Laplacian. Expected-value systems on cyclic state graphs are also solved this way.

Learn firstMath / Number TheoryBinary Exponentiation (fast power)
0%
mastery
0/3 solved
Practise nextBroken robot
IdeaPracticePitfalls

Recognise it

The signal

system of linear equations modulo a prime

  1. 1Brute force

    Cramer's rule / permutation expansion: O(n!).

  2. 2The observation

    Mutual dependencies between unknowns rule out a DP order.

  3. 3Why it works

    Row operations preserve the solution set. The determinant flips sign on a row swap and scales with row scaling.

Reach for it when

  • +count spanning trees of a graph with n <= 500
  • +expected steps on a graph with cycles (states depend on each other)
  • +determinant of a matrix

Not when

  • ×equations over GF(2) (xor-basis / bitset-opt)
  • ×the state graph is a DAG (plain DP)

Practice

Next

Broken robot

0/3 solved
  • Broken robotCodeforces2400
  • System of Linear EquationsCSESHard
  • Stranger TreesCodeforces2600

Pitfalls

Misconceptions

  • !pivoting on zero (pick a non-zero pivot; use partial pivoting with reals)
  • !forgetting to drop one row and column of the Laplacian
Go deeper2 combinations · resources

Combinations

  • Probability & Expected-Value DP (linearity of expectation)+Gaussian Elimination (mod p / reals) & Matrix-Tree Theorem

    expected steps of a random walk with self-loops or sideways moves

    Probability & Expected-Value DP (linearity of expectation):
    E[state] = 1 + sum of p * E[next]
    Gaussian Elimination (mod p / reals) & Matrix-Tree Theorem:
    solve the system when states reference each other

    Broken robot

  • Gaussian Elimination (mod p / reals) & Matrix-Tree Theorem+Lagrange Interpolation

    count spanning trees sharing exactly k edges with a given tree, n <= 100

    Gaussian Elimination (mod p / reals) & Matrix-Tree Theorem:
    matrix-tree determinants for spanning trees with weighted edges
    Lagrange Interpolation:
    recover the polynomial in x from n evaluations

    Stranger Trees

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