35
Basic Division Problem 1: Prime Palindromes, (K Narayan Kumar, CMI) An integer is said to be a palindrome if it is equal to its reverse. For example, 79197 and 324423 are palindromes. In this task you will be given an integer N, 1 ≤ N ≤ 1000000. You must find the smallest integer M ≥ N such that M is a prime number and M is a palindrome. For example, if N is 31 then the answer is 101. Input format A single integer N, (1 ≤ N ≤ 1000000), on a single line. Output format Your output must consist of a single integer, the smallest prime palindrome greater than or equal to N. Example We now illustrate the input and output formats using the example described above. Sample input: 31 Sample output: 101 Basic Division Problem 2: The Lead Game, (K Narayan Kumar, CMI) The game of billiards involves two players knocking 3 balls around on a green baize table. Well, there is more to it, but for our purposes this is sufficient.

Indian Olympiad Basic

Embed Size (px)

Citation preview

Page 1: Indian Olympiad Basic

Basic Division

Problem 1: Prime Palindromes, (K Narayan Kumar, CMI)

An integer is said to be a palindrome if it is equal to its reverse. For example, 79197 and 324423 are palindromes. In this task you will be given an integer N, 1 ≤ N ≤ 1000000. You must find the smallest integer M ≥ N such that M is a prime number and M is a palindrome.

For example, if N is 31 then the answer is 101.

Input format

A single integer N, (1 ≤ N ≤ 1000000), on a single line.

Output format

Your output must consist of a single integer, the smallest prime palindrome greater than or equal to N.

Example

We now illustrate the input and output formats using the example described above.

Sample input:

31

Sample output:

101

Basic Division

Problem 2: The Lead Game, (K Narayan Kumar, CMI)

The game of billiards involves two players knocking 3 balls around on a green baize table. Well, there is more to it, but for our purposes this is sufficient.

The game consists of several rounds and in each round both players obtain a score, based on how well they played. Once all the rounds have been played, the total score of each player is determined by adding up the scores in all the rounds and the player with the higher total score is declared the winner.

The Siruseri Sports Club organises an annual billiards game where the top two players of Siruseri play against each other. The Manager of Siruseri Sports Club decided to add his own twist to the game by changing the rules for determining the winner. In his version, at the end of

Page 2: Indian Olympiad Basic

each round the leader and her current lead are calculated. Once all the rounds are over the player who had the maximum lead at the end of any round in the game is declared the winner.

Consider the following score sheet for a game with 5 rounds:

Round Player 1 Player 2

1 140 82

2 89 134

3 90 110

4 112 106

5 88 90

The total scores of both players, the leader and the lead after each round for this game is given below:

Round Player 1 Player 2 Leader Lead

1 140 82 Player 1 58

2 229 216 Player 1 13

3 319 326 Player 2 7

4 431 432 Player 2 1

5 519 522 Player 2 3

The winner of this game is Player 1 as he had the maximum lead (58 at the end of round 1) during the game.

Your task is to help the Manager find the winner and the winning lead. You may assume that the scores will be such that there will always be a single winner. That is, there are no ties.

Input format

The first line of the input will contain a single integer N (N ≤ 10000) indicating the number of rounds in the game. Lines 2,3,...,N+1 describe the scores of the two players in the N rounds. Line i+1 contains two integer Si and Ti, the scores of the Player 1 and 2 respectively, in round i. You may assume that 1 ≤ Si ≤ 1000 and 1 ≤ Ti ≤ 1000.

Output format

Page 3: Indian Olympiad Basic

Your output must consist of a single line containing two integers W and L, where W is 1 or 2 and indicates the winner and L is the maximum lead attained by the winner.

Test Data

The range of values over which your program is to be tested is mentioned above. In addition, 50% of the test cases will also satisfy N ≤ 1000.

Example

We now illustrate the input and output formats using the example described above.

Sample input:

5

140 82

89 134

90 110

112 106

88 90

Sample output:

1 58

Basic Division

Problem 1: BCCS Elections, (K Narayan Kumar, CMI)

The Board of Cricket Control of Siruseri holds an election every year to elect a 3 member executive committee to run the affairs of the board. The executive committee is quite powerful, as it decides which television company gets to broadcast the Siruseri Cricket League and so on. The procedure for this election is the following. All those interested in serving as executive committee members inform the Manager. The Manager informs all the members of the BCCS about the list of candidates. On the day of the election, each member of BCCS is allowed to vote for exactly one of the candidates. Finally all the votes are counted and the top three candidates are deemed elected.

Suppose there are 5 candidates {1,2,3,4,5}, 10 members in all in BCCS and the votes cast are as follows:

Voter Vote

Page 4: Indian Olympiad Basic

1 2

2 5

3 4

4 4

5 3

6 1

7 2

8 1

9 1

10 2

Then, candidates 1, 2 (with 3 votes each) and 4 (with 2 votes) are elected to the executive committee.

You are given the number of candidates and voters and the list of votes. Your task to determine the candidate finishing third. You may assume that the votes are cast in such a way that there is never a tie between the second and third best candidates or between the third and fourth best candidates.

Input format

The first line of the input consists of two integers C and N, where C is the number of candidates and N is the number of voters. The next N lines, lines 2,..., N+1, provide information on the votes. Line i+1, 1 ≤ i ≤ N, contains a single integer Vi, 1 ≤ Vi ≤ C, indicating the choice of voter i.

Output format

The output must consist of a single line with a single integer, indicating the identity of the candidate finishing third.

Test data

You may assume that 3 ≤ N ≤ 100000 and 3 ≤ C ≤ 10000.

Example

We now illustrate the input and output formats using the example described above.

Sample input:

Page 5: Indian Olympiad Basic

5 10

2

5

4

4

3

1

2

1

1

2

Sample output:

4

Basic Division

Problem 2: A Board Game, (K Narayan Kumar, CMI)

In this task you have to determine the outcome of a board game. The board on which the game is played has N places labelled 1,2,...,N. Each position is marked with either a + or a -. The position 1 is always marked with a +.

A token is initially placed at position 1. The game proceeds as follows: In each round, a single die is thrown. Let the current position of the token be j and let the die show d. The new position of the token is determined as follows:

If the position j is labelled + and if j+d ≤ N then the token moves to j+d, otherwise it remains at j.

If the position j is labelled - and if j-d ≥ 1 then the token moves to j-d, otherwise it remains at j

The score at the end of the game is the number of times the token visits the position 1.

For example if the board looks like

1 2 3 4 5 6 7 8 9 10 11 12

+ + - + - - + - - + - -

Page 6: Indian Olympiad Basic

and the sequence of throws of the die is 1,1,2,3,2,6,3,2,6,2 then the sequence of positions visited by the token is 1 2 3 1 4 6 6 3 1 7 9 and the resulting score is 3.

You are given the description of the board and a sequence of throws of the die. Your aim is to determine the score at the end of the game.

Input format

The first line contains two integers N and M where N is the number of positions on the board and M is the number of throws of the die. Lines 2,...,N+1 contain a + or a -. Line i+1, 1 ≤ i ≤ N, indicates whether position i is marked with a + or a -. The next M lines, lines N+2,..., N+1+M, each contain a single integer d indicating a throw of the die, with 1 ≤ d ≤ 6.

Output format

A single integer indicating the score.

Test Data

You may assume 7 ≤ N ≤ 10000 and 1 ≤ M ≤ 10000.

Example

We now illustrate the input and output formats using the example described above.

Sample input:

12 10

+

+

-

+

-

-

+

-

-

+

-

Page 7: Indian Olympiad Basic

-

1

1

2

3

2

6

3

2

6

2

Sample output:

3

Basic Division

Problem 1: The Lazy Circus Gymnast, (K Narayan Kumar, CMI)

One of the highlights of the shows of the Ennui Circus Company is the performance by Palli, "The Incredible Lizard Man". As his stage name suggests, Palli can climb up and down any vertical surface - trees, walls, ropes, ...

For his performance, a set of smooth steel cylinders of varying heights are arranged in a line. Palli then climbs up to the top of each of these cylinders. To save himself some work (and to make the show interesting) he does not climb all the way down from a cylinder to move to the next. If the next cylinder is taller than the present one, he simply jumps across horizontally from the top of the current cylinder, latches onto the side of the next one and climbs up from there. If the next cylinder is shorter than the present one, he comes down from the top of the current cylinder till he is at the same level as the top of the next one and then jumps across horizontally.

Palli is very lazy and he would like to minimize the amount of climbing up and down that he has to do during his show. He has a limited choice in this matter because the circus owner has already purchased the set of cylinders to be used. All that Palli can do is to rearrange these cylinders in any sequence he wants. He would like to choose an arrangement that minimizes his work.

For example, suppose there are three cylinders C1, C2 and C3, of heights 4, 2 and 4 metres, respectively. If he chose to arrange them in the order C1 C2 C3 then he would have to climb 12

Page 8: Indian Olympiad Basic

metres (4 metres to reach the top of C1, descend 2 metres, jump to the top of C2, jump across to C3, climb 2 metres to reach the top of C3 and finally climb down 4 metres from the top of C3, adding up to 12 metres). Instead, if he were arrange these cylinders in the sequence C3 C1 C2 then he only needs to climb 8 metres.

Your task is to help him find an arrangment of the cylinders that minimizes the amount he has to climb up and down.

Input format

The first line of the input consists of an integer N indicating number of cylinders. The next N lines (i.e. lines 2,..., N+1) provide information on the heights of the cylinders. Line i+1, (1 ≤ i ≤ N) contains a single integer hi, indicating the height of cylinder i.

Output format

The output must consist of N lines, each containing one integer ai (1 ≤ ai ≤ N), so that the sequence a1 a2 ... aN describes a optimal way of arranging the cylinders 1,...,N. If there is more than one optimal arrangement, it suffices to print any one.

Test data

You may assume that 3 ≤ N ≤ 5000.

Example

We now illustrate the input and output formats using the example described above.

Sample input:

3

4

2

4

Sample output:

3

1

2

Basic Division

Page 9: Indian Olympiad Basic

Problem 2: Find the Numbers, (K Narayan Kumar, CMI)

This is a rather simple problem to describe. You will be given three numbers S, P and k. Your task is to find if there are integers n1, n2,...,nk such that n1 + n2 +...+ nk = S, n1 * n2 * ... * nk = P. If such integers exist, print them out. If no such sequence of integers exist, then print "NO".

For example if S=11, P=48 and k=3 then 3, 4 and 4 is a solution. On the other hand, if S=11, P=100 and k=3, there is no solution and you should print "NO".

Input format

A single line with three integers S, P and k.

Output format

A single word "NO" or a seqence of k integers n1, n2,..., nk on a single line. (The ni's must add up to S and their product must be P).

Test data

You may assume that 1 ≤ k ≤ 4, 1 ≤ S ≤ 1000 and 1 ≤ P ≤ 1000.

Example

We now illustrate the input and output formats using some examples.

Sample input 1:

11 48 3

Sample output 1:

3 4 4

Sample input 2:

11 100 3

Sample output 2:

NO

Basic Division

Problem 1: Cycles in Permutations, (K Narayan Kumar, CMI)

Page 10: Indian Olympiad Basic

We consider permutations of the numbers 1,..., N for some N. By permutation we mean a rearrangment of the number 1,...,N. For example

2 4 5 1 7 6 3 8

is a permutation of 1,2,...,8. Of course,

1 2 3 4 5 6 7 8

is also a permutation of 1,2,...,8.

We can "walk around" a permutation in a interesting way and here is how it is done for the permutation above:

Start at position 1. At position 1 we have 2 and so we go to position 2. Here we find 4 and so we go to position 4. Here we find 1, which is a position that we have already visited. This completes the first part of our walk and we denote this walk by (1 2 4 1). Such a walk is called a cycle. An interesting property of such walks, that you may take for granted, is that the position we revisit will always be the one we started from!

We continue our walk by jumping to first unvisited position, in this case position 3 and continue in the same manner. This time we find 5 at position 3 and so we go to position 5 and find 7 and we go to position 7 and find 3 and thus we get the cycle (3 5 7 3). Next we start at position 6 and get (6 6) and finally we start at position 8 and get the cycle (8 8). We have exhausted all the positions. Our walk through this permutation consists of 4 cycles.

One can carry out this walk through any permutation and obtain a set of cycles as the result. Your task is to print out the cycles that result from walking through a given permutation.

Input format

The first line of the input is a positive integer N indicating the length of the permutation. The next line contains N integers and is a permutation of 1,2,...,N.

Output format

The first line of the output must contain a single integer k denoting the number of cycles in the permutation. Line 2 should describe the first cycle, line 3 the second cycle and so on and line k+1 should describe the kth cycle.

Test data

You may assume that N ≤ 1000.

Example

We now illustrate the input and output formats using the example described above.

Page 11: Indian Olympiad Basic

Sample input 1:

8

2 4 5 1 7 6 3 8

Sample output 1:

4

1 2 4 1

3 5 7 3

6 6

8 8

Sample input 2:

8

1 2 3 4 5 6 7 8

Sample output 2:

8

1 1

2 2

3 3

4 4

5 5

6 6

7 7

8 8

Basic Division

Problem 2: Sorting Rows of Numbers, (K Narayan Kumar, CMI)

You will be given several lines of input where each line contains a sequence of positive integers. Your task is to sort these lines in lexicographic order. Lexicographic order is the order in which words are listed in the dictionary. The ordering is determined by the left most element (in this case the left most integer on each line) and if the left most elements are equal then by the second

Page 12: Indian Olympiad Basic

element from the left, if the left most and the second element from the left are equal then by the third element from the left and so on.

For example, when the lines

14 38 11 89

27 34

27 12 34

27

92 2 3 1

17 2

are sorted in the lexicographic order we get

14 38 11 89

17 2

27

27 12 34

27 34

92 2 3 1

Input format

The first line of the input contains a single integer N indicating the number of lines of input. This is followed by N lines (lines 2 through N+1) each which consists of a sequence of positive integers followed by a single -1. (The -1 is there just as an end marker and is not to be used in the sorting). Every line contains at the most 50 numbers.

Output format

N lines each containing a sequence of integers. These N lines must be the lexicographically sorted presentation of the N input lines.

Test data

You may assume that N ≤ 1000. (Recall that there are at the most 50 integers on each line).

Example

We now illustrate the input and output formats using the above example:

Page 13: Indian Olympiad Basic

Sample input:

6

14 38 11 89 -1

27 34 -1

27 12 34 -1

27 -1

92 2 3 1 -1

17 2 -1

Sample output:

14 38 11 89

17 2

27

27 12 34

27 34

92 2 3 1

Basic Division

Problem 1: Electoral Rolls Revision, (K Narayan Kumar, CMI)

The Chief Electoral Officer of Siruseri has ordered a revision of the electoral rolls. Siruseri is divided into a number of Blocks and an officer is assigned to each block to enumerate the list of voters in the block. Siruseri is a model town and assigns a unique identification number (ID number) to each resident at the time of his birth and he has to quote this in all his dealings with the government. The list compiled by the officer is a listing of the ID numbers of all the voters in his block. This list is in ascending order of ID numbers.

Once he obtains the lists from all the blocks the CEO will combine the lists to get a listing of the ID numbers of all eligible voters in Siruseri. Your task is to help him compile this sorted list from the sorted lists he gets from the different blocks.

Here is an example with four blocks. The listing of the voters in the four blocks is as follows:

349 448 900

Page 14: Indian Olympiad Basic

23 1045 1149 9876

785 1002

456 998 1047 9023 9987

Then, the CEO would like to combine these to get the list:

23 349 448 456 785 900 998 1002 1045 1047 1149 9023 9876 9987

Input format

The first line of the input contains a single integer B indicating the number of blocks in Siruseri. The next B lines (lines 2, ..., B+1) contain the voters list from the different blocks. Line i+1 contains the voters list of block i. It consists of a sequence of integers. The first integer indicates the number of voters in Block i. This is followed by i integers, sorted in ascending order, giving the ID numbers of the voters in this block.

Output format

A sequence of integers, one in each line, giving the sorted sequence of ID numbers, in ascending order, of the voters in Siruseri.

Test data

You may assume that B ≤ 500 and that the number of voters in every block is not more than 1000. Further, you may assume that in 50% of the test inputs, B ≤ 60 and each block has at the most 100 voters.

Example

We now illustrate input and output formats using the example described above.

Sample input:

4

3 349 448 900

4 23 1045 1149 9876

2 785 1002

5 456 998 1047 9023 9987

Sample output:

23

349

Page 15: Indian Olympiad Basic

448

456

785

900

998

1002

1045

1047

1149

9023

9876

9987

Basic Division

Problem 2: The Medu Vada Maze, (K Narayan Kumar, CMI)

A maze, for our purposes, consists of a rectangular grid of cells, where some of the cells are blocked and the remaining cells are empty. A mouse is placed in one of the empty cells and a dosa is placed at a different empty cell. From a cell, the mouse can move to any empty neighbouring cell according to the rules given below. Your aim is to determine whether the mouse can walk through this maze and reach the dosa.

A maze with R rows and C columns will be presented as a sequence of R lines, each with C characters. The character # denotes a blocked cell and the character . denotes an empty cell). The mouse and the dosa sit in distinct empty cells and those cells are marked by M and D instead of .

Here is a maze with 6 rows and 12 columns.

####.#..####

.M.#..#..D.#

#.#...#....#

...#..#..#..

...#.#.###.#

.........###

Page 16: Indian Olympiad Basic

We will refer to a cell by its row and column position. The rows are numbered from top to bottom and the columns from left to right. For example, in the maze above, the mouse is initially placed at postion (2,2) indicating second row and second column and the dosa is at position (2,10) indicating second row and tenth column.

The mouse can move from a cell to the one above it, the one below it, the one to its left or the one to its right. Further, from a cell in the leftmost column the mouse can move to the cell in the same row in the rightmost column and from a cell in the rightmost column it can move to the cell in the same row in the leftmost column. Similarly, from a cell in the top row the mouse can move to the cell in the same column in the bottom row and from a cell in the bottom row it can move to the cell in the same column in the top row. Thus, in the maze above, the mouse can move from (4,1) to (4,12) and from (6,5) to (1,5) and so on.

You should think of the maze as having the shape of a solid ring, like a Medu Vada. It is as if the paper on which the Maze is drawn is rolled back and stuck at the edge so that the bottom row touches the top row. The resulting cylinder is rolled around so that the right and left columns, which now form circles, touch each other. Thus, the left most column and right most column touch each other, as do the top row and the bottom row.

In the example above, the mouse can reach the dosa by walking through the following sequence of cells: (2,2), (3,2), (4,2), (4,1), (4,12), (4,11), (3,11), (3,10) and (2,10). This path is marked with with x's below:

####.#..####

.M.#..#..D.#

#x#...#..xx#

xx.#..#..#xx

...#.#.###.#

.........###

Alternatively it can also take the following path:

####.#.x####

.M.#..#xxD.#

#x#...#....#

.x.#..#..#..

.x.#.#.###.#

.xxxxxxx.###

Your task is to determine whether it possible for the mouse to reach the dosa.

Page 17: Indian Olympiad Basic

Input format

The first line of the input will contain two numbers R and C denoting the number and rows and columns respectively. This is followed by R lines, each with C characters, each of which is # or . or M or D. There is exactly one M and one D in the input.

Output format

If there is no path from the mouse to the dosa print a single line containing the word NO. If there are paths from the mouse to the dosa, then the first line of the output should consist of the word YES. Following this must be R lines of C characters each, where each character is # or . or x or M or D, describing a path from M to D using the x's as illustrated above. There may be many paths and it suffices to describe one path.

Test Data

In all the inputs, R ≤ 1000 and C ≤ 1000. Further, in 80% of the inputs, 3 ≤ R ≤ 60 and 3 ≤ C ≤ 60.

Example

We now illustrate the input and output formats using some examples.

Sample input 1:

6 12

####.#..####

.M.#..#..D.#

#.#...#....#

...#..#..#..

...#.#.###.#

.........###

Sample output 1:

YES

####.#..####

.M.#..#..D.#

#x#...#..xx#

xx.#..#..#xx

Page 18: Indian Olympiad Basic

...#.#.###.#

.........###

Sample input 2:

4 6

##.#..

.M.#.#

#.#.D.

...#.#

Sample output 2:

NO

Basic Division

Problem 1: Base b Arithmetic, (K Narayan Kumar, CMI)

Traditionally numbers are written in base 10 ("decimal"). That is, every digit is a number between 0 and 9. We think of a number as its decimal representation. However, as you might know, numbers can be written in base b for any b > 0. In this case, every digit is a number between 0 and b-1. For instance in base 4 we may write 3312 or 30.

The value of a number written in base b is determined as follows: Suppose the given number in base b is dn-1 ... d0 where each di lies between 0 and b-1. This represents the number

d0 + d1 * b + d2 * b2 + ... + di * bi + ... + dn-1 * bn-1

We don't permit leading 0's in the representation. For instance, we cannot write 003312 or 03312 instead of 3312.

Given a number in decimal one can compute the base b representation by inverting the above computation.

It is easy to check that the base 4 representation 3312 denotes the decimal number 246 and the base 4 representation 30 denotes 12. Similarly, the base 12 representation 2 11 10 (where we use blank spaces to separate the digits) denotes the decimal number 430 while the base 12 representation 3 0 2 denotes the number 434.

You will be given b and the base b representation of two numbers A and B. Your task is to printout the base b representation of the product A ×B.

Page 19: Indian Olympiad Basic

For example the product of the base 4 numbers 3312 and 30 written in base 4 is 232020. Similarly, the product of the base 12 numbers 2 11 10 and 3 0 2 written in base 12 is 8 11 11 11 8.

Input format

The first line of the input contains 3 integers b, N and M, where b is the base. N and M are the number of digits in the representation (in base b) of the two given numbers.

The second line contains N space separated integers DN-1  DN-2  ... D0 giving the base b representation of the first number and the third line contains M space separated integers EM-1  EM-2

... E0 giving the base b representation of the second number.

Output format

The first line of the output should be a single integer L denoting the length of the base b representation of the product. The second line should contain L space separated integers giving the base b representation of the product.

Test data

You may assume that 1 ≤ N,M ≤ 1000.

Example

We now illustrate input and output formats using the examples described above.

Sample input 1:

4 4 2

3 3 1 2

3 0

Sample output 1:

6

2 3 2 0 2 0

Sample input 2:

12 3 3

2 11 10

3 0 2

Page 20: Indian Olympiad Basic

Sample output 2:

5

8 11 11 11 8

Basic Division

Problem 2: Discrepancies in the Voters List, (K Narayan Kumar, CMI)

As you might remember, the collector of Siruseri had ordered a complete revision of the Voters List. He knew that constructing the list of voters is a difficult task, prone to errors. Some voters may have been away on vacation, others may have moved during the enrollment and so on.

To be as accurate as possible, he entrusted the task to three different officials. Each of them was to independently record the list of voters and send it to the collector. In Siruseri, every one has a ID number and the list would only list the ID numbers of the voters and not their names. The officials were expected to arrange the ID numbers in ascending order in their lists.

On receiving the lists, the Collector realised that there were discrepancies - the three lists were not identical. He decided to go with the majority. That is, he decided to construct the final list including only those ID numbers that appeared in at least 2 out of the 3 lists. For example if the three lists were

23 30 42 57 90

21 23 35 57 90 92

21 23 30 57 90

then the final list compiled by the collector would be:

21 23 30 57 90

The ID numbers 35, 42 and 92 which appeared in only one list each do not figure in the final list.

Your task is to help the collector by writing a program that produces the final list from the three given lists.

Input format

The first line of the input contains 3 integers N1, N2 and N3. N1 is the number of voters in the first list, N2 is the number of voters in the second list and N3 is the number of voters in the third list. The next N1 lines (lines 2,...,N1+1) contain one positive integer each and describe the first list in ascending order. The following N2 lines (lines N1+2,...,N1+N2+1) describe the second list in ascending order and the final N3 lines (lines N1+N2+2,...,N1+N2+N3+1) describe the third list in ascending order.

Page 21: Indian Olympiad Basic

Output format

The first line of the output should contain a single integer M indicating the number voters in the final list. The next M lines (lines 2,...,M+1) should contain one positive integer each, describing the list of voters in the final list, in ascending order.

Test data

You may assume that 1 ≤ N1,N2,N3 ≤ 50000. You may also assume that in 50% of the inputs 1 ≤ N1,N2,N3 ≤ 2000.

Example

We now illustrate the input and output formats using the above example:

Sample input:

5 6 5

23

30

42

57

90

21

23

35

57

90

92

21

23

30

57

90

Sample output:

Page 22: Indian Olympiad Basic

5

21

23

30

57

90

Basic Division

Problem 1: Overlapping Subwords, (K Narayan Kumar, CMI)

In this problem we are concerned with words constructed using the lowercase letters of the English alphabet (that is, a,b,c,...,z). A word need not be meaningful - for us, any sequence of letters is a word. For example, abbca is a word.

A subword of a word is a contiguous subsequence of the word. For example the subwords of the word abbba are a, b, ab, bb, ba, abb, bbb, bba, abbb, bbba and abbba.

The word b appears thrice as a subword of abbba (when we break it up as a b bba, ab b ba and abb b a. We call these the (three) instances of the subword b in abbba.

On the other hand, the word bb appears twice as a subword of abbba (corresponding to a bb ba and ab bb a). Notice that the two instances of bb in abbba are overlapping, the middle b appears in both of them. We are interested in words that have overlapping subwords.

Here are a couple of examples:

The word abbababbabbab has overlapping subwords. The subword abbab has three instances in this word -- abbab abbabbab, abbab abbab bab and abbababb abbab -- of which the last two instances overlap.

On the other hand, the word abbabaabbab has no overlapping subwords.

Your task is to determine whether a given word has overlapping subwords.

Input format

The first line of the input will contain a single integer N, indicating the number of letters in the given word. The next line contains a sequence of N letters drawn from a,b,...,z.

Output format

If there are overlapping subwords, the first line of the output should be YES. In this case, the second line should contain a single word w that has overlapping instances in the given word. If

Page 23: Indian Olympiad Basic

there are multiple such words, it suffices to print one. If there are no overlapping subwords, the output should consist of a single line with the word NO.

Test data

You may assume 1 ≤ N ≤ 200.

Example

We now illustrate input and output formats using the examples described above.

Sample input 1:

5

abbba

Sample output 1:

YES

bb

Sample input 2:

11

abbabaabbab

Sample output 2:

NO

Sample input 3:

13

abbababbabbab

Sample output 3:

YES

abbab

Basic Division

Problem 2: Balance the Teams, (K Narayan Kumar, CMI)

Page 24: Indian Olympiad Basic

Our friend the king has a fairly large army that is divided into a number of battalions. Once a year, in order to ensure that his army is fighting fit, he organises war exercises. Here the army is divided into two teams and they fight each other in mock battles.

Each team is a collection of battalions -- that is, each battalion is to assigned to one team or the other and cannot be broken up.

The sizes of these battalions are not all the same. Some of them are huge while others are small. To ensure that the war exercises are useful, the two teams should be of roughly the same size. So he asks the minister to divide the battalions into two teams in such a way that the difference in the total size of the two teams is minimized.

For instance, suppose there are 6 battalions and their sizes are as follows:

Battalion No Size

1 20

2 30

3 100

4 30

5 20

6 30

Then, by using Battalions 1 and 3 in one team and the other four battalions in the other team, the minister would get one team with size 120 and the other with size 110. Thus the difference is sizes is 10. You can verify that this is the best possible.

Your task is to help the minister to select the teams.

Input format

The first line of the input contains a single integer N indicating the number of battalions. The following N lines (lines 2,3,...,N+1) contain one positive integer each. The integer on line i+1 indicates the size of the ith battalion.

Output format

The first line of the output should be a single positive integer indicating the minimum possible difference in the sizes of the two teams. Following this there must be N lines (lines 2, ... N+1) of output, each containing a 0 or a 1. This is a description of a grouping that yields this optimal difference. The value on line i+1 describes whether battalion i goes to team 0 or team 1. If there is more than one such optimal grouping it suffices to describe any one.

Test data

Page 25: Indian Olympiad Basic

You may assume that N ≤ 20.

Example

We now illustrate the input and output formats using the above example:

Sample input:

6

20

30

100

30

20

30

Sample output:

10

0

1

0

1

1

1

Basic Division

Problem 1: Painting, (K Narayan Kumar, CMI)

A new university campus is coming up at Siruseri. At the centre of this campus is a piece of sculpture designed by a well known modern artist of Siruseri. The sculpture is made up of a set of concrete pillars of square cross-section of varying heights, placed next to each other.

The sculpture occupies a rectangular area of length M feet and width N feet. M × N pillars, each of cross section 1 foot by 1 foot, are placed in this area. These pillars of varying height. A description of such an sculpture can be given as an M × N array of numbers. The number at position (i,j) indicates the height of the pillar placed at position (i,j). For example, here is a description of a sculpture where M=2 and N=3.

Page 26: Indian Olympiad Basic

3 2 3

2 1 1

The artist wants the entire sculpture to be painted in canary yellow. Notice that when two pillars are placed next to each other, a part of the surface of each of these pillars is no longer exposed to the outside and need not be painted. In the example above, only 9 square feet of the pillar at position (1,1) need to be painted. As a standalone pillar it has 13 square feet of paintable area (1 square foot on the top, and 3 square foot on each of its four faces). But, in the current arrangement, it is placed adjacent to two pillars which hide 2 square feet each of its exposed area. So, only 9 square feet need to be painted. If you add up the paintable areas for all the 6 pillars, you can see that a total of 34 square feet need to be painted.

Your task is to determine the total area that is to be painted given a description of the sculpture.

Input format

The first line of the input contains two integers, M and N, indicating the length and width of the sculpture in feet. This is followed by M rows of N positive numbers each. The jth number on line i+1 denotes height of the pillar at position (i,j).

Output format

A single positive integer indicating the total area to be painted.

Test data

You may assume 1 ≤ M,N ≤ 1000.

Example

We now illustrate the input and output formats using the example described above.

Sample input

2 3

3 2 3

2 1 1

Sample output

34

Basic Division

Problem 2: Find the Distance, (Tanmoy Chakraborty, Indraneel Mukherjee, CMI)

Page 27: Indian Olympiad Basic

Given two numbers written out in binary with the same number of binary digits, the Hamming distance between them is the number of positions where they differ. For example the Hamming distance between

010010

and

100010

is 2 since they differ in the leftmost two positions and nowhere else. Similarly the Hamming distance between

0111110

and

0011100

is 2.

Suppose we consider binary numbers of length K. Given a number N with N ≤ 2K - 1, we want to find the sum of the Hamming distances between 0 and 1, 1 and 2 and so on till N-1 and N.

For example if K=3 and N=4 the answer is 7 since the Hamming distance between 000 and 001 (0 and 1 written using 3 bits) is 1, the distance between 001 and 010 is 2, between 010 and 011 is 1 and between 011 and 100 is 3.

Given K and N, your task is to find the sum of the Hamming distances between the K-digit binary representations of 0 and 1, 1 and 2, ..., N-1 and N.

Input format

A single line with two positive integers K and N.

Output format

A single line with a single integer giving the required sum of Hamming distances.

Test data

You may assume that K ≤ 32 and N ≤ 1000000.

Example

We now illustrate the input and output formats using the above example.

Page 28: Indian Olympiad Basic

Sample input

3 4

Sample output

7