Must do Math for Competitive Programming
Last Updated :
12 Jul, 2025
Competitive Programming (CP) doesn’t typically require one to know high-level calculus or some rocket science. But there are some concepts and tricks which are sufficient most of the time. You can definitely start competitive coding without any mathematical background, but maths becomes essential as you dive deep into the world of CP.
A majority of the Competitive Coding problems that you'll encounter will have some mathematical logic or trick. All the algorithms that we learn are derived from a mathematical point of view. Most of the time, maths helps us solve the question within the necessary time constraints.
All of the topics can’t be covered in a single article but we'll be looking into some of the most common mathematical concepts in competitive coding. Some of these concepts might look too difficult at first sight but applying them to problems will ease them for you.
1. BigInteger
For e. g. Calculating factorials of large numbers (lets say 100) or taking large numbers of input around 100000 digits in length. In c++, it is not possible to store these numbers even if we use long long int. One way to take this kind of number is, taking them into an array more wisely use a vector … each number will hold an index of the array, if the number is 12345 then 12345%10=5 will in index[4], and the number now=12345/10=1234. now 1234%10=4 will be in [3] and so on to 1%10=1 is in [0], or you can use string too, it is easier since the char array only allow 1 byte for each index so you don’t need that modulation operation to fit number into the index.
Java provides Biginteger class to handle this.
- LCM(a, b) * GCD(a, b) = a*b, calculating GCD is equivalent to calculating LCM.
Now, how do we calculate the GCD of two numbers?
We can of course find the factors of the two numbers and then determine the highest common factor. As the numbers get bigger though ( say 155566328819), factorization becomes ineffective.
This is where Euclid's algorithm comes to our rescue. This algorithm uses the easy-to-prove fact gcd(a, b)=gcd(b, r), where r is the remainder when a is divided by b, or just a%b.
C++
// Function to find the greatest common divisor (GCD) of two integers using Euclid's algorithm
static int GCD(int A, int B) {
// Base case: if B is 0, return A as GCD
if (B == 0)
return A;
else
// Recursive case: call GCD recursively with B and the remainder of A divided by B
return GCD(B, A % B);
}
//this code is contributed by Utkarsh
C
int GCD(int A, int B)
{
if (B == 0)
return A;
else
return GCD(B, A % B);
}
Java
static int GCD(int A, int B)
{
if (B == 0)
return A;
else
return GCD(B, A % B);
}
// This code is contributed by susmitamittal1329.
Python
def GCD(A, B):
if (B == 0):
return A
else:
return GCD(B, A % B)
# This code is contributed by subham348.
C#
static int GCD(int A, int B)
{
if (B == 0)
return A;
else
return GCD(B, A % B);
}
// This code is contributed by subham348.
JavaScript
function GCD(A, B)
{
if (B === 0)
return A;
else
return GCD(B, A % B);
}
// This code is contributed by subham348.
Can we find the numbers (x, y) such that ux + vy = gcd(u, v)?. There exists infinitely many pairs - this is Bezout's Lemma. The algorithm to generate such pairs is called Extended Euclidean Algorithm.
Generating primes fast is very important in some problems. Let's cut to the chase and introduce Eratosthenes's Sieve. You can use the Sieve of Eratosthenes to find all the prime numbers that are less than or equal to a given number N or to find out whether a number is a prime number.
The basic idea behind the Sieve of Eratosthenes is that at each iteration one prime number is picked up and all its multiples are eliminated. After the elimination process is complete, all the unmarked numbers that remain are prime.
Suppose we want to find all primes between 2 and 50. Iterate from 2 to 50. We start with 2. Since it is not checked, it is a prime number. Now check all numbers that are multiple of except 2. Now we move on, to number 3. It's not checked, so it is a prime number. Now check all numbers that are multiple of 3, except 3. Now move on to 4. We see that this is checked - this is a multiple of 2! So 4 is not a prime. We continue doing this.
C++
void sieve(int N)
{
bool isPrime[N + 1];
for (int i = 0; i& lt; = N; ++i) {
isPrime[i] = true;
}
isPrime[0] = false;
isPrime[1] = false;
for (int i = 2; i * i <= N; ++i) {
// Mark all the multiples of i as composite numbers
if (isPrime[i] == true) {
for (int j = i * i; j <= N; j += i)
isPrime[j] = false;
}
}
}
Java
// Java program for above approach
import java.util.*;
class GFG {
public static void sieve(int N)
{
boolean isPrime[] = new boolean[N + 1];
for (int i = 0; i <= N; ++i) {
isPrime[i] = true;
}
isPrime[0] = false;
isPrime[1] = false;
for (int i = 2; i * i <= N; ++i) {
// Mark all the multiples of i as composite
// numbers
if (isPrime[i] == true) {
for (int j = i * i; j <= N; j += i)
isPrime[j] = false;
}
}
}
}
Python
def sieve(N):
isPrime = [True for i in range(N + 1)]
isPrime[0] = False
isPrime[1] = False
for i in range(2, int(N ** 0.5) + 1):
# Mark all the multiples of i as composite numbers
if (isPrime[i] == True):
for j in range(i * i, N + 1, i):
isPrime[j] = False
# This code is contributed by phasing17.
C#
// C# program for above approach
using System;
using System.Collections.Generic;
class GFG {
public static void sieve(int N)
{
bool[] isPrime = new bool[N + 1];
for (int i = 0; i <= N; ++i) {
isPrime[i] = true;
}
isPrime[0] = false;
isPrime[1] = false;
for (int i = 2; i * i <= N; ++i) {
// Mark all the multiples of i as composite
// numbers
if (isPrime[i] == true) {
for (int j = i * i; j <= N; j += i)
isPrime[j] = false;
}
}
}
}
// This code is contributed by phasing17
JavaScript
function sieve( N)
{
let isPrime = new Array(N + 1);
for (var i = 0; i <= N; ++i) {
isPrime[i] = true;
}
isPrime[0] = false;
isPrime[1] = false;
for (var i = 2; i * i <= N; ++i) {
// Mark all the multiples of i as composite numbers
if (isPrime[i] == true) {
for (var j = i * i; j <= N; j += i)
isPrime[j] = false;
}
}
}
// This code is contributed by phasing17
What if the number is large (say 10^16), in that case we require segmented sieve.
The idea of segmented sieve is to divide the range [0..n-1] in different segments and compute primes in all segments one by one. This algorithm first uses Simple Sieve to find primes smaller than or equal to ?(n). Below are steps used in Segmented Sieve.
- Use Simple Sieve to find all primes up to square root of ‘n’ and store these primes in an array “prime[]”. Store the found primes in an array ‘prime[]’.
- We need all primes in range [0..n-1]. We divide this range in different segments such that size of every segment is at-most ?n
- Do following for every segment [low....high]
- Create an array mark[high-low+1]. Here we need only O(x) space where x is number of elements in given range
- Iterate through all primes found in step 1. For every prime, mark its multiples in given range [low..high].
In Simple Sieve, we needed O(n) space which may not be feasible for large n. Here we need O(?n) space and we process smaller ranges at a time
When one number is divided by another, the modulo operation finds the remainder. It is denoted by the % symbol.
Example
Assume that you have two numbers 10 and 3. 10%3 is 1 because when 10 is divided by 3, the remainder is 1.
Properties :
1. (a+b)%c = ((a%c)+(b%c))%c
2. (a*b)%c = ((a%c)*(b%c))%c
3. (a-b)%c = ((a%c)-(b%c)+c)%c
4. (a/b)%c = ((a%c)?(b%c))%c
When are these properties used?
Assume that a = 10^12, b = 10^12, and c = 10^9+7. You have to find (a*b)%c. When you multiply a with b, the answer is 10^24, which does not confirm with the standard integer data types. Therefore, to avoid this we used the properties. (a*b)%c = ((a%c)*(b%c))%c
Fast Modulo exponentiation
Calculate a^b in modular m in O(log b),
It uses binary expansion of b, and is very straightforward.
C++
ll expo(ll a, ll b, ll m)
{
if (b == 0)
return 1;
ll p = expo(a, b / 2, m) % m;
p = (p * p) % m;
return (b % 2 == 0) ? p : (a * p) % m;
}
Java
// Java program to implement the approach
import java.util.*;
class GFG
{
long expo(long a, long b, long m)
{
if (b == 0)
return 1;
long p = expo(a, b / 2, m) % m;
p = (p * p) % m;
return (b % 2 == 0) ? p : (a * p) % m;
}
}
// This code is contributed by phasing17
Python
def expo(a, b, m):
if b == 0:
return 1
p = expo(a, b // 2, m) % m
p = (p * p) % m
return [p, (a * p) % m][b % 2]
C#
// C# program to implement the approach
using System;
using System.Collections.Generic;
class GFG
{
long expo(long a, long b, long m)
{
if (b == 0)
return 1;
long p = expo(a, b / 2, m) % m;
p = (p * p) % m;
return (b % 2 == 0) ? p : (a * p) % m;
}
}
// This code is contributed by phasing17
JavaScript
// JavaScript implementation of the approach
function expo(a, b, m)
{
if (b == 0)
return 1;
let p = expo(a, Math.floor(b / 2), m) % m;
p = (p * p) % m;
return (b % 2 == 0) ? p : (a * p) % m;
}
// This code is contributed by phasing17
Now, let us talk about modular inverse.
By using Extended Euclidean Algorithm, we can get the inverse of a modulo m.
C++
// Returns modulo inverse of a with respect
// to m using extended Euclid Algorithm
// Assumption: a and m are coprimes, i.e.,
// gcd(a, m) = 1
int modInverse(int a, int m)
{
int m0 = m;
int y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1)
{
// q is quotient
int q = a / m;
int t = m;
// m is remainder now, process same as
// Euclid's algo
m = a % m, a = t;
t = y;
// Update y and x
y = x - q * y;
x = t;
}
// Make x positive
if (x < 0)
x += m0;
return x;
}
Java
import java.util.*;
class GFG {
// Returns modulo inverse of a with respect
// to m using extended Euclid Algorithm
// Assumption: a and m are coprimes, i.e.,
// gcd(a, m) = 1
int modInverse(int a, int m)
{
int m0 = m;
int y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1) {
// q is quotient
int q = a / m;
int t = m;
// m is remainder now, process same as
// Euclid's algo
m = a % m;
a = t;
t = y;
// Update y and x
y = x - q * y;
x = t;
}
// Make x positive
if (x < 0)
x += m0;
return x;
}
}
Python
# Python3 program to implement the approach
# This function returns modulo inverse of a with respect
# to m using extended Euclid Algorithm
# Assumption: a and m are coprimes, i.e.,
# gcd(a, m) = 1
def modInverse(a, m):
m0 = m;
y = 0
x = 1;
if (m == 1):
return 0;
while (a > 1) :
# q is quotient
q = int(a / m);
t = m;
# m is remainder now, process same as
# Euclid's algo
m = a % m
a = t;
t = y;
# Update y and x
y = x - q * y;
x = t;
# Make x positive
if (x < 0) :
x += m0;
return x;
# This code is contributed by phasing17
C#
using System;
using System.Collections.Generic;
class GFG {
// Returns modulo inverse of a with respect
// to m using extended Euclid Algorithm
// Assumption: a and m are coprimes, i.e.,
// gcd(a, m) = 1
int modInverse(int a, int m)
{
int m0 = m;
int y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1) {
// q is quotient
int q = a / m;
int t = m;
// m is remainder now, process same as
// Euclid's algo
m = a % m;
a = t;
t = y;
// Update y and x
y = x - q * y;
x = t;
}
// Make x positive
if (x < 0)
x += m0;
return x;
}
}
JavaScript
// JS program to implement the approach
// This function returns modulo inverse of a with respect
// to m using extended Euclid Algorithm
// Assumption: a and m are coprimes, i.e.,
// gcd(a, m) = 1
function modInverse(a, m)
{
let m0 = m;
let y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1)
{
// q is quotient
let q = Math.floor(a / m);
let t = m;
// m is remainder now, process same as
// Euclid's algo
m = a % m, a = t;
t = y;
// Update y and x
y = x - q * y;
x = t;
}
// Make x positive
if (x < 0)
x += m0;
return x;
}
// This code is contributed by phasing17
Fermat's Little Theorem gives a^(p-1)==a (mod p) if gcd(a, p)=1, where p is a prime. Therefore, we can calculate the modular inverse of a as a^(p-2), by fast exponentiation also.
We can calculate nCr in modulo p (p is a prime) very fast using Lucas' Theorem. Lucas theorem basically suggests that the value of nCr can be computed by multiplying results of n(i)Cr(i) where n(i) and r(i) are individual same-positioned digits in base p representations of n and r respectively. This is very efficient when p is small and n, r is huge. We can precalculate the factorials and inverse of factorials modulo p by using the above code.
Two numbers (positive integers) a and b are relatively prime (prime to each other), if they have no common prime factors. The numbers m1, m2, ....mr, are pair wise relatively prime if any two distinct numbers in that collection, are relatively prime. Chinese remainder theorem says that given any r pair wise relatively prime numbers m1, m2, ....mr, and any numbers b1, b2, b3, ....br, we can always find a number M which leaves the remainders b1, b2, b3, ..br when it is divided by m1, m2, ...mr respectively.
Let us solve x == r (mod mi), where mi are pairwise coprime.
(If they are not coprime, break them into prime powers, and if some are contradictory, there are no solutions.)
You just need to know some basics like :
- What is a series and does it converge to some value?
- Know about famous series like trigonometric, hyperbolic…etc.
- How to calculate the finite limit of famous series like ( geometric series, harmonic series)
and basically the same thing for the sequences, you just need to know the basics. (Trick: use OEIS site)
We sometimes land up in a situation when various coding problems can be simplified to a mathematical formula but often finding that formula isn’t that straightforward .Here comes, OEIS for rescue. We can calculate the terms for initial indices i.e n=0, 1, 2, 3, …….. and then may use OEIS to find the mathematical expression.
Catalan numbers are a sequence of natural numbers that helps to solve many counting problem. Terms starting with n=0 are : 1, 1, 2, 5, 14, 42, 132, 429, 1430 ….and so on.
Questions based on catalan number may appear in many coding competitions. So it is always a plus point to know in depth about catalan number.
Catalan numbers find extensive applications in forming closed solutions to combinatorics problems. Some of the examples are:
- The number of binary search trees that can be formed using 'n' nodes is the nth Catalan number.
- The number of ways that a convex polygon of n+2 sides, can be cut into 2 or more triangles by joining any 2 edges is the nth Catalan number.
- The closed solution to the number of possible parentheses matching given 'n' pairs is the nth Catalan number.
The pigeonhole principle is a powerful tool used in combinatorial maths. But the idea is simple and can be explained by the following peculiar problem. Imagine that 3 pigeons need to be placed into 2 pigeonholes. Can it be done? The answer is yes, but there is one catch. The catch is that no matter how the pigeons are placed, one of the pigeonholes must contain more than one pigeon.
The logic can be generalized for larger numbers.
The pigeonhole principle states that if more than n pigeons are placed into n pigeonholes, some pigeonhole must contain more than one pigeon. While the principle is evident, its implications are astounding.
For example consider this statement “If you pick five numbers from the integers 1 to 8, then two of them must add up to nine.”
Explanation: Every number can be paired with another to sum to nine. In all, there are four such pairs: the numbers 1 and 8, 2 and 7, 3 and 6, and lastly 4 and 5.Each of the five numbers belongs to one of those four pairs. By the pigeonhole principle, two of the numbers must be from the same pair–which by construction sums to 9.
Inclusion Exclusion principle is a very basic theorem of counting and many problems in various programming contests are based on it, a formal explanation of inclusion exclusion principle goes as follows:
Consider A as a collection of objects and |A| as the number of objects in A and similarly for B, then the cardinality of collection of objects of both sets A and B ( when both A and B are disjoint) can be stated as (for 2 finite sets) :
But what if the sets are not disjoint?
Then we need to subtract the common objects counted twice while calculating the cardinality of both A and B and new form will become:
- AUB| = |A| + |B| - |A ∩ B|
This is the most basic form of the inclusion-exclusion principle.
But what if there are more than 2 sets, let`s say n sets.
Then it can be stated as :
(Include=add, exclude=subtract)
|A1 U A2 U A3 …..U AN| = (Include count of each set, Exclude count of pairwise set, Include count of triplet sets, exclude count of quadruplet sets……till nth tuple is included( if odd) or excluded( if even))
i. e., |A1 U A2 U A3 …..U AN| = (|A1| + |A2| + |A3| + |A4| … + |AN|) - ( |A1 ∩ A2| + |A1 ∩ A3| + |A1 ∩ A4|.. + all combinations) + (|A1 ∩ A2 ∩ A3| … all combinations)………. and so on.
This list is not exhaustive but the concepts will be very useful in contests in codeforces, codechef etc.. So grab your pen, paper and laptop and start practicing.
Happy coding!
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem