Sunteți pe pagina 1din 27

Welcome to CP101-s1

KIIT Codechef Campus Chapter


Why invest time and effort in CP ?
● Learn DS / algos / problem solving skills (good for placement)
● Might help in optimizing software in future
● Represent university in different contests
● Have fun (and not regret that you have wasted time)
Contests to prepare for
● ACM ICPC ( qualifiers sept-oct, onsite nov-dec )
● Google codejam ( qualifiers apr-may, changes every year )
● FB Hacker cup
● Google APAC ( 4th year only )
● IEEEXtreme
● CodeChef SnackDown
● TopCoder Open
● Various institute contests around the year ( IIT Techfest etc )
Practice websites (in no particular order)
● CodeChef
● CodeForces
● TopCoder
● HackerEarth
● HackerRank
Where to find good tutorials ?
● GFG, StackOverflow, MathExchange
● Codechef forums
● TopCoder editorials
● Personal blogs of CP champs
● Youtube
● KIIT Codechef Campus Chapter
Steps to solve a CP problem
● Read, understand, think
● Visualize the problem
● Come up with a solution
● Prove / test whether solution is correct
● Analyze time complexity, calculate whether solution will pass constraints
● If no, either try to optimize solution or think of entirely new approach
● If yes, implement the solution
Time complexity analysis
● Big-O notation
Language-specific optimization tricks
C++ :

Disable sync with stdio in cin and cout

JAVA :

Use BufferedReader in place of Scanner for input, and PrintWriter for output

Find more by googling “Fast I/O for x language”


General advice
● Learn as many algorithms as possible to learn new ways to think about
problems
● Read editorials to problems
● Use your programming language to its full potential
● Write debuggable code
Fast Modulo Exponentiation
Why is modulo necessary ?
To prevent overflow
Naive method ( Linear Time )
int modExpo (int x, int y, int m) {

long long result = 1;

for (int i=0; i < y; i++) {

result = (result * x) % m;

return result;

}
Optimized method ( O(log n) )
Recursive implementation
long long modExpo (int x, int y, int m) {

if (y == 0)

return 1;

else if (y%2 == 0)

return modExpo (x*x, y/2, m) % m;

else

return ((x%m) * modExpo (x*x, (y-1)/2, m)) % m;

}
Iterative implementation
long long modExpo (int x, int y, int m) {
if (y == 0)
return 1;
x = x % m;
long long res = 1;
while (y > 0) {
if (y%2==1)
res = (res * x) % m;
x = (x * x) % m;
y = y/2;
}
return res;
Fibonacci number using fast exponentiation
(Donald E. Knuth)
Sieve of eratosthenes
Features of SOE
● Ancient algorithm for finding all prime numbers up to any
given limit.
● Also used for finding prime numbers in a given range
● Time complexity : O(n log logn)
● Space complexity : O(n)
Algorithm
● Create a list of consecutive integers from 2 through n: (2, 3, 4, ..., n).
● Initially, let p equal 2, the smallest prime number.
● Count to n from 2p in increments of p, and mark them as non-primes in the list
(these will be 2p, 3p, 4p, ...; the p itself should not be marked).
● Find the first number greater than p in the list that is not marked. If there was
no such number, stop. Otherwise, let p now equal this new number (which is
the next prime), and repeat from step 3.
● When the algorithm terminates, the numbers remaining not marked in the list
are all the primes below n.
Segmented sieve
The drawback with sieve of Erastothenes is not the number of
operations it performs but rather its memory requirements.

For numbers/limits greater than 10^6 the Sieve of


Erastothenes doesn’t work due to array size limitations.

To solve this problem we use Segmented Sieve.


Last but not the least
● Ask if anyone has any doubt
● Ask for suggestions
● Suggest team formation for ICPC and other contests
● (Shamelessly) Ask everyone to like our FB page
Thank you for attending CP101-s1
White page for board work

S-ar putea să vă placă și