escola de verao - unicamp - aritmetica - fidel

download escola de verao - unicamp - aritmetica - fidel

of 74

Transcript of escola de verao - unicamp - aritmetica - fidel

  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    1/74

    Review on arithmetics for ICPC

    Escola de Verao

    Maratona de Programacao

    Fidel I. Schaposnik (UNLP) - [email protected] 28, 2013

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    2/74

    Contents

    Natural numbers

    Finding prime numbersFactorization, Eulers (n) and the number of divisors of n

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    3/74

    Contents

    Natural numbers

    Finding prime numbersFactorization, Eulers (n) and the number of divisors of n

    Modular arithmetics

    Basic operationsModExp

    GCD and its extensionChinese Reminder Theorem

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    4/74

    Contents

    Natural numbers

    Finding prime numbersFactorization, Eulers (n) and the number of divisors of n

    Modular arithmetics

    Basic operationsModExp

    GCD and its extensionChinese Reminder Theorem

    Matrices

    Notation and basic operationsAdjacency matrix for a graph

    Markov chains and other linear problemsSystems of equationsGauss-Jordan algorithmA particular case: bi-diagonal matrices

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    5/74

    Contents

    Natural numbers

    Finding prime numbersFactorization, Eulers (n) and the number of divisors of n

    Modular arithmetics

    Basic operationsModExp

    GCD and its extensionChinese Reminder Theorem

    Matrices

    Notation and basic operationsAdjacency matrix for a graph

    Markov chains and other linear problemsSystems of equationsGauss-Jordan algorithmA particular case: bi-diagonal matrices

    One additional problem

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    6/74

    Natural numbers

    Recall that

    p N is prime 1 and p are ps only divisors in N

    Given n N, we may uniquely factorize it as

    n = pei1 . . . pekk

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    7/74

    Natural numbers

    Recall that

    p N is prime 1 and p are ps only divisors in N

    Given n N, we may uniquely factorize it as

    n = pei1 . . . pekk

    Finding prime numbers and/or the factorization of a given numberwill be useful to solve problems involving:

    (completely) multiplicative functions

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    8/74

    Natural numbers

    Recall that

    p N is prime 1 and p are ps only divisors in N

    Given n N, we may uniquely factorize it as

    n = pei1 . . . pekk

    Finding prime numbers and/or the factorization of a given numberwill be useful to solve problems involving:

    (completely) multiplicative functionsdivisors of a given number

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    9/74

    Natural numbers

    Recall that

    p N is prime 1 and p are ps only divisors in N

    Given n N, we may uniquely factorize it as

    n = pei1 . . . pekk

    Finding prime numbers and/or the factorization of a given numberwill be useful to solve problems involving:

    (completely) multiplicative functionsdivisors of a given number

    prime numbers and factorizations in general :-)

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    http://find/http://goback/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    10/74

    Algorithms to find prime numbers

    We would like to find all prime numbers up to a given number N(e.g. to factorize m we need all prime numbers up to

    m).

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    11/74

    Algorithms to find prime numbers

    We would like to find all prime numbers up to a given number N(e.g. to factorize m we need all prime numbers up to

    m).

    A very naive algorithm: for each n [2,N), we check if n isdivisible by some prime number smaller than

    n (we have

    already found all of them). With some minor optimizations:

    1 p [ 0 ] = 2 ; P = 1 ;

    2 f o r ( i =3; i

  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    12/74

    Algorithms to find prime numbers

    We would like to find all prime numbers up to a given number N(e.g. to factorize m we need all prime numbers up to

    m).

    A very naive algorithm: for each n [2,N), we check if n isdivisible by some prime number smaller than

    n (we have

    already found all of them). With some minor optimizations:

    1 p [ 0 ] = 2 ; P = 1 ;

    2 f o r ( i =3; i

  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    13/74

    Algorithms to find prime numbers (cont.)

    A very old algorithm: build a table with the numbers in therange [2,N), and iterate through it. Each un-crossed numberwe find is a prime, so we can cross all its multiples in the table:

    2 3 4 5 6 7 8 9 10 1112 13 14 15 16 17 18 19 20 2122 23 24 25 26 27 28 29 30 3132 33 34 35 36 37 38 39 40 41

    42 43 44 45 46 47 48 49 50 51

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    14/74

    Algorithms to find prime numbers (cont.)

    A very old algorithm: build a table with the numbers in therange [2,N), and iterate through it. Each un-crossed numberwe find is a prime, so we can cross all its multiples in the table:

    2 3 4 5 6 7 8 9 10 1112 13 14 15 16 17 18 19 20 2122 23 24 25 26 27 28 29 30 31

    32 33

    34 35

    36 37

    38 39

    40 41

    42 43 44 45 46 47 48 49 50 51

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    15/74

    Algorithms to find prime numbers (cont.)

    A very old algorithm: build a table with the numbers in therange [2,N), and iterate through it. Each un-crossed numberwe find is a prime, so we can cross all its multiples in the table:

    2 3 4 5 6 7 8 9 10 1112 13 14 15 16 17 18 19 20 2122 23 24 25 26 27 28 29 30 3132 33 34 35 36 37 38 39 40 4142 43 44 45 46 47 48 49 50 51

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    ( )

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    16/74

    Algorithms to find prime numbers (cont.)

    A very old algorithm: build a table with the numbers in therange [2,N), and iterate through it. Each un-crossed numberwe find is a prime, so we can cross all its multiples in the table:

    2 3 4 5 6 7 8 9 10 1112 13 14 15 16 17 18 19 20 2122 23 24 25 26 27 28 29 30 3132 33 34 35 36 37 38 39 40 4142 43 44 45 46 47 48 49 50 51

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Al i h fi d i b ( )

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    17/74

    Algorithms to find prime numbers (cont.)

    A very old algorithm: build a table with the numbers in therange [2,N), and iterate through it. Each un-crossed numberwe find is a prime, so we can cross all its multiples in the table:

    2 3 4 5 6 7 8 9 10 1112 13 14 15 16 17 18 19 20 2122 23 24 25 26 27 28 29 30 3132 33 34 35 36 37 38 39 40 4142 43 44 45 46 47 48 49 50 51

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Al i h fi d i b ( )

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    18/74

    Algorithms to find prime numbers (cont.)

    A very old algorithm: build a table with the numbers in therange [2,N), and iterate through it. Each un-crossed numberwe find is a prime, so we can cross all its multiples in the table:

    2 3 4 5 6 7 8 9 10 1112 13 14 15 16 17 18 19 20 2122 23 24 25 26 27 28 29 30 31

    32

    33

    34

    35

    36 37

    38

    39

    40 41

    42 43 44 45 46 47 48 49 50 51

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Al i h fi d i b ( )

    http://find/http://goback/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    19/74

    Algorithms to find prime numbers (cont.)

    The corresponding code looks like

    1 memset( is p , t r u e , s i z e o f ( i s p ) ) ;2 f o r ( i =2; i

  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    20/74

    Algorithms to find prime numbers (cont.)

    The corresponding code looks like

    1 memset( is p , t r u e , s i z e o f ( i s p ) ) ;2 f o r ( i =2; i

  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    21/74

    Factorization using the sieve

    The sieve can actually hold more information

    1 f o r ( i =4; i

  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    22/74

    Factorization using the sieve

    The sieve can actually hold more information

    1 f o r ( i =4; i

  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    23/74

    Divisors of a given number

    We can now generate the divisors of a given number recursively

    1 i n t d [MAXD] , D=0;23 v o i d d i v ( i n t cu r , i n t f [ ] , i n t s , i n t e ) {4 i f ( s == e ) d [ D++] = cu r ;5 e l s e {6 i n t m;7 f o r (m=s +1; m

  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    24/74

    Some functions from number theory

    If we can factorize a given number n, we can calculate somenumber theory related functions:

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Some functions from number theory

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    25/74

    Some functions from number theory

    If we can factorize a given number n, we can calculate somenumber theory related functions:

    Eulers function: (n) is the number of positive integersless than n that are coprime with n. We have

    (n) = (pe11 pe111 ) . . . (pekk pek1k )

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Some functions from number theory

    http://find/http://goback/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    26/74

    Some functions from number theory

    If we can factorize a given number n, we can calculate somenumber theory related functions:

    Eulers function: (n) is the number of positive integersless than n that are coprime with n. We have

    (n) = (pe11 pe111 ) . . . (pekk pek1k )

    The number of divisors of n is0(n) = (e1 + 1) . . . (ek + 1)

    Larger numbers of divisors are achieved when there are manydistinct prime factors (n different prime factors

    (n) = 2n):

    6.469.693.230 = 2 3 5 7 11 13 17 19 23 29only has 1024 divisors.

    There are similar formulas for m(n) =d|n

    dm

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Further reading

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    27/74

    Further reading

    There are other, faster algorithms to find prime numbers. Inparticular, the sieve of Eratosthenes can be further optimized:

    in memory, to use just one bit per number;

    using a wheel to achieve better running time:

    1 i n t w [ 8 ] = {4 ,2 ,4 ,2 ,4 ,6 ,2 ,6 } ;23 f o r ( i =7 , c u r = 0; i i

  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    28/74

    Further reading

    There are other, faster algorithms to find prime numbers. Inparticular, the sieve of Eratosthenes can be further optimized:

    in memory, to use just one bit per number;

    using a wheel to achieve better running time:

    1 i n t w [ 8 ] = {4 ,2 ,4 ,2 ,4 ,6 ,2 ,6 } ;23 f o r ( i =7 , c u r = 0; i i

  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    29/74

    Modular arithmetic

    Recall that given a, r Z and m N

    a m r a = q.m + r with r = 0, 1, . . . , m 1

    Summation, substraction and multiplication operations are triviallyextended and keep their well-known properties

    a b = c = a bm c

    a.b = c = a.bm c

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Modular arithmetic

    http://find/http://goback/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    30/74

    Modular arithmetic

    Recall that given a, r Z and m N

    a m r a = q.m + r with r = 0, 1, . . . , m 1

    Summation, substraction and multiplication operations are triviallyextended and keep their well-known properties

    a b = c = a bm c

    a.b = c = a.bm cDivision is defined as the inverse of multiplication, so

    a/b = a.b1 with b.b1 = 1

    Does the inverse mod m always exist? How do we calculate it?

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    ModExp

    http://goforward/http://find/http://goback/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    31/74

    ModExp

    Sometimes we can avoid calculating inverses altogether: e.g.problem Last Digit (MCA07) asks us to calculate the last

    non-zero digit of

    =

    N

    m1 . . .mM

    =

    N!

    m1! . . . mM!with

    Mi=1

    mi = N and N 106

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    ModExp

    http://find/http://goback/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    32/74

    p

    Sometimes we can avoid calculating inverses altogether: e.g.problem Last Digit (MCA07) asks us to calculate the last

    non-zero digit of

    =

    N

    m1 . . .mM

    =

    N!

    m1! . . . mM!with

    Mi=1

    mi = N and N 106

    We may factorize using the sieve, then evaluate it mod 10 aftereliminating any factors of 10 (i.e. the maximum possible numberof 5s and 2s). We also need to efficiently evaluate ab mod m:

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    ModExp

    http://goforward/http://find/http://goback/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    33/74

    p

    Sometimes we can avoid calculating inverses altogether: e.g.problem Last Digit (MCA07) asks us to calculate the last

    non-zero digit of

    =

    N

    m1 . . .mM

    =

    N!

    m1! . . . mM!with

    Mi=1

    mi = N and N 106

    We may factorize using the sieve, then evaluate it mod 10 aftereliminating any factors of 10 (i.e. the maximum possible numberof 5s and 2s). We also need to efficiently evaluate ab mod m:

    Direct evaluation is O(b), which is (usually) too slow.

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    ModExp

    http://goforward/http://find/http://goback/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    34/74

    p

    Sometimes we can avoid calculating inverses altogether: e.g.problem Last Digit (MCA07) asks us to calculate the last

    non-zero digit of

    =

    N

    m1 . . .mM

    =

    N!

    m1! . . . mM!with

    Mi=1

    mi = N and N 106

    We may factorize using the sieve, then evaluate it mod 10 aftereliminating any factors of 10 (i.e. the maximum possible numberof 5s and 2s). We also need to efficiently evaluate ab mod m:

    Direct evaluation is O(b), which is (usually) too slow.Write b in binary, then b = c0.2

    0 +

    + clog b.2

    log b and we

    can evaluate ab in O(log b)

    ab =

    log bi=0,ci=0

    a2i

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    ModExp (code)

    http://find/http://goback/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    35/74

    p ( )

    1 l o n g l o n g modexp( l o n g l o n g a , i n t b ) {2 l o ng l o n g RES = 1LL;3 w h i l e ( b > 0 ) {4 i f ( b&1) RES = (RESa )% MOD;5 b >>= 1 ;

    6 a = ( aa )% MOD;7 }8 r e t u r n RES;9 }

    ModExp

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Last Digit (MCA07)

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    36/74

    ( )

    1 i n t c a l c ( i n t N, i n t m [ ] , i n t M) {2 i n t i , RES ;3

    4 e [ N]++;5 f o r ( i =0; i 1) e [m[ i ] ];6 f o r ( i=N1; i >=2; i ) e [ i ] += e [ i +1] ;7 f o r ( i=N; i >=2; i ) i f ( p [ i ] ) {8 e [ i /p [ i ] ] += e [ i ] ;9 e [ p [ i ] ] += e [ i ] ;

    10 e [ i ] = 0 ;11 }12 i n t tmp = min ( e [ 2 ] , e [ 5 ] ) ;13 e [ 2 ] = tmp ; e [ 5 ] = t m p ;1415 RES = 1 ;16 f o r ( i =2; i

  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    37/74

    The greatest common divisor between a and b is the largest d suchthat d

    |a and d

    |b. Note that

    a = q.b+ r = gcd(a, b) = gcd(b, r)

    Which leads to

    1 i n t g c d ( i n t a , i n t b ) {2 i f (b == 0 ) r e t u r n a ;3 r e t u r n gcd (b , a%b ) ;4 }

    Euclids algorithm

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    GCD

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    38/74

    The greatest common divisor between a and b is the largest d suchthat d

    |a and d

    |b. Note that

    a = q.b+ r = gcd(a, b) = gcd(b, r)

    Which leads to

    1 i n t g c d ( i n t a , i n t b ) {2 i f (b == 0 ) r e t u r n a ;3 r e t u r n gcd (b , a%b ) ;4 }

    Euclids algorithm

    It can be shown that gcd(Fn+1,Fn) takes exactly n operations(here Fn are the Fibonacci numbers). Because Fn growsexponentially and is the worst-case input for this algorithm,running time is O(log n).

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Extended GCD

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    39/74

    One may prove that

    gcd(a,m) = 1

    1 = a.x + m.y

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Extended GCD

    http://find/http://goback/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    40/74

    One may prove that

    gcd(a,m) = 1

    1 = a.x + m.y

    Then xm a1, so that a has an inverse mod m if and only ifgcd(a,m) = 1. [Corolary: Zp is a field.]

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Extended GCD

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    41/74

    One may prove that

    gcd(a,m) = 1

    1 = a.x + m.y

    Then xm a1, so that a has an inverse mod m if and only ifgcd(a,m) = 1. [Corolary: Zp is a field.] To find x and y, we tracethem along Euclids algorithm:

    1 p i i e g cd ( i n t a , i n t b ) {

    2 i f (b == 0 ) r e t u r n m a k e p a i r ( 1 , 0 ) ;3 e l s e {4 p i i RES = e gcd ( b , a%b ) ;5 r e t u r n m a k e p a i r ( RES . s e c o n d , RES . f i r s t RES . sec ond ( a / b ) ) ;6 }7 }8

    9 i n t i n v ( i n t n , i n t m) {10 p i i EGCD = e g c d ( n , m) ;11 r e t u r n ( (EGCD . f i r s t % m)+m)% m;12 }

    Extension of Euclids algorithm; inverse mod m

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Chinese Remainder Theorem

    http://goforward/http://find/http://goback/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    42/74

    Given a set of conditions

    x ai mod ni for i = 1, . . . , k with gcd(ni, nj) = 1 i= j

    there is a unique x mod N = n1 . . . nk satisfying all theseconditions simultaneously.

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Chinese Remainder Theorem

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    43/74

    Given a set of conditions

    x ai mod ni for i = 1, . . . , k with gcd(ni, nj) = 1 i= j

    there is a unique x mod N = n1 . . . nk satisfying all theseconditions simultaneously. We may construct it considering

    mi =j=i

    nj = gcd(ni,mi) = 1

    Let mi = m1i mod ni, then

    xk

    i=1

    mimiai mod N

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Chinese reminder theorem (code)

    http://find/http://goback/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    44/74

    1 i n t c r t ( i n t n [ ] , i n t a [ ] , i n t k ) {2 i n t i , tmp , MOD, RES ;34 MOD = 1 ;5 f o r ( i =0; i

  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    45/74

    An N M matrix is an array of N rows and M columns ofelements. A natural way to define the sum and the difference of

    matrices is (O(N.M)):A B = C Cij = Aij Bij

    The product of matrices is defined as (O(N.M.L))

    ANM BML = CNL Cij =M

    k=1

    Aik.Bkj

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Matrices

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    46/74

    An N M matrix is an array of N rows and M columns ofelements. A natural way to define the sum and the difference of

    matrices is (O(N.M)):A B = C Cij = Aij Bij

    The product of matrices is defined as (O(N.M.L))

    ANM BML = CNL Cij =M

    k=1

    Aik.Bkj

    For square matrices (from now on, assume we are working with

    N N), it makes sense to ask if a given matrix A has an inverse.One finds that if det A = 0, the inverse exists and satisfies

    A A1 = 1 = A1 A

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Matrices (cont.)

    http://find/http://goback/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    47/74

    We represent matrices using bi-dimensional arrays. In C++, to usea matrix as an argument of a function, it is convenient to define alist of pointers to avoid fixing one of the dimensions in thedefinition of the function

    1 t yp e f u n c t i o n ( i n t A , i n t N, i n t M) {2 . . .3 }

    45 i n t main ( ) {6 i n t a [MAXN] [MAXN] , pa [MAXN] ;78 f o r ( i n t i =0 ; i

  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    48/74

    We may use a matrix to represent the edges of a graph.

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Adjacency matrix

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    49/74

    We may use a matrix to represent the edges of a graph. For agraph of N nodes, a matrix ANN can have Aij:

    the weight of the edge going from node i to node j (or ifthis edge does not exist);

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Adjacency matrix

    http://find/http://goback/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    50/74

    We may use a matrix to represent the edges of a graph. For agraph of N nodes, a matrix ANN can have Aij:

    the weight of the edge going from node i to node j (or ifthis edge does not exist);

    the number of edges that go from node i to node j (0 if thereare none).

    In the latter case,

    (A2)ij =

    k

    AikAkj = paths i j with exactly 2 edges

    and in general An contains the number of paths between the pairsof nodes of the original graph with exactly n edges.

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Adjacency matrix

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    51/74

    We may use a matrix to represent the edges of a graph. For agraph of N nodes, a matrix ANN can have Aij:

    the weight of the edge going from node i to node j (or ifthis edge does not exist);

    the number of edges that go from node i to node j (0 if thereare none).

    In the latter case,

    (A2)ij =

    k

    AikAkj = paths i j with exactly 2 edges

    and in general An contains the number of paths between the pairsof nodes of the original graph with exactly n edges.We may calculate An using an adapted version of ModExp inO(N3 log n).

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Linear recurrences

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    52/74

    A linear recurrence of order K defines a sequence {an} through

    an =

    Kk=1

    ckank

    and a vector aK = (aK, aK1, . . . , a1) of initial values.

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Linear recurrences

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    53/74

    A linear recurrence of order K defines a sequence {an} through

    an =

    Kk=1

    ckank

    and a vector aK = (aK, aK1, . . . , a1) of initial values.

    Let an be a vector containing an and its K

    1 previous elements.Then the recurrence can be represented as

    an = Ran1

    anan1

    ..

    .anK+2anK+1

    =

    c1 c2 cK1 0 00 1

    0

    ......

    0 1 0

    an1an2

    ..

    .anK+1

    anK

    So we can calculate the N-th term using aN = RNKaK

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Markov chains

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    54/74

    Let

    {Si

    }be a set of states, and pij the known probabilities to

    observe a transition from state i to state j. Terminal states {Sk}are such that

    i pki = 0. What is the expected number of

    transitions Ei needed to reach a terminal state from state Si?

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Markov chains

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    55/74

    Let

    {Si

    }be a set of states, and pij the known probabilities to

    observe a transition from state i to state j. Terminal states {Sk}are such that

    i pki = 0. What is the expected number of

    transitions Ei needed to reach a terminal state from state Si?For terminal states, we clearly have

    Ek = 0

    For the rest,Ei = 1 +

    j

    pij.Ej

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Markov chains

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    56/74

    Let

    {Si

    }be a set of states, and pij the known probabilities to

    observe a transition from state i to state j. Terminal states {Sk}are such that

    i pki = 0. What is the expected number of

    transitions Ei needed to reach a terminal state from state Si?For terminal states, we clearly have

    Ek = 0

    For the rest,Ei = 1 +

    j

    pij.Ej

    In other words, we need to solve a system of equations over theexpected number of transitions Ei.

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Other linear problems

    http://find/http://goback/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    57/74

    Some linear systems of equations can be found in computational

    geometry problems. As an example, something you may need latertoday: plane-plane intersection ;-)

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Other linear problems

    http://find/http://goback/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    58/74

    Some linear systems of equations can be found in computational

    geometry problems. As an example, something you may need latertoday: plane-plane intersection ;-)

    A plane is defined by a point p0 on it and a vector n0 which isorthogonal to the plane. All other points on the plane satisfy

    (p p0) n0 = 0

    which leads to the familiar equation

    ax + by + cz + d = 0 for n0 = (a, b, c) and d =

    p0n0

    In the general case, two planes defined by {p1,n1} and {p2,n2}intersect to give a line. How do we find this line?

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Other linear problems (cont.)

    A line is defined by a point p on it and a directing vector v

    http://find/http://goback/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    59/74

    A line is defined by a point p on it and a directing vector vpointing along its direction (either way).

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Other linear problems (cont.)

    A line is defined by a point p on it and a directing vector v

    http://find/http://goback/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    60/74

    A line is defined by a point p on it and a directing vector vpointing along its direction (either way).

    Our line should belong to both planes, so it is orthogonal to bothnormal vectors n1 and n2. Then

    v = n1 n2

    Ifv =0, the planes are parallel and there is no intersection. Buthow do we find p?

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Other linear problems (cont.)

    A line is defined by a point p on it and a directing vector v

    http://find/http://goback/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    61/74

    A line is defined by a point p on it and a directing vector vpointing along its direction (either way).

    Our line should belong to both planes, so it is orthogonal to bothnormal vectors n1 and n2. Then

    v = n1 n2

    Ifv =0, the planes are parallel and there is no intersection. Buthow do we find p?

    Let p be that point on the line which is closest to some fixed,arbitrary point (e.g. the origin). Then p = (x, y, z) minimizes

    D2 = x2 + y2 + z2

    while being on both planes, i.e. satisfying

    (p p1) n1 = 0 and (p p2) n2 = 0Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Other linear problems (cont.)

    We can find the point p minimizing D2 under these constraints

    http://find/http://goback/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    62/74

    We can find the point p minimizing D under these constraintsusing Lagrange multipliers. Define

    f(x, y, z, 1, 2) = x2 + y2 + z2 +1 (p p1) n1 +2 (p p2) n2Then we must ask

    f

    x= 2x+1n1x = 0

    f

    y= 2y+1n1y = 0

    f

    z= 2z+1n1z = 0

    f

    1= (p p1) n1 = 0 f

    2= (p p2) n2 = 0

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Other linear problems (cont.)

    We can find the point p minimizing D2 under these constraints

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    63/74

    We can find the point p minimizing D under these constraintsusing Lagrange multipliers. Define

    f(x, y, z, 1, 2) = x2 + y2 + z2 +1 (p p1) n1 +2 (p p2) n2Then we must ask

    f

    x= 2x+1n1x = 0

    f

    y= 2y+1n1y = 0

    f

    z= 2z+1n1z = 0

    f

    1= (p p1) n1 = 0 f

    2= (p p2) n2 = 0

    Or in matrix notation

    2 0 0 n1x n2x

    0 2 0 n1y n2y0 0 2 n1z n2z

    n1x n1y n1z 0 0n2x n2y n2z 0 0

    x

    yz12

    =

    0

    00

    p1 n1p2 n2

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Systems of equations

    A system of equations over N variables is

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    64/74

    A system of equations over N variables is

    a11 x1 +

    + a1N xN = b1

    ...

    aN1 x1 + + aN xN = bNIt can be represented by matrices as

    Ax = b

    a11 a1N...

    . . ....

    aN1 aNN

    x1...

    xN

    =

    b1...

    bN

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Systems of equations

    A system of equations over N variables is

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    65/74

    A system of equations over N variables is

    a11 x1 +

    + a1N xN = b1

    ...

    aN1 x1 + + aN xN = bNIt can be represented by matrices as

    Ax = b

    a11 a1N...

    . . ....

    aN1 aNN

    x1...

    xN

    =

    b1...

    bN

    Solving this system reduces to finding the inverse A1

    , becausethen x = A1b. We can solve many systems of equations withdifferent independent terms bi by building a matrix B with thevectors bi as its columns. Then X = A

    1B, and in particular ifB

    1, X

    A1.

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Systems of equations (cont.)

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    66/74

    To solve a system by hand, we solve for an arbitrary variable in one

    of the equations, and then use this to eliminate that variable fromevery other equation, while working simultaneously with theindependent terms. In order to do this, we may:

    Multiply or divide an equation (row) by any number.

    Sum or subtract an equation (row) to another one.Swap two rows (leaves the equations unchanged)

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Systems of equations (cont.)

    http://find/http://goback/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    67/74

    To solve a system by hand, we solve for an arbitrary variable in one

    of the equations, and then use this to eliminate that variable fromevery other equation, while working simultaneously with theindependent terms. In order to do this, we may:

    Multiply or divide an equation (row) by any number.

    Sum or subtract an equation (row) to another one.Swap two rows (leaves the equations unchanged)

    Gauss-Jordans algorithm is a formalization of this procedure, withone caveat: to reduce the numerical error, in each step the

    corresponding variable is solved from the equation where it appearswith the largest coefficient in absolute value (this is calledpivoting).

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Gauss-Jordan elimination

    1 b o o l i n v e r t ( d o u b l e A , d o u b l e B , i n t N) {

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    68/74

    ( ) {2 i n t i , j , k , jmax ; d o u b l e tmp;3 f o r ( i =1; i= i5 f o r ( j=i +1; j a b s ( A [ j m ax ] [ i ] ) ) j m ax = j ;78 f o r ( j =1; j

  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    69/74

    ( )we can multiply two N N matrices in O (T(N)), then we caninvert a matrix or calculate its determinant in the same asymptotictime.

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Gauss-Jordan elimination for bi-diagonal matrices

    Gauss-Jordans algorithm is clearly O(N3). It can be seen that if

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    70/74

    we can multiply two N N matrices in O (T(N)), then we caninvert a matrix or calculate its determinant in the same asymptotictime.

    Usually, instead of optimizing the general algorithm it is a lot moreconvenient to take advantage of some special property of the

    matrices we would like to invert: for instance, we can invert abi-diagonal or tri-diagonal matrix (with non-zero diagonalelements) in O(N).

    a11 a12 0 0 0 . . . 0

    a21 a22 a23 0 0 . . . 00 a32 a33 a34 0 . . . 0...

    ...0 . . . 0 aNN1 aNN

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    Further reading

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    71/74

    There are more efficient algorithms that you can use to

    multiply two matrices (Strassens is O(n2,807

    ),CoppersmithWinograds is O(n2,376)), but they are notnecessarily convenient for a competition...

    There are many important decompositions of a matrix (i.e.

    ways to write it as a sum or product of some other matriceswith special properties). These play an important role inmany algorithms, such as those that are used to calculatedeterminants.

    There are many ways to generalize the treatment of linearrecurrences: one can work with sets of multiple dependentrecurrences, inhomogeneous recurrences, etc.

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    An additional problem

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    72/74

    Bases (SARC08): Analyze in which bases an expression such as10000 + 3 5 334 = 3 5000 + 10 + 0 is true.

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    An additional problem

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    73/74

    Bases (SARC08): Analyze in which bases an expression such as10000 + 3 5 334 = 3 5000 + 10 + 0 is true.

    Things we have not covered:

    Polynomials (evaluation, operations, properties, etc.).Evaluation of mathematical expressions (parsing).

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    An additional problem

    http://find/
  • 7/29/2019 escola de verao - unicamp - aritmetica - fidel

    74/74

    Bases (SARC08): Analyze in which bases an expression such as10000 + 3 5 334 = 3 5000 + 10 + 0 is true.

    Things we have not covered:

    Polynomials (evaluation, operations, properties, etc.).Evaluation of mathematical expressions (parsing).

    Maybe next time ;-)

    Escola de Verao Maratona de Programacao Review on arithmetics for ICPC

    http://find/