LearnToCP
Sign in
Navigation
HomeRoadmapProblemsAbout Us
Theory
Contest Knowledge
Selecting an IDEInteractive TasksOutput-Only Tasks
Basics
Your First ProgramData types and IOC++ syntaxModuloFunctionsVectorsMatricesTime Complexity
Sorting
SortingCounting sortRadix Sort
Optimization Techniques
Two PointersSum of numbers 1 to nPrefix sumBinary SearchGreedyBinary Search FunctionsBinary Search by AnswerDivide and Conquer
Binary Numbers
Binary NumbersNumbers in codeBitwise OperationsBitmasks
Math
Binary ExponentiationPrime NumbersPrime FactorizationGCD and LCMSieve of EratosthenesModified Sieve
Data Structures
StringsStackQueueMapsSetsPriority QueueCustom Criteria for FunctionsSegment TreesFenwick TreesSparse TablesUnion FindSqrt Decomposition
Combinatorics
Addition PrincipleMultiplication PrincipleCombinatoric ObjectsInclusion Exclusion Principle
Geometry
Geometry BasicsCross and Dot ProductLinesPolygonsPoints and PolygonsConvex Hull
Recursion
PointersRecursionGenerating Combinatoric Objects
Dynamic Programming
About DPDP problemsTree DPBitmask DP
Graph Theory
GraphsDFS and BFSShortest PathsTreesTopological SortingDijkstra's AlgorithmMinimum Spanning TreesShortest Path Algorithms
Advanced Graph Theory
BiconnectivityStrongly Connected ComponentsBipartite GraphGraph FlowAugmenting PathsFlow - Minimum Cut DualityHeavy-Light DecompositionCentroid Decomposition
Advanced Data Structures
2D and 3D Segment TreesLazy PropagationImplicit Segment TreesPersistent Segment TreesLowest Common AncestorTrieBalanced Binary Search TreesMo's Algorithm

Addition Principle

In this set of lessons about combinatorics, we will learn how to think about problems that involve counting, arranging or combining.

This lesson will be focusing on the addition principle.

It states:

If one task can be done in m ways and a second task can be done in n ways, and the two tasks cannot be done at the same time, then there are m + n ways to choose one of the tasks

Example:

A person has decided to shop at one store today, either in the north part of town or the south part of town. If they visit the north part of town, they will shop at either a mall, a furniture store, or a jewelry store (3 ways). If they visit the south part of town then they will shop at either a clothing store or a shoe store (2 ways). How many different shops can they end up at?

There are 3+2=5 possible shops the person could end up shopping at today.

Explanation

This principle might seem trivial, or useless to know. But it is a really important foundation and something to think about when solving problems.

In the context of competitive programming, think about it like this:

We are looking for the number of solutions to a problem. We divide the problem into non-overlapping subcases, count the solutions for each case separately, and finally add these numbers together. (X1 + X2 + X3 + ...)

The sum symbol

This is also a good time to introduce the sum symbol

In math when we have

x1​+x2​+x3​+x4​+...+xn​

We can write it down as:

i=1∑n​xi​

These two notations mean the identical thing, one is just shorter to write.

x1​+x2​+x3​+x4​+...+xn​=i=1∑n​xi​

If its easier, you can also imagine it as a for loop:

long long sum = 0;
for (int i = 1; i <= n; i++) {
    sum += x[i];
}