Constraint → technique
The constraints are the strongest hint the setter gives. Read them first.
| Constraint | Consider |
|---|---|
| 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 says | Likely 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
- What is ? It picks the complexity class.
- What is being asked — a count, a maximum, an existence, a construction?
- Is there structure? A tree, a DAG, bipartite, an interval, planar, a grid.
- Are the queries offline? If so, sorting them is free.
- Is the objective minimax or maximin? → binary search.
- Is it NP-hard? → check the constraints for the intended exponential.
- Does it decompose into independent parts? → solve separately and combine.
- What would brute force be? Then look for what to speed up.
Reformulations worth trying
| Original | Try instead |
|---|---|
| Choosing a subset | a flow / cut, or a DP over a sorted order |
| A hard constraint | binary search on it, or Lagrangian relaxation (Aliens trick) |
| Maximise a ratio | binary search , test |
| Count configurations | count the contribution of each element instead |
| A condition on all pairs | sort, then it becomes a condition on neighbours |
| A grid | an implicit graph |
| A sequence of operations | a graph on states |
| ”At least one of each” | inclusion-exclusion |
| Reachability with cycles | condense the SCCs |
| Something about all subarrays | contribution of each element as min/max (monotonic stack) |
When stuck
- Solve by hand or brute force; look at the answers.
- Try the reverse: process from the end, or think about what the answer cannot be.
- Look for an invariant or a parity obstruction.
- Consider the complement, or the dual.
- Ask what a slightly simpler version would need — then add the missing piece.
- Re-read the statement. The unused constraint is usually the key.
See also: Complexity Cheatsheet · CP Workflow · Graph Modelling Patterns