Stage 1 · Lesson 7 of 8
Integer overflow and choosing types
An int holds values up to about 2.1 * 10^9, and a long long up to about 9.2 * 10^18. Sums of many large values, and products of two values around 10^5, already exceed int. does not give an error: the value silently wraps around and your answer is wrong.
Multiplying two int variables overflows before the result is stored, even if you store it in a long long, so write 1LL * a * b. Doubles (floating point numbers) cannot hold every large integer exactly, so avoid them for exact answers. A common habit is to use long long for every sum and answer.
Common trapthinking the answer fits in int because the input does.
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· follow one of these
- CourseFreeLearnCpp.com
LearnCpp.com · Beginner
Free and thorough: read the chapters on vectors, references, lambdas and the standard library if C++ is new to you
- BookFreeCompetitive Programmer's Handbook
Antti Laaksonen · Beginner
Chapter 1 covers C++ for contests, input and output and working with numbers; Chapter 2 covers complexity
Practise
Keep handy
Also worth your time
A clear walk through time and space complexity, the language you need to read constraints
- USACO GuideGo deeper
The General section has practical pages on choosing a language, fast input and output and debugging