## Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

PROGRAMMING CONTESTS

Jaehyun Park

Today’s Lecture

Algebra

Number Theory

Combinatorics

(non-computational) Geometry

Emphasis on “how to compute”

Sum of Powers

2

=1

=

1

6

( +1)(2 +1)

3

=

2

=

1

2

+1

2

Pretty useful in many random situations

Memorize above!

Fast Exponentiation

=

1, if = 0

, if = 1

/2

2

, if is even

(−1)/2

2

∙ , if is odd

Can be computed recursively

Implementation (recursive)

double pow(double a, int n) {

if(n == 0) return 1;

if(n == 1) return a;

double t = pow(a, n/2);

return t * t * pow(a, n%2);

}

Running time: log

Implementation (non-recursive)

double pow(double a, int n) {

double ret = 1;

while(n) {

if(n%2 == 1) ret *= a;

a *= a; n /= 2;

}

return ret;

}

You should understand how it works

Linear Algebra

Gaussian Elimination

Solve a system of linear equations

Invert a matrix

Find the rank of a matrix

Compute the determinant of a matrix

Cramer’s Rule

Greatest Common Divisor (GCD)

gcd (, ): greatest integer divides both and

Used very frequently in number theoretical

problems

Some facts:

gcd , = gcd (, −)

gcd , 0 =

gcd (, ) is the smallest positive number in { +

| , ∈ }

Euclidean Algorithm

Repeated use of gcd , = gcd (, −)

gcd 1989, 867

= gcd 1989 −2 × 867, 867

= gcd 255, 867

= gcd 255, 867 −3 × 255

= gcd 255, 102

= gcd 255 −2 × 102, 102

= gcd 51, 102

= gcd 51, 102 −2 × 51

= gcd 51, 0

= 51

Implementation

int gcd(int a, int b) {

while(b){int r = a % b; a = b; b = r;}

return a;

}

Running time: log +

Be careful: a % b follows the sign of a

5 % 3 == 2

-5 % 3 == -2

Congruence & Modulo Operation

≡ (mod ) means and have the same

remainder when divided by

Multiplicative inverse

−1

is the inverse of modulo if

−1

≡ 1 (mod )

5

−1

≡ 3 (mod 7) because 5 ⋅ 3 ≡ 15 ≡ 1 (mod 7)

May not exist (e.g. Inverse of 2 mod 4)

Exists iff gcd , = 1

Multiplicative Inverse

All intermediate numbers computed by Euclidean

algorithm are integer combinations of and

Therefore, gcd , = + for some integers ,

If gcd , = 1, then + = 1 for some ,

Taking modulo gives ≡ 1 (mod )

We will be done if we can find such and

Extended Euclidean Algorithm

Main idea: keep the original algorithm, but write all

intermediate numbers as integer combinations of

and

Exercise: implementation!

Chinese Remainder Theorem

Given , , , such that and are coprime

Find such that ≡ mod , ≡ mod

Solution:

Let

−1

be the inverse of modulo

Let

−1

be the inverse of modulo

Set =

−1

+

−1

(check this yourself)

Extension: solving for more simultaneous equations

Binomial Coefficients

**is the number of ways to choose objects out
**

of distinguishable objects

or, the coefficient of

−

in the expansion of

+

Appears everywhere in combinatorics

Computing

Solution 1: Compute using the following formula

=

( −1) ⋯( − +1)

!

Solution 2: Use Pascal’s triangle

Case 1: Both and are small

Use either solution

Case 2: is big, but or − is small

Use Solution 1 (carefully)

Fibonacci sequence

Definition:

0

= 0,

1

= 1

=

−1

+

−2

, where ≥ 2

Appears in many different contexts

Closed Form

=

−

5

, where =

1+ 5

2

Bad because and 5 are irrational

Cannot compute the exact value of

for large

There is a more stable way to compute

** … and any other recurrence of a similar form
**

Better Closed Form

+1

=

1 1

1 0

−1

=

1 1

1 0

1

0

Use fast exponentiation to compute the matrix

power

Can be extended to support any linear recurrence

with constant coefficients

Geometry

In theory: not that hard

In programming contests: avoid if you can…

Will cover basic stuff today

Computational geometry in week 9

When Solving Geometry Problems

Precision, precision, precision!

If possible, don’t use floating-point numbers

If you have to, always use double and never use float

Avoid division whenever possible

Introduce small constant in (in)equality tests

e.g. Instead of if(x == 0), write if(abs(x) < EPS)

No hacks!

In most cases, randomization, probabilistic methods,

small perturbations won’t help

2D Vector Operations

Have a vector (, )

Norm (distance from the origin):

2

+

2

Counterclockwise rotation by :

Left-multiply by

cos −sin

sin cos

Make sure to use correct units (degrees, radians)

Normal vectors: (, −), −,

Memorize all of them!

Line-Line Intersection

Have two lines: + + = 0, + + = 0

Write in matrix form:

= −

** Invert the matrix on the left using the following:
**

−1

=

1

−

−

−

Memorize this!

Edge case: =

Circumcircle of a Triangle

Have three points , ,

Want to compute that is equidistance from , ,

Don’t try to solve the system of quadratic equations!

Instead, do the following:

Find the bisectors of and

Compute their intersection

Area of a Triangle

Have three points , ,

Use cross product: 2 ∙ = − × −

Cross product:

1

,

1

×

2

,

2

=

1

2

1

2

=

1

2

−

2

1

Very important in computational geometry. Memorize!

Area of a Simple Polygon

Given

1

,

2

, …,

**around perimeter of polygon
**

If is convex, we can decompose into triangles:

2 ∙ =

+1

−

1

×

−

1

−1

=2

It turns out that the formula above works for non-

convex polygons too

Area is the absolute value of the sum of “signed area”

Alternative formula:

2 ∙ = (

+1

−

+1

)

=1

Conclusion

No need to look for one-line closed form solutions

Multi-step algorithms are good enough

This is a CS class, after all

Have fun with the exercise problems

… and come to the practice contest if you can!

- 5 SD-Fix (Repaired)lizanova
- Functions.docxGoutham Yeluri
- FunctionsGoutham Yeluri
- Mupad User Manualtemptempmememe
- Pure Maths Wilson's TheoremVamsi Karthik
- Number TheoryMyra O Valin
- 307UnNotes.pdfAnurup Sinha
- Assignment 5 Icp Hassaan Javaied 023roxter_15
- high school mathematics420
- Highest Commom Factor (HCF) and Least Common Multiple (LCM)Abhi Ram
- Q1Sunil Dhaka
- Jones TheTheoryOfNumbers TextLivardy Wufianto
- Hp 300s ManualDavid Wang
- gcdraul
- Factoring IntegersAli QasimPouri
- An Integral of Dedekind Eta-functions in Ramanujan's Lost Notebookapi-26401608
- Lesson 1 IntegersStandardFlux
- MATH140_ONLINE_ASSIGNMENT_2017.docxElsa Titastic
- banaagapi-245030597
- RSAthh42th
- Vedic Multiplicationapi-3771640
- Bipolar Neutrosophic Planar GraphsMia Amalia
- Sen10cNir Infosys
- DivConq01kunjan_mehta
- Dr Taan ElAli Curriculum Vita (CV)Taan ElAli
- Hl 2.10 Polynomials - Inequalities_solutionsKimwnas ⑬ Karageorgiou
- Indices & Standard Form F2 2013Juan Chee Wong
- PigeonHoleTing Sie Kim
- 20131119171506Harun Musić
- Surds and IndicesAbhishek Pandey

- UT Dallas Syllabus for ecs2305.003.10f taught by Charles Bernardin (cpb021000)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for se3306.001 06f taught by Gopal Gupta (gupta)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for te3307.001.08f taught by Charles Bernardin (cpb021000)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for cs5333.501 05f taught by Timothy Farage (tfarage)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for cs2305.501.06f taught by Timothy Farage (tfarage)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for cs5333.501 06s taught by Nancy Van Ness (nancyvn)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for cs3305.501 05f taught by Timothy Farage (tfarage)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for cs5333.001.09f taught by Jorge Cobb (jcobb)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for cs5333.001 06f taught by Nancy Van Ness (nancyvn)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for te3307.001 05f taught by Charles Bernardin (cpb021000)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for ecs2305.003.10f taught by Charles Bernardin (cpb021000)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for cs2305.001 05s taught by (wxw021000)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for math5320.501 05f taught by Thomas Butts (tbutts)UT Dallas Provost's Technology Group
- tmp1204.tmpFrontiers
- UT Dallas Syllabus for math5320.501.07f taught by Thomas Butts (tbutts)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for mthe5324.501.11f taught by Titu Andreescu (txa051000)UT Dallas Provost's Technology Group
- UT Dallas Syllabus for mthe5320.501.09f taught by Titu Andreescu (txa051000)UT Dallas Provost's Technology Group

- Introprogramming-Tutorial Sheets-Week 4 Tut 2i_khandelwal
- AFT's 2-Days Workshop - The Wall Street Club BITS Pilanii_khandelwal
- Lecture 5- Electronicsi_khandelwal
- Time Series Classificationi_khandelwal
- Value Points for Women Empowermenti_khandelwal
- MID SEM 2012-13i_khandelwal
- Computer Mediated Communication Analysisi_khandelwal
- FFT Algorithmsi_khandelwal
- Assignment 1 Marksi_khandelwal
- Introprogramming-Tutorial Sheets-Week 4 Tut 1i_khandelwal
- IIT Delhi Internship _ Internshalai_khandelwal
- The Best Way to Learn Ja...t - Tuts+ Code Tutoriali_khandelwal
- Report Writingi_khandelwal
- Answers - Ch. 3i_khandelwal
- Chelsea Wolfei_khandelwal
- Introprogramming-Tutorial Sheets-Week 1 Tut 2i_khandelwal
- 2013 Batch Resultsi_khandelwal
- Stocks 101i_khandelwal
- Batch List WKND B Lot - Copy (2)i_khandelwal
- TimeTable.pdfi_khandelwal
- Batch_113 Mark 6 Novi_khandelwal
- Introduction to Websim 20130905i_khandelwal
- Chapters Physics Basic tics 8i_khandelwal
- 1i_khandelwal

Sign up to vote on this title

UsefulNot usefulRead Free for 30 Days

Cancel anytime.

Close Dialog## Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

Loading