Overflow

BugFix
int a, b; long long c = a * b;1LL * a * b — the multiplication happens in int
mid = (lo + hi) / 2 near INT_MAXlo + (hi - lo) / 2
i * i <= n for large ni <= n / i
Graph distancesalmost always need long long
Prefix sums of values up to reaches long long
(a * b) % m with (__int128)a * b % m
abs() on a long longuse llabs or std::abs from <cmath> with the right overload
Cross products with coordinatesreaches — near the long long limit
Factorialsoverflow at

The rule: if a product of two inputs can exceed , write 1LL *.

Modular arithmetic

x = ((a - b) % MOD + MOD) % MOD;             // subtraction can go negative
x = (long long)a * b % MOD;                  // cast BEFORE multiplying
BugNote
Negative result after subtractionadd MOD before the final %
Forgetting % after an addition in a loopaccumulates to overflow
Dividing under a modulusneeds a modular inverse
powmod(x, MOD-2) with a composite modulusFermat does not apply
Reducing an exponent mod should be mod
in powmoddefine it as 1 explicitly

Arrays and indices

BugSymptom
0-based vs 1-based confusionoff-by-one, or a segfault
Not clearing globals between test casesfirst test right, rest wrong
Array too small by one (n vs n+1)corruption, hard to trace
Reading past the end in a loop conditionUB, sometimes silent
vector reallocation invalidating iterators/pointersUB
Recursion depth stack overflow

Clearing globals is the most common multi-test bug. Either reset exactly the range you used, or use the version-stamp trick to avoid clearing at all.

Floating point

BugFix
a == b on doublescompare with an epsilon
too small for the magnitudescale relative to the values
while (hi - lo > eps) with huge valuesuse a fixed iteration count
sqrt of a tiny negative from cancellationsqrt(max(0.0, x))
acos of a value slightly outside clamp, or use atan2(cross, dot)
Using doubles where integers would docompare squared distances instead
Printing -0.000000add 0.0, or clamp near-zero
(int)sqrt(n) off by oneadjust with a while loop

See Floating Point.

Undefined behaviour

PatternWhy
1 << 31 on intshift into the sign bit
1 << 63write 1LL << 63
Shifting by the type’s widthUB, not zero
__builtin_clz(0) / __builtin_ctz(0)undefined
Signed integer overflowUB — the compiler may assume it never happens
Reading an uninitialised variableUB
A comparator that is not a strict weak orderingsort can segfault
Modifying a container while iterating itinvalidation

The comparator one is worth emphasising: sort with return a <= b (non-strict) crashes on some inputs and works on others.

Algorithm-specific traps

AlgorithmTrap
Dijkstrasilently wrong with negative edges
priority_queuethe comparator is inverted (greater gives a min-heap)
Bridgesskip the edge, not the parent vertex (parallel edges)
0/1 knapsackthe weight loop must go downward
Coin changeloop order decides combinations vs permutations
Cycle detection, directedneed three colours, not just “visited”
multiset::erase(value)\removes all copies
std::lower_bound on a set — use the member function
Hashingfixed base/modulus is hackable
Rollback DSUpath compression breaks it
Point in polygonthe -range must be half-open
Mo’s algorithmpointer-move order matters (expand before shrink)
SPFAhackable to

Input/output

ios::sync_with_stdio(false);
cin.tie(nullptr);
BugNote
Slow cin without the above10× slower on large input
Mixing scanf and cin after sync_with_stdio(false)undefined interleaving
endl in a loopflushes every time; use "\n"
Not reading all of the inputsome judges report a wrong answer
Trailing whitespace / missing newlineusually fine, occasionally not
Reading a long long with %dgarbage

The pre-submit checklist

  1. Test , , all-equal, all-negative, the maximum constraint.
  2. Are globals cleared between test cases?
  3. Any int that should be long long?
  4. Is the modulus applied after every operation?
  5. Does recursion go deeper than ?
  6. Is the comparator a strict weak ordering?
  7. Did you print the answer in the required format and precision?

See also: Debugging and Stress Testing · Contest Checklist · Floating Point