CS 491 CAP Mathematics

Similar documents
4 Number Theory and Cryptography

LARGE PRIME NUMBERS. In sum, Fermat pseudoprimes are reasonable candidates to be prime.

Public Key Encryption

MATH 433 Applied Algebra Lecture 4: Modular arithmetic (continued). Linear congruences.

MATH 501 Discrete Mathematics. Lecture 6: Number theory. German University Cairo, Department of Media Engineering and Technology.

Number Theory. Zachary Friggstad. Programming Club Meeting

LARGE PRIME NUMBERS (32, 42; 4) (32, 24; 2) (32, 20; 1) ( 105, 20; 0).

CMPUT 403: Number Theory

Maths Probability Problems. Maths. League of Programmers. ACA, IIT Kanpur. October 22, 2012

3 The fundamentals: Algorithms, the integers, and matrices

CS250: Discrete Math for Computer Science

p = This is small enough that its primality is easily verified by trial division. A candidate prime above 1000 p of the form p U + 1 is

a the relation arb is defined if and only if = 2 k, k

Lecture 8: Number theory

Basic elements of number theory

Intermediate Math Circles February 26, 2014 Diophantine Equations I

Basic elements of number theory

Number theory (Chapter 4)

MATH 145 Algebra, Solutions to Assignment 4

Q 2.0.2: If it s 5:30pm now, what time will it be in 4753 hours? Q 2.0.3: Today is Wednesday. What day of the week will it be in one year from today?

[Part 2] Asymmetric-Key Encipherment. Chapter 9. Mathematics of Cryptography. Objectives. Contents. Objectives

Applied Cryptography and Computer Security CSE 664 Spring 2017

PUTNAM TRAINING NUMBER THEORY. Exercises 1. Show that the sum of two consecutive primes is never twice a prime.

Ch 4.2 Divisibility Properties

cse 311: foundations of computing Spring 2015 Lecture 12: Primes, GCD, applications

1. Algebra 1.7. Prime numbers

Number Theory and Group Theoryfor Public-Key Cryptography

M381 Number Theory 2004 Page 1

Chapter 9 Mathematics of Cryptography Part III: Primes and Related Congruence Equations

Chapter 3: The Euclidean Algorithm and Diophantine. Math 138 Burger California State University, Fresno

5: The Integers (An introduction to Number Theory)

Homework #2 solutions Due: June 15, 2012

Slides by Christopher M. Bourke Instructor: Berthe Y. Choueiry. Spring 2006

CPSC 467b: Cryptography and Computer Security

Mathematical Foundations of Cryptography

ALG 4.0 Number Theory Algorithms:

COMP239: Mathematics for Computer Science II. Prof. Chadi Assi EV7.635

The Euclidean Algorithm and Multiplicative Inverses

CHAPTER 6. Prime Numbers. Definition and Fundamental Results

MTH 346: The Chinese Remainder Theorem

NUMBER THEORY AND CODES. Álvaro Pelayo WUSTL

Solution Sheet (i) q = 5, r = 15 (ii) q = 58, r = 15 (iii) q = 3, r = 7 (iv) q = 6, r = (i) gcd (97, 157) = 1 = ,

Applied Cryptography and Computer Security CSE 664 Spring 2018

Mathematics of Cryptography

Discrete Structures Lecture Primes and Greatest Common Divisor

CSE 311: Foundations of Computing. Lecture 12: Two s Complement, Primes, GCD

4 Powers of an Element; Cyclic Groups

Number Theory Proof Portfolio

CSC 474 Information Systems Security

Intermediate Math Circles February 29, 2012 Linear Diophantine Equations I

Addition. Ch1 - Algorithms with numbers. Multiplication. al-khwārizmī. al-khwārizmī. Division 53+35=88. Cost? (n number of bits) 13x11=143. Cost?

Number Theory. CSS322: Security and Cryptography. Sirindhorn International Institute of Technology Thammasat University CSS322. Number Theory.

Numbers. Çetin Kaya Koç Winter / 18

cse 311: foundations of computing Fall 2015 Lecture 12: Primes, GCD, applications

Math 312/ AMS 351 (Fall 17) Sample Questions for Final

Corollary 4.2 (Pepin s Test, 1877). Let F k = 2 2k + 1, the kth Fermat number, where k 1. Then F k is prime iff 3 F k 1

Number Theory Notes Spring 2011

ECE 646 Lecture 5. Mathematical Background: Modular Arithmetic

Math 109 HW 9 Solutions

Number theory. Myrto Arapinis School of Informatics University of Edinburgh. October 9, /29

1 Recommended Reading 1. 2 Public Key/Private Key Cryptography Overview RSA Algorithm... 2

Objective Type Questions

Senior Math Circles Cryptography and Number Theory Week 2

7.2 Applications of Euler s and Fermat s Theorem.

Homework 7 solutions M328K by Mark Lindberg/Marie-Amelie Lawn

Outline. Number Theory and Modular Arithmetic. p-1. Definition: Modular equivalence a b [mod n] (a mod n) = (b mod n) n (a-b)

Introduction to Information Security

Math From Scratch Lesson 20: The Chinese Remainder Theorem

CS 5319 Advanced Discrete Structure. Lecture 9: Introduction to Number Theory II

A Few Primality Testing Algorithms

2.3 In modular arithmetic, all arithmetic operations are performed modulo some integer.

MATH FINAL EXAM REVIEW HINTS

PROBLEMS ON CONGRUENCES AND DIVISIBILITY

Introduction to Number Theory

The set of integers will be denoted by Z = {, -3, -2, -1, 0, 1, 2, 3, 4, }

Elementary Number Theory MARUCO. Summer, 2018

Chapter 4 Finite Fields

NUMBER THEORY. Anwitaman DATTA SCSE, NTU Singapore CX4024. CRYPTOGRAPHY & NETWORK SECURITY 2018, Anwitaman DATTA

Chuck Garner, Ph.D. May 25, 2009 / Georgia ARML Practice

2x 1 7. A linear congruence in modular arithmetic is an equation of the form. Why is the solution a set of integers rather than a unique integer?

The RSA Cryptosystem: Factoring the public modulus. Debdeep Mukhopadhyay

This is a recursive algorithm. The procedure is guaranteed to terminate, since the second argument decreases each time.

Lecture notes: Algorithms for integers, polynomials (Thorsten Theobald)

CIS 551 / TCOM 401 Computer and Network Security

CSE 20 DISCRETE MATH. Winter

Elliptic curves: Theory and Applications. Day 3: Counting points.

Cryptography CS 555. Topic 18: RSA Implementation and Security. CS555 Topic 18 1

Remainders. We learned how to multiply and divide in elementary

An integer p is prime if p > 1 and p has exactly two positive divisors, 1 and p.

Numbers, Groups and Cryptography. Gordan Savin

CISC-102 Fall 2017 Week 6

12x + 18y = 50. 2x + v = 12. (x, v) = (6 + k, 2k), k Z.

A Guide to Arithmetic

Math 131 notes. Jason Riedy. 6 October, Linear Diophantine equations : Likely delayed 6

CSE 311 Lecture 13: Primes and GCD. Emina Torlak and Kevin Zatloukal

Lecture 11 - Basic Number Theory.

Math.3336: Discrete Mathematics. Primes and Greatest Common Divisors

Elementary Number Theory Review. Franz Luef

2x 1 7. A linear congruence in modular arithmetic is an equation of the form. Why is the solution a set of integers rather than a unique integer?

Advanced Algorithms and Complexity Course Project Report

Transcription:

CS 491 CAP Mathematics Zhengkai Wu University of Illinois at Urbana-Champaign Oct 20, 2017

Today Number theory Combinatorics and Probability 2

Number theory Primes Sieve of Eratosthenes GCD/LCM Euclidean Algorithm and Extension Fast exponentiation Fermat Little Theorem Miller Rabin Inverse element in modular group Chinese Remainder Theorem 3

Primes >1, only two divisors, 1 and itself. How to check if an integer is a prime? By definition, loop from 2 to N-1 to see if divisible. Can be optimized to check 2 to n Why? Complexity: O( n) 4

Sieve of Eratosthenes What if we want to generate all primes <= N? Brute Force: Run prime check on every integer, O(n* n) Let P[1..n] be the array of bools to represent each integer s primality. 5

Sieve of Eratosthenes For each prime number p, we will mark 2p, 3p, as nonprime. Complexity: O(NloglogN) 6

Linear Sieve Let prime[1..k] to store all primes. Count = 0; For i = 2 to N do If (p[i]) prime[count++] = I; For j = 0 ; j<count && prime[j]*i<=n;++j P[prime[j]*i] = false; If (i mod prime[j] == 0) break; Why linear? All composites are eliminated by its minimal prime factor exactly once. 7

Greatest Common Divisor (GCD) GCD(a,b) is the greatest divisor of both a and b. Example: GCD(30,12) = 6; GCD(17,15) = 1 How to compute? GCD(a,b) = GCD(b, a mod b) GCD(a,0) = a Euclidean Algorithm. Complexity: O(log(a+b)^3) 8

Extended Euclidean Algorithm How to solve the equation: ax + by = gcd(a,b) given a,b. 6x + 4y = 2 => x=1 and y=-1 Start from b = 0: gcd(a, 0) = a ax = a => x = 1 Any hint from gcd(a,b) = gcd(b, a mod b)? 9

Extended Euclidean Algorithm Suppose we can solve bx + (a mod b)y = gcd(b, a mod b) Since gcd(a,b) = gcd(b, a mod b). Also a mod b = a a b b Therefore ay + b(x - a b y ) = gcd(a,b) Thus if we have x and y. x = y, y = x - a b y Note that this won t be the unique solution. (Why?) What if the equation is ax + by = c? 10

Extended Euclidean Algorithm If gcd(a,b) c, then there exists a solution. Otherwise, no solution. In competitive programming, the most important problem/usage of Number Theory is to solve different Diophantine equation (find integer solution of polynomial equations). May also be true in real math. (Fermat s last theorem) 11

Fast exponentiation How to compute a^n quickly if n is large? Time complexity: O(log(n)*each multiplication time) Can be used in any operator that have associative law. (For example: Matrix exponentiation) 12

Fermat Little Theorem If n is prime, then a^n = a (mod n) holds for all integer a. Is the converse true? No, counter example: Pseudo prime: 341, 2^341 = 2 (mod 341), but 341 is not a prime. (341 = 31*11) It is called a pseudoprime under base 2. Strong Pseudo prime: 561, pseudoprime under all bases. 13

Miller-Rabin If the converse is true, we can actually check an integer n by randomly choosing base a and quickly calculate a^n mod n. Check wiki for Miller-Rabin algorithm: https://en.wikipedia.org/wiki/miller%e2%80%93rabin _primality_test Based on Fermat Little Theorem, but changed into a probability algorithm. You may want to have this in your template. 14

Inverse element Sometimes we want to calculate the division under modulo. What is a/b mod n? We call c is the inverse element of b under modulo n if c*b mod n = 1. So /b will be equal to *c under the meaning of mod n. 15

Inverse element This means, c*b = k*n +1. => c*b k*n = 1, in which b and n are known and we want to solve for c. Use extended euclidean algorithm. Have solution iff gcd(n,b) = 1. Much easier if n is a prime. (most cases in ICPC) Since a^n = a (mod n) => a^(n-1) = 1 (mod n) Thus the inverse of a would be a^(n-2). 16

Chinese Remainder Theorem We have a set of equations: How to solve it? (assuming n1,,nk are coprime) 17

Chinese Remainder Theorem Let N = the product of all ni and Ni = N/ni. We want to find Mi such that Mi*Ni + mi*ni = 1. So that let x = σk i=1 a i M i N i will be the solution. Why? Think of what does Mi mean here. 18

Combinatorics P(n,r), the number of ways of permutation of r elements out of n elements. (n elements all different) n! P(n,r) = n r! C(n,r), the number of ways of picking r elements out of n elements. (n elements all different) C(n,r) = P(n,r) = n! r! r! n r! 19

Combinatorics C(n,r) = C(n-1,r)+C(n-1,r-1) C(n,0) = 1 Useful when solving C(n,r) for not large n (typically <= 5k, and module some prime number) 20

Stars and bars Method How many integer solutions in the equation x1+x2+ +xm = n? (xi >= 0) C(n+m,m-1) What if xi>=ai? 21

How to compute C(n,r) mod p? If p is a prime: N<=10^6, r<=10^6 Preprocess the n! and inverse of n!. N<=10^10,r<=10^10,p<=1000 Lucas theorem, write n and r in base p, (n1 n2.. nk)p and (r1 r2 rk)p, then C(n,r)= product of c(ni,ri). If p is not a prime: N<=5k, r<=5k C(n,r) = C(n-1,r)+C(n-1,r-1) N<=10^6,r<=10^6, p<=10^5 Find factors of p, get the count of the factors in n!, (n-r)! and r!. 22

Probability Must be clear what is the base event in the problem. Conditional probability: P(A B)=P(A B)/P(B) 23

Expected value E[x] Expectation of random variable x. E[x] = σ p i x i discrete = p x xdx continuous E[x+y] = E[x] + E[y] for any x and y. E[x*y] = E[x] * E[y], for independent x and y. 24

POJ 2096 A system has n bugs and s subsystems. (n,s <= 1000) When you are testing the system, you can run exactly one subsystem everyday and you are finding exactly one bug (on that subsystem) everyday. (The bug you find may not be new) What s the expected days of finding every bugs at least one time and also finding at least one bug in each subsystem. Dp[i][j] = (i/n)*(j/s) * dp[i][j] + (i/n)*(s-j)/s * dp[i][j-1] + (n-i)/n*(j/s) * dp[i-1][j] + (n-i)/n*(s-j)/s * dp[i-1][j-1]; 25

Uva 11762 Given n, each time randomly pick an prime p that is less than n. If n is divisible by p, then let n/=p, otherwise n remains same. What s the expected steps needed to transform n to 1. N <= 10^4 Let k = number of primes less than n. f[n] = 1/k * f[a0] + 1/k * f[a1] + ai = n/pi if n divisible by pi = n otherwise. 26