Overflow
| Bug | Fix |
|---|
int a, b; long long c = a * b; | 1LL * a * b — the multiplication happens in int |
mid = (lo + hi) / 2 near INT_MAX | lo + (hi - lo) / 2 |
i * i <= n for large n | i <= n / i |
| Graph distances | almost always need long long |
| Prefix sums of 105 values up to 109 | reaches 1014 — long long |
(a * b) % m with a,b≈1018 | (__int128)a * b % m |
abs() on a long long | use llabs or std::abs from <cmath> with the right overload |
| Cross products with 109 coordinates | reaches 4⋅1018 — near the long long limit |
| Factorials | overflow at 21! |
The rule: if a product of two inputs can exceed 2⋅109, write 1LL *.
Modular arithmetic
x = ((a - b) % MOD + MOD) % MOD; // subtraction can go negative
x = (long long)a * b % MOD; // cast BEFORE multiplying
| Bug | Note |
|---|
| Negative result after subtraction | add MOD before the final % |
Forgetting % after an addition in a loop | accumulates to overflow |
| Dividing under a modulus | needs a modular inverse |
powmod(x, MOD-2) with a composite modulus | Fermat does not apply |
| Reducing an exponent mod m | should be mod φ(m) |
00 in powmod | define it as 1 explicitly |
Arrays and indices
| Bug | Symptom |
|---|
| 0-based vs 1-based confusion | off-by-one, or a segfault |
| Not clearing globals between test cases | first test right, rest wrong |
Array too small by one (n vs n+1) | corruption, hard to trace |
| Reading past the end in a loop condition | UB, sometimes silent |
vector reallocation invalidating iterators/pointers | UB |
| Recursion depth >2⋅105 | 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
| Bug | Fix |
|---|
a == b on doubles | compare with an epsilon |
| ε too small for the magnitude | scale ε relative to the values |
while (hi - lo > eps) with huge values | use a fixed iteration count |
sqrt of a tiny negative from cancellation | sqrt(max(0.0, x)) |
acos of a value slightly outside [−1,1] | clamp, or use atan2(cross, dot) |
| Using doubles where integers would do | compare squared distances instead |
Printing -0.000000 | add 0.0, or clamp near-zero |
(int)sqrt(n) off by one | adjust with a while loop |
See Floating Point.
Undefined behaviour
| Pattern | Why |
|---|
1 << 31 on int | shift into the sign bit |
1 << 63 | write 1LL << 63 |
| Shifting by ≥ the type’s width | UB, not zero |
__builtin_clz(0) / __builtin_ctz(0) | undefined |
| Signed integer overflow | UB — the compiler may assume it never happens |
| Reading an uninitialised variable | UB |
| A comparator that is not a strict weak ordering | sort can segfault |
| Modifying a container while iterating it | invalidation |
The comparator one is worth emphasising: sort with return a <= b (non-strict) crashes on some inputs and works on others.
Algorithm-specific traps
ios::sync_with_stdio(false);
cin.tie(nullptr);
| Bug | Note |
|---|
Slow cin without the above | 10× slower on large input |
Mixing scanf and cin after sync_with_stdio(false) | undefined interleaving |
endl in a loop | flushes every time; use "\n" |
| Not reading all of the input | some judges report a wrong answer |
| Trailing whitespace / missing newline | usually fine, occasionally not |
Reading a long long with %d | garbage |
The pre-submit checklist
- Test n=0, n=1, all-equal, all-negative, the maximum constraint.
- Are globals cleared between test cases?
- Any
int that should be long long?
- Is the modulus applied after every operation?
- Does recursion go deeper than 105?
- Is the comparator a strict weak ordering?
- Did you print the answer in the required format and precision?
See also: Debugging and Stress Testing · Contest Checklist · Floating Point