Escolar Documentos
Profissional Documentos
Cultura Documentos
1a100
100 a
2
|
=
1a50
100 2a
2
| +
1a50
100 (2a 1)
2
|
=
1a50
100 2a
2
+
1a50
100 2a
2
= 2
1a50
(50 a)
= 2450.
Problem 5
(1) Let p be a prime and n 1. Show that the largest power of p that divides n!
is precisely (the sum is actually nite since beyond a point, all terms are zero):
n
p
| +
n
p
2
| +
n
p
3
| + . . .
(2)When 1000! is written as a decimal integer, how many zeroes does it have
at the end?
Proof. (1) The largest power of p that divides n! is the same as that in
1in
i =
1in,p|n
i =
1in/p
pi = p
n/p
n/p|!,
Repeat this precedure, we obtain the desired conclusion.
(2) The number of zeros is equal to the largest power of 10 that divides
1000!, which is the minimum between the largest power of 2 and 5 that divides
1000!. Since
1000
5
| +
1000
5
2
| +
1000
5
3
| + . . . = 249,
there are 249 zeros in 1000!.
Problem 6
An interval [a, b] is the set of all real numbers between (and including) a and b.
Given n intervals,
[a
1
, b
1
], [a
2
, b
2
], . . . , [a
n
, b
n
],
design an O(nlog n) time algorithm to decide whether there exists a pair of
intervals that overlap (i.e. share a point).
Proof. First, sort the intervals by the left end point in time O(nlog n) and
denote n intervals by [a
i
, b
i
]. Calculate values M
i
= max
ji
b
i
in time O(n).
Its easy to there exists overlap if and only if [a
i
, M
i
] [a
i+1
, b
i+1
] ,= for some
1 i n 1, which can also be determined in O(n) time.
2
Problem 6
Given a set of m(m1) +1 distinct integers A = a
1
, a
2
, ..., a
m(m1)
+1, show
that there exists a subset B A such that
[B[ = m.
The average of integers in B is also an integer.
Proof. There are only m distinct residues mod m and m(m+1)+1 integers. By
pigeonhole principle, there are
m(m1)+1
m
| = m integers with the same residu
mod m, and their sum is divisible by m.
Challenge Problem 1
Assuming that only equality checks are allowed, design an O(n) time algorithm
to check if there is an element which occurs more than n/2 times in an array
containing n elements. Note that the elements are not necessarily integers and
the only operation allowed is checking whether two elements are equal.
Proof. Assume occurs more than n/2 times in set A. The key observation is
that, if will still be occur more than (n 2)/2 times if two dierent elements
in A are removed. The rest is left to the reader.
Challenge Problem 2
Given a mn matrix of integers such that every row is strictly increasing (from
left to right), and every column is strictly increasing (from top to bottom),
design an O(m + n) time algorithm to test if a given integer b is contained in
the matrix.
Proof. Denote by M(i, j) the element in the row i and column j. Scan the rst
row from right to left, and stop at the rst element which is b, say M(1, c
1
).
If M(1, c
1
) = b, we have done. Otherwise, move to the second row, and scan
from the M(2, c
1
), and move left until we meet the rst element which is b,
say M(2, c
2
). If M(2, c
2
) = b, we have done. Otherwise, move to the third row
and repeat above precedure.
The correctness and the time complexity analysis are left to the reader.
3