0% found this document useful (0 votes)
12 views18 pages

CP Session

The document discusses various techniques in competitive programming, focusing on modular arithmetic, binary exponentiation, prime checking, and bit manipulation. It includes definitions, operations, and time complexities for each topic, along with links to relevant problems and resources. Additionally, it provides problem-solving examples and encourages community engagement.

Uploaded by

Infancy Pio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views18 pages

CP Session

The document discusses various techniques in competitive programming, focusing on modular arithmetic, binary exponentiation, prime checking, and bit manipulation. It includes definitions, operations, and time complexities for each topic, along with links to relevant problems and resources. Additionally, it provides problem-solving examples and encourages community engagement.

Uploaded by

Infancy Pio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

ACM - CEG Presents

#Rapid CP
Unlocking Competitive
Programming Secrets
behind Speedy Solutions
Date : 28-03-2024
Modulo Arithmetics
Modular arithmetic is a system of arithmetic for
integers, where numbers “wrap around” upon reaching
a certain value, known as the modulus.

This becomes particularly crucial when handling large


numbers in competitive programming.
Operations

The implementation of modular arithmetic involves


various operations like,

● Modular Addition: (a + b) mod m = ((a mod m) +


(b mod m)) mod m.
● Modular Subtraction: (a + b) mod m = ((a mod
m) + (b mod m)) mod m.
● Modular Multiplication: (a * b) mod m = ((a mod
m) * (b mod m)) mod m.
Operations

● Modular Division: (a / b) mod m = (a * (inverse of


b if exists)) mod m. The modular inverse of a mod
m exists only if a and m are relatively prime i.e.,
gcd(a, m) = 1.
● Modular Exponentiation: (a ^ b) mod m = (a ^
b) mod m
Binary Exponentiation
Binary exponentiation (also known as exponentiation by
squaring) is a trick which allows to calculate a^n using only
log n multiplications instead of naive n multiplications

Let us consider 2 ^ 12

Naive approach , 2*2*2*2…….

Binary Exponentiation ,

2 ^ 12 = (2)^ 2 * 6 = (16) ^ 3

= (4) ^ 6 = 16 * (16) ^ 2

= (4) ^ 2 * 3 = 16 * (256) ^ 1
Binary Exponentiation
Methodology
So, the final time complexity is O(log n) :
To find a^b ,
Prime Check
Prime Number Definition :
Prime numbers are numbers that have only 2 factors: 1 and
themselves

Ex : 2, 5, 7 …

Naive Aproach : O(N)


Sqrt Appoach : O(sqrt(N))
Sieve of Eratosthenes

Motivation :
Given a number n, print all primes smaller
than or equal to n. It is also given that n is a small
number

Intuition : Precompution + Basic Maths

Time Complexity : O(N*Log(Log N))


Space Complexity : O(N)
Proof :
https://cp-algorithms.com/algebra/sieve-of-eratosthenes.html
Let's Solve Problem

Problem:
https://leetcode.com/problems/count-primes/

Intuition : Direct Implementation of Sieve


Bit Manipulation Basics

AND Operator (&)


OR Operator (|)
XOR Operator (^)
Not Operator (~)
Right Shift (>>)
Left Shift (<<)
Bit Manipulation Tricks

1.How to Print the Number in a Binary ?


2.Checking if i th Bit is set or not ?
3.Count the no of set bits in a Number ?
4. Dividing and Multiplying by 2
5. Odd or Even
6. IsPower of 2
Let’s Solve Problems
1.Swap two variables without using variable

a, b --> integers to be swapped


a = a^b; // Now a contains a^b
b = a^b; // Now b will have a
a = a^b; // Now a will have b
Let’s Solve Problems
2. Problem

https://leetcode.com/problems/missing-number/

Intuition : XOR Property


Subset Generation using Bit Mask

WHAT IS SUBSET ?
Subset vs SubArray

Bit Mask ?

Lets code …

Check it out :
https://www.geeksforgeeks.org/find-distinct-subsets-
given-set/
Links to ponder

● https://www.geeksforgeeks.org/modular-arithmetic-for-compe
titive-programming/
● https://cp-algorithms.com/algebra/binary-exp.html
● https://leetcode.com/problems/count-good-numbers/
● https://cp-algorithms.com/algebra/sieve-of-eratosthenes.html

● https://leetcode.com/problems/count-primes/
● https://leetcode.com/problems/four-divisors/
● https://www.geeksforgeeks.org/bits-manipulation-important-t
actics/
Join our Community

You might also like