Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1. Coast Guard
Problem Description
Of late, smuggling has increased many fold and as Chief of the Coast Guard,
you are responsible for intercepting the smuggling vessels and nullify them.
You have stationed several smart, high speed boats in the sea. The entire sea
under your control can be divided into a rectangular grid of 1 km by 1 km
squares. Due to a design flaw in the boats, they can move only in horizontal
or vertical directions (EW or NS) (they cannot move diagonally, in
particular). Of course, no two boats are stationed in the same grid square.
squares can be reached in equal (minimum) time by both boats, and are
hence uncontrolled.
Given the position of the boats, write a program to identify the number of
grid squares that are uncontrolled.
Constraints
0<M,N<50, 1<k<10
Input Format
The first line will be three comma separated integers M, N and k. M and N
give the number of rows and columns of the grid, and k the number of
boats.
The next k lines will a pair of comma separated numbers giving the
coordinates of the grid square with a boat
Output
Example 1
Input
3,4,2
2,0
0,2
Output
Explanation
M=3,N=4,k=2. There are 3 rows and 4 columns. There are 2 boats at (2,0)
and (0,2).
The position is the same as in the earlier figure. There are 4 uncontrolled
squares. Hence the result is 4.
Example 2
Input
2,4,2
0,1
2,0
Output
Explanation
M=2, N=4, k=2. There are two boats positioned as below
It can be seen that there is no grid square that is reached at the same
minimum time from the two boats. Hence the result is 0.
For some algorithms, it is important to find out the square free numbers that
divide a number. Note that 1 is not considered a square free number.
In this problem, you are asked to write a program to find the number of square
free numbers that divide a given number.
Constraints
N < 10^9
Input Format
The only line of the input is a single integer N which is divisible by no prime
number larger than 19
Output
One line containing an integer that gives the number of square free numbers
(not including 1)
Explanation
Example 1
Input
20
Output
3
Explanation
N=20
1, 2, 4, 5, 10, 20
Example 2
Input
72
Output
3
Explanation
N=72. The numbers that divide 72 are
1 is not considered square free. 4, 9 and 36 are perfect squares, and 8,12,18,24
and 72 are divisible by one of the. Hence only
2, 3 and 6 are square free. (It is easily seen that none of them are divisible by a
perfect square). The result is 3
3.Spiral Primes
Problem Description
The prime numbers are written in a spiral form staring at (0,0) and moving as
shown in the diagram below.
.
Constraints
N<=20
Each output prime < 1000000
-130< x,y <130
Input Format
The first line has an integer N that specifies the number of coordinates in this
test case
The next N lines each have a pair of comma separated integers, which are the x
and y coordinates of the position
Output
The output consists of N lines.
Each consists of an integer specifying the prime at the corresponding position.
Explanation
Example 1
Input
2
1,0
0,1
Output
3
7
Explanation
N=2. There are 2 sets of coordinates in this test case. The coordinates are (1,0)
and (0,1).. The corresponding primes in the spiral are 3 and 7. The output hence
has these.
Example 2
Input
1,1
-1,1
-1,0
Output
11
13
Explanation
There are 3 sets of coordinates in this test case (N=3). The coordinates are
(1,1),(-1,1) and (-1,0). The corresponding primes at these positions are 5, 11,
13. Hence the output has these in 3 lines.
4. N-pod Numbers
Problem Description
Consider the natural numbers 1, 2, 4. Using these numbers alone or in
combinations (as sums), one can realize the fo
1
2
3=1+2
4
5=1+4
6=2+4
7=1+2+4
That's all!
Let us call 1, 2 and 4 as the "seeds" and the generated numbers 1 through 7 as
the "plants".
Let us call a seed-list as an "N-pod" if its seeds are distinct and it generates
plants 1 through N.
19-Pod-1 = {1, 2, 3, 4, 9}
19-Pod-2 = {1, 2, 3, 5, 8}
19-Pod-3 = {1, 2, 3, 6, 7}
19-Pod-4 = {1, 2, 4, 5, 7}
Note that the largest seed ("max-seed") in both 19-Pod-3 and 19-Pod-4 is 7,
which is smaller than the max-seeds of
Focusing on the 19-Pods that have max-seeds 7. let us calculate the medians
of the seeds:
Median-19-Pod-3 = Median(1, 2, 3, 6, 7) = 3
Median-19-Pod-4 = Median(1, 2, 4, 5, 7) = 4
When number of seeds in pod is even then median has to be calculated as the
average of middle two numbers.
Example 1: Input: 7
Output: {1,2,4}
Example 2:
Input: 32
Output: {1,2,3,7,9,10}OR{1,2,4,6,9,10}
5.Hop Game
Problem Description
Dr Felix Kline, the Math teacher at Gauss School introduced the following
game to teach his students problem solving. He places a series of “hopping
stones” (pieces of paper) in a line with points (a positive number) marked on
each of the stones.
Students start from one end and hop to the other end. One can step on a stone
and add the number on the stone to their cumulative score or jump over a stone
and land on the next stone. In this case, they get twice the points marked on the
stone they land but do not get the points marked on the stone they jumped over.
At most once in the journey, the student is allowed (if they choose) to do a
double jump that is, they jump over two consecutive stones - where they would
get three times the points of the stone they land on, but not the points of the
stone they jump over.
The teacher expected his students to do some thinking and come up with a plan
to get the maximum score possible. Given the numbers on the sequence of
stones, write a program to determine the maximum score possible.
Constraints
Input Format
The first line contains N, the number of integers (this is a positive integer)
The next line contains the N points (each a positive integer) separated by
commas. These are the points on the stones in the order the stones are placed.
Output
One integer representing the maximum score
Explanation
Example 1
Input
3
4,2,3
Output
10
Explanation
There are 3 stones (N=3), and the points (in the order laid out) are 4,2 and
3 respectively.
If we step on the first stone and skip the second to get 4 + 2 x 3 = 10. A
double jump to the third stone will get only 9. Hence the result is 10, and
the double jump is not used
Example 2
Input
4,5,6,7,4,5
Output
35
Explanation
N=6, and the sequence of points is given. One way of getting 35 is to start
with a double jump to stone 3 (3 x 6=18), go to stone 4 (7) and jump to stone
6 (10 points) for a total of 35. The double jump was used only once, and the
result is 35.
6. Sorting Boxes
Problem Description
The parcel section of the Head Post Office is in a mess. The parcels that
need to be loaded to the vans have been lined up in a row in an arbitr order
of weights. The Head Post Master wants them to be sorted in the increasing
order of the weights of the parcels, with one exception. wants the heaviest
(and presumably the most valuable) parcel kept nearest his office.
You and your friend try to sort these boxes and you decide to sort them by
interchanging two boxes at a time. Such an interchange needs eff equals to
the product of the weights of the two boxes.
Constraints
N<=50
Input Format
The first line consists of two space separated positive integers giving the
number of boxes (N) and the position of the Head Post Master's offi (k)
where the heaviest box must be.
The second line consists of N space separated positive integers giving the
weights of the boxes. You may assume that no two weights are equal
Output
The output is one line giving the total effort taken to get the boxes in sorted
order, and the heaviest in position
Explanation
Example 1
Input
52
20 50 30 80 70
Output
3600
Explanation
There are 5 boxes (N=5) and the heaviest box must be in position 2 (k=2).
If we look at the final order (sorted, with the heaviest at position 2) should
be 20 80 30 50 70. If we look at this, we notice that only the 50 and the 80
parcels need to be exchanged. As this takes effort of the produ of the
weights, the effort is 4000.
50 80 30 20 70
20 80 30 50 70
Example 2
Input
63
30 20 40 80 70 60
Output
7600
Explanation
There are 6 parcels, and the heaviest should be at position 3. Hence the final order
needs to be 20 30 80 40 60 70. If we look at the initial positi we see that 20 and
30 need to be exchanged (effort 600), 40 and 80 need to be exchanged (effort
3200) and 60 and 70 need to be exchang (effort 4200). Hence the total effort is
600+3200+4200=8000.
(3200) 20 30 80 40 70 60
(1200) 60 30 80 40 70 20
(1400) 60 30 80 40 20 70
(1200) 20 30 80 40 60 70
A total effort of 7600 is obtained rather than an effort of 8000, which is the
output.