Professional Documents
Culture Documents
http://blog.benoitblanchon.fr/ - v1.0
Algorithm design
Ask yourself before coding
log n
n log n
n2
n3
2n
n!
109
108
104
103
30
10
2.10
Long
9
9. 10
BigInteger
18
doubles
308
10
decimal
28
10
, 15
digits
digits
Mathematics
Quadratic equation
ax 2+ bx+ c=0
=b2 4 ac
x=
b
2a
Number of pairs
, 28
With repeated
elements:
Cnk =
n!
k !(nk )!
C2n =
n(n1)
2
kn =Ckn +k1
Number of permutations
n
A n=n!
Probability
A and B
P ( A B ) =P ( B| A ) . P (A )
A or B
A knowing B (Bayes
P ( A B )=P ( A ) + P ( B )P( A theorem)
B)
P ( B| A ) P( A)
P ( A|B )=
P( B)
A
=e ln A ln B
B
Prime numbers
function gcd(a, b)
while b 0
t := b
b := a mod b
a := t
return a
Stuck?
1. Read the problem again, make sure you leverage every piece of information
(including the limits, the problem size and the statistics)
2. Try to solve by hand
3. Watch carefully the results and look for a pattern
4. Still nothing? Keep 30 to 60 minutes for the brute force implementation.