Headstart Week 2
Headstart Week 2
Note:
The articles mentioned contain theoretical concepts and should be
read by everyone. The codes might be given in C++, but we can
correspondingly develop the python code from the theory given
The articles from USACO Guide can also be accessed in both python
and C++ by changing the language using an option on the webpag
Python users can checkout our GitHub repo which contains the
code of some useful functions to be covered ahead.
SORTING
(Expected Time: 1-2 days)
order.
There are several sorting algorithms. Some basic sorting algorithms are
Insertion Sor
Articl
Video
2. Merge Sor
Articl
Video
While doing CP problems, we don’t write any sorting algorithm. C++ STL
SYNTAX - C++
Formally, sort( it1, it2) will sort elements in ascending order in the range
[it1, it2) . Please note that this is even true for array {arr is a pointer} .
element, we will have to use custom sort. We can write our own
SYNTAX - Python
Dictionary/Set : sorted(iterable,reverse=True/False)
Sorting a list of pairs can be done by the normal sort function itself. Let’s
say a list, a= [(4,5),(2,3),(2,1),(4,4)] then simply calling a.sort() will give
a = [(2,1),(2,3),(4,4),(4,5)] (sorting based on the first element of each tuple).
You can learn more about custom sort using lambda function.
Practice Problem
Smallest Pai
Incinerat
Stick Length
Divisibility by 2^n
Additional Question
A greedy algorithm, as the name suggests, always makes the choice that
seems to be the best at that moment. This means that it makes a locally-
optimal choice in the hope that this choice will lead to a globally-optimal
solution.
Resource
Watch this lecture on Algomaniacs
Practice Problem
Different Difference
Kathmand
Berland Musi
Closing the Ga
Paprika and Permutation
Additional Question
Problems by Algomaniacs
NUMBER THEORY (Expected Time: 2-3 days)
CALCULATING GCD
GCD of two numbers can be calculated using the Euclidean
Algorithm, i.e., gcd(a,b)=gcd(a, a-b).
This can be further optimized as we can reduce repeated subtractions.
Hence, gcd(a,b)=gcd(a, a%b)
Its time complexity is O(logn)
You can also use the inbuilt functions
C++
: __gcd(a,b) ( on some compiler __gcd(0, 0) gives exception so while
Resource
CPH, page 19
Primality test: algorith
*Extended Primality Tests
INTEGER FACTORIZATION
CPH, page 197-199 (Give it a quick read, it mostly contains
mathematics that you must have studied before like number of
factors of a number etc
Prime Factorization of a number can be done using trial division
method in O(sqrt(n)) time
However, again for multiple queries, it may not be optimal. So we can
precompute the smallest prime factor (spf) of every number from 1 to
n. This takes O(nloglogn) time
Suppose we have a number x, one of its prime factors will be spf(x).
And rest of the prime factors will be contained in x/spf(x). We can do
this recursively to find all prime factors in O(logn) time for each query
Precomputing SP
*Integer factorization
*Optional topics ! Study only if you have time .
MODULAR ARITHMETIC
An essential part of CP is working with remainders of integers with a
particular number, instead of working with integers themselves. This is
done to prevent integer overflows in built-in data types.
Note: In C++, -8%7=-1 and not 6. You have to add 7 at the end and take
mod again. In general, n mod m= (n%m+m)%m.
*ADDITIONAL READ
Essentials of Number Theory this will help in developing an intuitive
feeling for elementary number theory .
Practice Problems
Exponentiatio
Counting Divisor
Kill Demodog
Divisor Analysi
Exponentiation I
Divisible Numbers
Additional Question
Strange Functio
Half of Sam
X-Magic Pai
Taxes
TWO POINTERS (Expected Time: 1-2 days)
The name “Two Pointer” suggests the use of two different pointers, and it
is exactly that, but without actually using C++ pointers (Well, you can use,
but why would you want to?). It’s just a fancy name for a technique which
uses two variables in a single loop. The variables are generally used to
keep track of indices of an array or string, and generally in a sorted array.
Two-pointers-CF
Practice Problems
Diamond Collecto
Sum of Two Value
Sum of Three Value
Maximum Subarray Su
Balanced Team
Codeforce
Codeforces edu Two Pointers - Step 1
Additional Question
Codeforces edu Two Pointers - Step 2
COMBINATORICS (Expected Time: 0-1 days)
Remember your JEE days? Well guess what… the stuff you have learnt for
JEE doesn’t go for waste, it has its own importance in CP too!
CALCULATING
Naïve method
Optimal method
You must have also learnt an alternate formula for nCr which is
n!/(r!*(n-r)!). This can be used to find out the binomial coefficients too.
Also we know that factorials tend to be pretty large which can overflow
the integer size (and in python even though you can store integers of
very long size, it takes up a lot of time giving TLE), so we generally
calculate it modulo some number (which is generally prime). This can be
done in O(n+log(mod)) complexity by precomputing the factorials and
inverse factorials individually and then using them in the problem as
required. Code is available at USACO.
Practice Problem
Binomial Coefficient
Creating Strings I
Binary Strings are Fu
Close Tuples
Additional Question
USACO Problemset
While we see integers in decimal system, they are still stored in binary
form. We can apply operations like and, or, xor etc to each bit of a number
and use it for our benefit.
CPH (pg 96 to 99): This covers the essentials of how bit operations
work and how they can be useful. This is a must read
It is fine if you find the concepts difficult to grasp, the following video
can be helpful- Bitwise Operations tutorial #1 | XOR, Shift, Subset
Bitwise Operators in Python (Uses the same idea as the above, just
that the code will be in py).
Practice Problem
Petr and a Combination Loc
Absolute Maximizatio
NIT orz!
Additional Question
Even-Odd XO
Orra
Odd Topic