Maths for Programming Contests

Slides:



Advertisements
Similar presentations
Chapter 8 Introduction to Number Theory. 2 Contents Prime Numbers Fermats and Eulers Theorems.
Advertisements

Diffie-Hellman Diffie-Hellman is a public key distribution scheme First public-key type scheme, proposed in 1976.
§ 1.10 Properties of the Real Number System. Angel, Elementary Algebra, 7ed 2 Commutative Property Commutative Property of Addition If a and b represent.
Slide 1 Insert your own content. Slide 2 Insert your own content.
Combining Like Terms. Only combine terms that are exactly the same!! Whats the same mean? –If numbers have a variable, then you can combine only ones.
0 - 0.
ALGEBRAIC EXPRESSIONS
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
MULTIPLYING MONOMIALS TIMES POLYNOMIALS (DISTRIBUTIVE PROPERTY)
ADDING INTEGERS 1. POS. + POS. = POS. 2. NEG. + NEG. = NEG. 3. POS. + NEG. OR NEG. + POS. SUBTRACT TAKE SIGN OF BIGGER ABSOLUTE VALUE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Teacher Name Class / Subject Date A:B: Write an answer here #1 Write your question Here C:D: Write an answer here.
Addition Facts
Modular Arithmetic Several important cryptosystems make use of modular arithmetic. This is when the answer to a calculation is always in the range 0 –
CS4026 Formal Models of Computation Running Haskell Programs – power.
BT Wholesale October Creating your own telephone network WHOLESALE CALLS LINE ASSOCIATED.
5.9 + = 10 a)3.6 b)4.1 c)5.3 Question 1: Good Answer!! Well Done!! = 10 Question 1:
User Defined Functions
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Copyright © Recursive GCD Demo public class.
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
Past Tense Probe. Past Tense Probe Past Tense Probe – Practice 1.
Properties of Exponents
Addition 1’s to 20.
Test B, 100 Subtraction Facts
Basic Math Terms.
Week 1.
Complexity Analysis (Part II)
Lecture 12 Recursion part 1
Cryptography and Network Security Chapter 8 Fourth Edition by William Stallings.
1 Lect. 12: Number Theory. Contents Prime and Relative Prime Numbers Modular Arithmetic Fermat’s and Euler’s Theorem Extended Euclid’s Algorithm.
RSA COSC 201 ST. MARY’S COLLEGE OF MARYLAND FALL 2012 RSA.
Chapter 8 – Introduction to Number Theory. Prime Numbers prime numbers only have divisors of 1 and self –they cannot be written as a product of other.
Chapter 8 Introduction To Number Theory. Prime Numbers Prime numbers only have divisors of 1 and Prime numbers only have divisors of 1 and self. self.
Chapter 8 Introduction to Number Theory. Prime Numbers prime numbers only have divisors of 1 and self –they cannot be written as a product of other numbers.
CSE 311 Foundations of Computing I Lecture 13 Number Theory Autumn 2012 CSE
Chapter Primes and Greatest Common Divisors ‒Primes ‒Greatest common divisors and least common multiples 1.
ACM Workshop Number Theory.
Number Theory(L5) Number Theory Number Theory(L5).
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2002 Tuesday, 26 November Number-Theoretic Algorithms Chapter 31.
6/20/2015 5:05 AMNumerical Algorithms1 x x1x
Lecture 3.2: Public Key Cryptography II CS 436/636/736 Spring 2012 Nitesh Saxena.
Chapter 8 – Introduction to Number Theory Prime Numbers
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 7 Tuesday, 11/6/01 Number-Theoretic Algorithms Chapter.
CSE 311 Foundations of Computing I Lecture 12 Primes, GCD, Modular Inverse Spring

Information Security and Management 4. Finite Fields 8
Implementing RSA Encryption in Java
YSLInformation Security -- Public-Key Cryptography1 Prime and Relatively Prime Numbers Divisors: We say that b  0 divides a if a = mb for some m, where.
CSE 311: Foundations of Computing Fall 2014 Lecture 12: Primes, GCD.
MA/CSSE 473 Day 08 Extended Euclid's Algorithm Modular Division Fermat's little theorem.
Introduction to Number Theory
Introduction to Number Theory Department of Computer Engineering Sharif University of Technology 3/8/2006.
CSE 311: Foundations of Computing Fall 2013 Lecture 12: Primes, GCD, modular inverse.
Discrete Mathematics
CSE 311: Foundations of Computing Fall 2013 Lecture 11: Modular arithmetic and applications.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Information and Computer Security CPIS 312 Lab 8 1 Asymmetric Key Algorithms RSA Algorithm TRIGUI Mohamed Salim.
CSE 311 Foundations of Computing I Lecture 12 Modular Arithmetic and Applications Autumn 2012 CSE
MA/CSSE 473 Day 05 Factors and Primes Recursive division algorithm.
Discrete Math II Howon Kim
MA/CSSE 473 Day 05 Factors and Primes Recursive division algorithm.
Integers and Division Section 3.4.
Numerical Algorithms x x-1 Numerical Algorithms
CSE 311 Foundations of Computing I
Numerical Algorithms x x-1
NUMBER THEORY.
Prime and Relatively Prime Numbers
Discrete Math for CS CMPSC 360 LECTURE 12 Last time: Stable matching
Recursion Method calling itself (circular definition)
Presentation transcript:

Maths for Programming Contests

Basics Number Represenation in varius bases Basic operations Divisibility Reading long long long numbers Basic math functions like factorial http://www.spoj.pl/problems/FCTRL/

GCD of Two Numbers If b|a then gcd(a,b) = b Otherwise a = bt+r for some t and r gcd(a,b) = gcd(b,r) gcd(a,b) = gcd(b,a%b) Lcm(a,b) = (a*b)/gcd(a,b) int gcd(int a, int b){ if (b==0) return a; else return gcd(b,a%b); } http://www.spoj.pl/problems/GCD2/

Prime Numbers Definition Checking if a number is prime Generating all prime factors of a number Generating all prime numbers less than a given number (Sieve of Eratosthenes) http://www.codechef.com/problems/PRIMES2/

Modular Arithmetic (x+y) mod n = ((x mod n) + (y mod n))mod n (xy) mod n=((xy/2)mod n) ((xy/2)mod n)mod n a simple recursive function to compute the value in O(log n) time.

Modular Exponentiation Another way to compute it in O(log n) time Y = a0 + a1*2 + a2*22+…+ak*2k X(a+b) = Xa*Xb Xab = (Xa)b XY = Xa0 + (X2)a1 + (X2^2)a2 +….+(X2^k)ak int exp(int x,int y,int mod){ int result=1; while(y){ if((y & 1) == 1){ result = (result*x)%mod; } y = y >> 1; x = (x*x)%mod; return result;

Modular Multiplication int mulmod(int a,int b,int MOD){ int result = 0,tmp=a,x=b; while(x){ if(x&1){ result += tmp; if(result >=MOD) result -= MOD; } tmp += tmp; if(tmp >= MOD) tmp -= MOD; x = x>>1; return result;

Euler’s totient function Φ(n) – Number of positive integers less than or equal to n which are coprime to n Φ(ab) = φ(a) φ(b) For a prime p φ(p) = p-1 For a prime p φ(pk) = pk-pk-1 = pk(1-1/p) N = (p1k1)*(p2k2)*…*(pnkn) Φ(N) = (p1k1(1-1/p1) )*…*(pnkn(1-1/pn)) Φ(N) = N(1-1/p1)(1-1/p2)…..(1-1/pn)

Sieve technique for Euler’s function void prefill(){ int i,j; phi[1] = 0; for(i=2;i<=MAX;i++){ phi[i] = i; } if(phi[i] == i){ for(j=i;j<=MAX;j+=i){ phi[j] = (phi[j]/i)*(i-1);

Euler’s Theorem When a and n are co-prime if x ≡ y (mod φ(n)), then ax ≡ ay (mod n). aφ(n) ≡ 1 (mod n) (actual theorem the above is a generalization) Euler’s Theorem is a genaralization for Fermat's little theorem

Counting Product Rule Sum Rule Principle of inclusion and exclusion Closed form expression Recurrence Relations Use of DP to compute the values using the above recurren relations

Additional things to look up Extended Euclidean algorithm for GCD Chinese Remaindering theorem Mobius Function and its computation using seive

Practice http://www.spoj.pl/problems/AU12/ http://www.spoj.pl/problems/PRIME1/ http://www.spoj.pl/problems/LASTDIG2/ http://www.spoj.pl/problems/AUCSE015/ http://www.spoj.pl/problems/MAIN111/ http://www.spoj.pl/problems/PRIMES2/ http://www.spoj.pl/problems/IITD4/ http://www.spoj.pl/problems/NDIVPHI/ http://www.spoj.pl/problems/TRICOUNT/ http://www.spoj.pl/problems/CUBEFR/ http://www.spoj.pl/problems/NOSQ/ http://www.spoj.pl/problems/MMOD29/ http://www.spoj.pl/problems/PROGPROG/