Constraint → technique

The constraints are the strongest hint the setter gives. Read them first.

ConstraintConsider
brute force, all permutations, backtracking
bitmask DP
meet in the middle
, or flow
Floyd-Warshall, interval DP, matrix ops
DP
Mo’s, sqrt decomposition
— sorting, segment tree, Dijkstra
or with a small constant
, careful I/O
math, binary exponentiation, matrix power, digit DP
values sieve, frequency array, smallest prime factor
values Miller-Rabin, Pollard’s rho
two small parameters ( big, )FPT — exponential in
Sum of over all tests the per-test complexity may depend on that

Phrase → tool

The statement saysLikely tool
”minimise the maximum” / “maximise the minimum”binary search the answer
”number of subarrays with…“prefix sums + hash map, or two pointers
”range update / range query”difference array, BIT, lazy segtree
”connect / merge components”DSU
”shortest path, unweighted”BFS
”weights are 0 or 1”0-1 BFS
”non-negative weights”Dijkstra
”negative weights”Bellman-Ford
”dependencies / prerequisites”topological sort + DAG DP
”tree path queries”LCA, HLD
”subtree queries”Euler tour + BIT
”count paths in a tree”centroid decomposition
”palindromic substrings”Manacher, Eertree, hashing
”many patterns at once”Aho-Corasick
”distinct substrings / substring order”suffix automaton, suffix array
”choose from , mod a prime”factorials + inverse factorials
huge, prime small”Lucas
”system of congruences”CRT
”maximum XOR of a subset”linear basis
”maximum XOR with an element”binary trie
”convolution / count pairs summing to”FFT / NTT
”two choices, pairwise conflicts”2-SAT
”assign A to B optimally”matching, Hungarian, MCMF
”select items, some require others”project selection / min cut
”both play optimally”game theory, Grundy
”expected value”linearity of expectation
”count with digit constraints”digit DP
”queries known in advance”go offline

The diagnostic questions

  1. What is ? It picks the complexity class.
  2. What is being asked — a count, a maximum, an existence, a construction?
  3. Is there structure? A tree, a DAG, bipartite, an interval, planar, a grid.
  4. Are the queries offline? If so, sorting them is free.
  5. Is the objective minimax or maximin? → binary search.
  6. Is it NP-hard? → check the constraints for the intended exponential.
  7. Does it decompose into independent parts? → solve separately and combine.
  8. What would brute force be? Then look for what to speed up.

Reformulations worth trying

OriginalTry instead
Choosing a subseta flow / cut, or a DP over a sorted order
A hard constraintbinary search on it, or Lagrangian relaxation (Aliens trick)
Maximise a ratiobinary search , test
Count configurationscount the contribution of each element instead
A condition on all pairssort, then it becomes a condition on neighbours
A gridan implicit graph
A sequence of operationsa graph on states
”At least one of each”inclusion-exclusion
Reachability with cyclescondense the SCCs
Something about all subarrayscontribution of each element as min/max (monotonic stack)

When stuck

  1. Solve by hand or brute force; look at the answers.
  2. Try the reverse: process from the end, or think about what the answer cannot be.
  3. Look for an invariant or a parity obstruction.
  4. Consider the complement, or the dual.
  5. Ask what a slightly simpler version would need — then add the missing piece.
  6. Re-read the statement. The unused constraint is usually the key.

See also: Complexity Cheatsheet · CP Workflow · Graph Modelling Patterns