0% found this document useful (0 votes)
20 views13 pages

Lecture 1 - Mathematics

The document discusses various mathematical concepts and algorithms related to prime numbers, including methods for identifying primes and their properties. It also presents problems involving number theory, such as calculating coprime integers, navigating mazes, and optimizing workout routines. Additional resources for further study on these topics are provided at the end.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views13 pages

Lecture 1 - Mathematics

The document discusses various mathematical concepts and algorithms related to prime numbers, including methods for identifying primes and their properties. It also presents problems involving number theory, such as calculating coprime integers, navigating mazes, and optimizing workout routines. Additional resources for further study on these topics are provided at the end.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

ALGOMANIACS

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.

By slightly modifying this code,


we can also get all the primes up
till n and the smallest prime
factor of each number in the
same time complexity.
Deepak’s Dilemma
Deepak is afraid of prime numbers, but they are not afraid of all prime numbers. They were
afraid of only a special kind of prime numbers. He is afraid of the prime numbers (without the
digit zero, they love all the primes which have digits 0 in them) that remain prime no matter
how many of the leading digits are omitted. For example, he is afraid of 4632647 because it
doesn't have the digit 0 and each of its truncations (632647, 32647, 2647, 647, 47, and 7) are
primes.

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)

Fermat’s Little Theorem - ap-1 = 1 (mod p)

Therefore a-1 = ap-2 (mod p)


Prime pair connection
Consider the consecutive primes p1 = 19 and p2 = 23. It can be verified that 1219 is the smallest
number such that the last digits are formed by p1 whilst also being divisible by p2.

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.

Given L and R, find ΣS for every pair of consecutive primes with L ≤ p1 ≤ R.

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)

The number of primes < n is approximately n/log(n)

Euler Totient Function : The number of numbers < n coprime to n

See also - Extended euclidean algorithm


Tanmay and Caltech
Tanmay wants to go to Caltech, but in the entrance exam he came across a very difficult task:
Given an integer n, it is required to calculate ∑lcm(c,gcd(a,b)), for all triples of positive integers
(a,b,c), where a+b+c=n.

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:Each workout must have an integer number of minutes

1 ≤ n,m,k < 105


Trippy Adventure
Shreyans has just returned from an exciting "adventure" and now finds himself at the entrance of a
peculiar n×n maze at position (0,0)(top left corner). The maze is divided into two regions, with lava
covering the upper-right half (i.e., all positions (i, j) where j > i). Shreyans must navigate through the
maze without stepping into the lava taking only down and right steps. In how many different ways
can he successfully cross the maze?

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/

PNC other important topics- https://usaco.guide/gold/combo?lang=cpp#binomial-coefficients


https://cp-algorithms.com/combinatorics/burnside.html

Pseudo-Random Based Prime Fact. in O(n1/4)-https://cp-algorithms.com/algebra/


factorization.html#pollards-rho-algorithm

Info/Questions on Inclusion/Exclusion - https://codeforces.com/blog/entry/64625

Newton’s method tc -
https://en.citizendium.org/wiki/Newton%27s_method#Computational_complexity

Maths other imp stuff - Probability, Matrices, FFT, Geometry(Very Rare)


Thank you

You might also like