Competitive programming

Stage 2 · Lesson 6 of 6

Parity and invariants

Maths basics

An is a quantity that an allowed operation never changes. Parity, whether a number is odd or even, is the most common one. If an operation adds 2 to an element, each element's parity never changes; if it swaps two elements, the sum never changes.

When the starting position and the goal differ in an invariant, the answer is "impossible" and no is needed. Many problems rated 800 to 1200 look like long simulations and are solved by one such observation. Train yourself to ask, before coding, "what can this operation never change?" and to test your idea on small cases by hand.

Common trapsimulating operations step by step when a single invariant answers the question.

A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock

Resources for this stage

Start with

  • BookFree
    Competitive Programmer's Handbook

    Antti Laaksonen · Beginner

    Section 1.5 covers the maths basics and Chapter 21 covers number theory, from primes to modular inverses

Practise

  • The Mathematics section: exponentiation, counting divisors and binomial coefficients

  • Filter the problem set by the number theory and math tags, near your rating

Keep handy

Also worth your time

Mark this stage as not done?