Algorithms and Olympic Programming (1)
Algorithms and Olympic Programming (1)
The Euclidean algorithm is a way to find the greatest common divisor of two positive integers. GCD of
two numbers is the largest number that divides both of them. A simple way to find GCD is to factorize
both numbers and multiply common prime factors.
● If we subtract a smaller number from a larger one (we reduce a larger number), GCD doesn’t
change. So if we keep subtracting repeatedly the larger of two, we end up with GCD.
● Now instead of subtraction, if we divide the larger number, the algorithm stops when we find
the remainder 0.
def find_gcd(numbers):
result = numbers[0]
for num in numbers[1:]:
result = gcd(result, num)
return result
nums = list(map(int,input().split()))
print("GCD:", find_gcd(nums))
10 5 25 35 40
GCD: 5
Dynamic Programming or DP
Dynamic Programming is an algorithmic technique with the following properties.
● It is mainly an optimization over plain recursion. Wherever we see a recursive solution that
has repeated calls for the same inputs, we can optimize it using Dynamic Programming.
● The idea is to simply store the results of subproblems so that we do not have to re-compute
them when needed later. This simple optimization typically reduces time complexities from
exponential to polynomial.
● Some popular problems solved using Dynamic Programming are Fibonacci Numbers, Diff
Utility (Longest Common Subsequence), Bellman–Ford Shortest Path, Floyd Warshall, Edit
Distance and Matrix Chain Multiplication.
Dynamic Programming is a commonly used algorithmic technique used to optimize recursive solutions
when the same subproblems are called again.
● The core idea behind DP is to store solutions to subproblems so that each is solved only once.
● To solve DP problems, we first write a recursive solution in a way that there are overlapping
subproblems in the recursion tree (the recursive function is called with the same parameters
multiple times)
● To make sure that a recursive value is computed only once (to improve time taken by
algorithm), we store results of the recursive calls.
● There are two ways to store the results, one is top down (or memoization) and other is bottom
up (or tabulation).
#1.
Representation of numbers (Euclidean)
Given a natural number N. It is required to represent it as the sum of two natural numbers A and B such
that the GCD (the largest common divisor) of the numbers A and B is maximal.
Input data
9
The input file contains a natural number N (2≤N≤10 )
Output data
In the output file, print the two required numbers A and B. If there are several solutions, print any of
them.
№ INPUT.TXT OUTPUT.TXT
1 15 5 10
2 16 88
#2.
Cutting into squares(Euclidean)
The strip of paper has dimensions A × B. Each time, a square of the maximum size is cut off from it until
a square is formed. How many squares will you get?
Input data
9
The program is given the numbers A and B (1 ≤ A, B ≤10 ).
Output data
№ INPUT.TXT OUTPUT.TXT
1 15 3 5
2 12 8 3
3 55 1
#3.
A ball on the stairs
At the top of the ladder, which contains N steps, there is a ball that starts jumping down them to the base.
The ball can jump to the next step, one step later or two steps later. That is, if the ball is on the 8th step,
then it can move to the 5th, 6th or 7th.
It is required to write a program that will determine the number of possible "routes" of the ball from the
top to the ground.
Input data
Output data
№ INPUT.TXT OUTPUT.TXT
1 1 1
2 4 7
#4.
Bunny
A rabbit has appeared in our zoo. He was placed in a cage, and so that he
would not be bored, the zoo director ordered a ladder to be put in his cage.
Now our bunny can jump up the ladder, jumping over the steps. The staircase
has a certain number of steps. A hare can climb no more than K steps in one
jump. For a change, the bunny tries every time to find a new way to the top of the ladder. The director is
curious about how many different ways the hare has to get to the top of the ladder given the values of K
and N. Help the director write a program that will help calculate this number. For example, if K=3 and
N=4, then there are the following routes: 1+1+1+1, 1+1+2, 1+2+1, 2+1+1, 2+2, 1+3, 3+1. That is, for
these values, y There are only 7 different routes for the hare to get to the top of the stairs.
Input data
In the single line of the input file INPUT.TXT two natural numbers K and N are written (1 ≤ K ≤ N ≤
300). K is the maximum number of steps that a hare can climb in one jump, N is the total number of
stairs.
Output data
In a single line of the output file OUTPUT.TXT it is necessary to display the number of possible options
for different routes of the hare to the top rung of the ladder without leading zeros.
№ INPUT.TXT OUTPUT.TXT
1 13 1
2 27 21
3 3 10 274
#5.
Knight's move
The Chess Association decided to equip all its employees with phone numbers that
could be dialed on a push-button phone with a knight's move. For example, the
number 340-49-27 is dialed by the knight's move. In this case, the phone number
cannot begin with either the number 0 or the number 8.
Input data
Output data
To the output file OUTPUT.TXT output the required number of phone numbers.
№ INPUT.TXT OUTPUT.TXT
1 1 8
2 2 16