Lecture 1 - Mathematics
Lecture 1 - Mathematics
MATHS
Prime Numbers
Using brute force, we can check if a number is prime in O(√n) time. Using an algorithm called Sieve
of Eratosthenes, we can cut that down to constant time with a O(nlogn) preprocessing step.
You are given a simple task, given a number of N, find out the number of primes not greater
that N, that Deepak is afraid of. There are T testcases.
T ≤ 105
1 ≤ N < 106
Exponentiation and
ModInv
ab= ab/2 * ab/2 * ab%2
This recursive formula means that
we can calculate ab in O(log2n)
In fact, with the exception of 3 and 5, for every pair of consecutive primes p1, p2, there exist values of
n for which the last digits are formed by p1 and is divisible by p2. Let S be the smallest of these
values of n.
T ≤ 10
5 ≤ L ≤ R < 109
|R-L| ≤ 106
Number Theory:
Important facts
gcd(a,b) = gcd(a-b,b) (provided a-b > 0)
lcm(a,b) = a*b/gcd(a,b)
3≤n≤105
Too many primes?
Given N,L,R, you need to compute the number of integers x in the interval [L,R] such that x is coprime
with N.
There are T testcases.
T ≤ 100
N ≤ 109
1 ≤ L ≤ R < 1015
Gainz
Shobhit has k minutes to spend on his workout routine. He can choose from n different exercises,
but if he spends more than m minutes on any single exercise, he risks injury. Fortunately, he can
also choose to skip some exercises if needed.
Shobhit wants to use up all k minutes without overdoing any single exercise. In how many ways can
he plan his workout?
Note: Position (i,j) is the ith row from the top and jth row from the left.
1 ≤ n< 106
Apple
Given a number n, find largest x such that x2 ≤ n
n<10100000
Assume you can store the number in O(n) space and do basic operations (+,-,*,/) in O(log(n)) time
and other operations accordingly.(using python or java or custom c++ class)
Additional Resources
Number theory other important topics -
https://cp-algorithms.com/algebra/extended-euclid-algorithm.html
https://cp-algorithms.com/algebra/linear-diophantine-equation.html
https://cp-algorithms.com/algebra/fibonacci-numbers.html
https://cp-algorithms.com/algebra/chinese-remainder-theorem.html
https://forthright48.com/category/cpps/number-theory/
Newton’s method tc -
https://en.citizendium.org/wiki/Newton%27s_method#Computational_complexity