Sunteți pe pagina 1din 16

Al-Khawarizmi 08

Programming Competition

Hosted by International Islamic University


Malaysia

2nd and 3rd July, 2008

You get 16 Pages


10 Problems
&
300 Minutes
The 2008 Al-khawarizmi Programming Competition
Hosted by International Islamic University, Malaysia

Square Numbers
A Input: Standard Input
Output: Standard Output

A square number is an integer number whose square root is also an integer. For example 1, 4, 81 are
some square numbers. Given two numbers a and b you will have to find out how many square
numbers are there between a and b (inclusive).

Input
The input file contains at most 201 lines of inputs. Each line contains two integers a and b
(0<a≤b≤100000). Input is terminated by a line containing two zeroes. This line should not be
processed.

Output
For each line of input produce one line of output. This line contains an integer which denotes how
many square numbers are there between a and b (inclusive).

Sample Input Output for Sample Input


1 4 2
1 10 3
0 0

Page 2 of 17
The 2008 Al-khawarizmi Programming Competition
Hosted by International Islamic University, Malaysia

Age Sort
B Input: Standard Input
Output: Standard Output

You are given the ages (in years) of all people of a country with at least 1 year of age. You know that
no individual in that country lives for 100 or more years. Now, you are given a very simple task of
sorting all the ages in ascending order.

Input
There are multiple test cases in the input file. Each case starts with an integer n (0<n<=2000000), the
total number of people. In the next line, there are n integers indicating the ages. Input is terminated
with a case where n = 0. This case should not be processed.

Output
For each case, print a line with n space separated integers. These integers are the ages of that country
sorted in ascending order.

Warning: Input Data is pretty big (~ 25 MB) so use faster IO.

Sample Input Output for Sample Input


5 1 2 3 4 5
3 4 2 1 5 1 2 2 3 3
5
2 3 2 3 1
0

Page 3 of 17
The 2008 Al-khawarizmi Programming Competition
Hosted by International Islamic University, Malaysia

Commandos
C Input: Standard Input
Output: Standard Output

A group of commandos were assigned a critical task. They are to destroy an enemy head quarter. The
enemy head quarter consists of several buildings and the buildings are connected by roads. The
commandos must visit each building and place a bomb at the base of each building. They start their
mission at the base of a particular building and from there they disseminate to reach each building.
The commandos must use the available roads to travel between buildings. Any of them can visit one
building after another, but they must all gather at a common place when their task in done. In this
problem, you will be given the description of different enemy headquarters. Your job is to determine
the minimum time needed to complete the mission. Each commando takes exactly one unit of time to
move between buildings. You may assume that the time required to place a bomb is negligible. Each
commando can carry unlimited number of bombs and there is an unlimited supply of commando
troops for the mission.

Input
The first line of input contains a number T<50, where T denotes the number of test cases. Each case
describes one head quarter scenario. The first line of each case starts with a positive integer N≤100,
where N denotes the number of buildings in the head quarter. The next line contains a positive integer
R, where R is the number of roads connecting two buildings. Each of the next R lines contain two
distinct numbers, 0≤u,v<N, this means there is a road connecting building u to building v. The
buildings are numbered from 0 to N-1. The last line of each case contains two integers 0≤s,d<N.
Where s denotes the building from where the mission starts and d denotes the building where they
must meet.

You may assume that two buildings will be directly connected by at most one road. The input will be
such that, it will be possible to go from any building to another by using one or more roads.

Output
For each case of input, there will be one line of output. It will contain the case number followed by the
minimum time required to complete the mission. Look at the sample output for exact formatting.

Sample Input Output for Sample Input


2 Case 1: 4
4 Case 2: 1
3
0 1
2 1
1 3
0 3
2
1
0 1
1 0

Page 4 of 17
The 2008 Al-khawarizmi Programming Competition
Hosted by International Islamic University, Malaysia

Even Parity
D Input: Standard Input
Output: Standard Output

We have a grid of size N x N. Each cell of the grid initially contains a zero(0) or a one(1).
The parity of a cell is the number of 1s surrounding that cell. A cell is surrounded by at most 4 cells
(top, bottom, left, right).

Suppose we have a grid of size 4 x 4:


1010 1312
1111 2331
The parity of each cell would be
0100 2121
0000 0100

For this problem, you have to change some of the 0s to 1s so that the parity of every cell becomes
even. We are interested in the minimum number of transformations of 0 to 1 that is needed to achieve
the desired requirement.

Input
The first line of input is an integer T (T<30) that indicates the number of test cases. Each case starts
with a positive integer N(1≤N≤15). Each of the next N lines contain N integers (0/1) each. The
integers are separated by a single space character.

Output
For each case, output the case number followed by the minimum number of transformations required.
If it's impossible to achieve the desired result, then output -1 instead.

Sample Input Output for Sample Input


3 Case 1: 0
3 Case 2: 3
0 0 0 Case 3: -1
0 0 0
0 0 0
3
0 0 0
1 0 0
0 0 0
3
1 1 1
1 1 1
0 0 0

Page 5 of 17
The 2008 Al-khawarizmi Programming Competition
Hosted by International Islamic University, Malaysia

Count the Polygons


E Input: Standard Input
Output: Standard Output

A polygon is a plane figure that is bounded by a closed path and composed of a finite sequence of
straight line segments. These segments are called its edges, and the points where two edges meet are
the polygon's vertices.

You have got a set of N sticks of various lengths. How many ways can you choose K sticks from this
set and form a polygon with K sides by joining the end points.

Input
The first line of input is an integer T (T<100) that indicates the number of test cases. Each case starts
with a line containing 2 positive integers N and K ( 3≤N≤30 & 3≤K≤N ). The next line contains N
positive integers in the range [1, 2^31), which represents the lengths of the available sticks. The
integers are separated by a single space.

Output
For each case, output the case number followed by the number of valid polygons that can be formed
by picking K sticks from the given N sticks.

Sample Input Output for Sample Input


4 Case 1: 2
4 3 Case 2: 15
10 10 20 20 Case 3: 0
6 4 Case 4: 1
1 1 1 1 1 1
4 3
10 20 30 100000000
6 6
2 3 4 5 6 7

Page 6 of 17
The 2008 Al-khawarizmi Programming Competition
Hosted by International Islamic University, Malaysia

Largest Prime Divisor


F Input: Standard Input
Output: Standard Output

All integer numbers are divisible by primes. If a number is divisible by more than one prime number,
then it obviously has a largest prime divisor. The numbers which do not fall in this category do not
have a largest prime divisor. Given a number N your job is to write a program that finds its largest
prime divisor. An integer number n is divisible by another integer number m if there is an integer t
such that mt=n.

Input
The input file contains at most 450 sets of inputs. Each line contains a decimal integer N. N does not
have more than 14 digits. Input is terminated by a line containing a single zero. So no other line except
the last line contains a zero in the input. This line need not be processed.

Output
For each line of the input produce one line of output. This line contains an integer LPD, which is the
largest prime divisor of the input number N. If the input number is not divisible by more than one
prime number output a -1.

Sample Input Output for Sample Input


2 -1
6 3
100 5
0

Page 7 of 17
The 2008 Al-khawarizmi Programming Competition
Hosted by International Islamic University, Malaysia

Pythagorean Triangles
G Input: Standard Input
Output: Standard Output

Many lattice triangles are formed in an (NxN) grid, but not all of them are Pythagorean (Right angled)
triangles. Given the value of N your job is to write a program that produces the number of lattice
triangles in an (NxN) grid. A lattice triangle is triangle whose three vertices are lattice points. A lattice
point in two dimensional Cartesian coordinate system is a point whose abscissa and ordinate are
integers.

Input
The input file contains at most 15 lines of inputs. Each line contains an integer N (0<N<2001). Input is
terminated by a line containing a single zero. This line should not be processed.

Output
For each value of N produce one line of output which contains an integer T. Here T denotes the total
number of right angled triangles in that (NxN) grid.

Sample Input Output for Sample Input


10 23596
20 418716
30 2288304
0

Page 8 of 17
The 2008 Al-khawarizmi Programming Competition
Hosted by International Islamic University, Malaysia

Substring
H Input: Standard Input
Output: Standard Output

Given a set of pattern strings, and a text, you have to find, if any of the pattern is a substring of the
text. If any of the pattern string can be found in text, then print “yes”, otherwise “no” (without quotes).
But, unfortunately, that’s not what is asked here. ☺
The problem described above, requires a input file generator. The generator generates a text of length
L, by choosing L characters randomly. Probability of choosing each character is given as priori, and
independent of choosing others.
Now, given a set of patterns, calculate the probability of a valid program generating “no”.

Input
First line contains an integer T, the number of test cases. Each case starts with an integer K, the
number of pattern strings. Next K lines each contain a pattern string, followed by an integer N, number
of valid characters. Next N lines each contain a character and the probability of selecting that
character, pi. Next an integer L, the length of the string generated. The generated text can consist of
only the valid characters, given above.

There will be a blank line after each test case.

Output
For each test case, output the number of test case, and the probability of getting a “no”.

Constraints
• T ≤ 50
• K ≤ 20
• Length of each pattern string is between 1 and 20
• Each pattern string consists of only alphanumeric characters (‘a’ to ‘z’, ‘A’ to ‘Z’,’0’ to ‘9’)
• Valid characters are all alphanumeric characters
• ∑pi = 1
• L ≤ 100

Sample Input Output for Sample Input


2 Case #1: 0.250000
1 Case #2: 0.840000
a
2
a 0.5
b 0.5
2

Page 9 of 17
The 2008 Al-khawarizmi Programming Competition
Hosted by International Islamic University, Malaysia

2
ab
ab
2
a 0.2
b 0.8
2

Page 10 of 17
The 2008 Al-khawarizmi Programming Competition
Hosted by International Islamic University, Malaysia

Treasure Hunt
I Input: Standard Input
Output: Standard Output

Famous Archaeologist Dr. Asiana Jones has discovered something extraordinary. He found a thousand
years old tomb, and discovered that there are thousands of treasure rooms beneath it. Each treasure
room contains a chest full of treasure. There can be a number of tunnels to go from one room to
another. But, since the tunnels are thousand years old(like the tomb itself), they are very fragile. You
have to be very quick, and you can't use a tunnel more than once.
Dr. Asiana Jones has managed to calculate the exact positions of all the tombs. But, as the rooms are
beneath the tomb, he must dig a hole, one the floor to get to any of them. He doesn't want to dig more
than one hole, because, otherwise, the whole tomb may become unstable.
If Dr. Jones can start from any of the treasure room, find the maximum number of treasure he can
collect. Dr. Jones has to start and end at the same room, since, that's the only way to get out of the
tomb.

Input
First line of input contains an integer T, the number of test cases, followed by T scenarios. Each
scenario starts with two integers, N and M, the number of treasure rooms, and the number of tunnels.
Each treasure room is uniquely identified by an integer between 1 and N. Each of the following M
lines each, contain two integers, the treasure rooms at the end of the corresponding tunnel. The tunnels
can be traversed in any direction, but at most once. That is, if there is a tunnel between room 1 and 2,
it can be used to move from room 1 to 2, or 2 to 1. But, after any of these move, you can't use this
tunnel any more. There will be a blank line before each scenario.

Output
For each scenario, print the scenario number, followed by the maximum number of treasure to collect.
Next line will contain a sequence of integers, the starting rooms, from where, you can collect
maximum number of treasures.

Constraints
• T ≤ 31
• 1 ≤ N ≤ 10,000
• 0 ≤ M ≤ 1,000,000

Sample Input Output for Sample Input


3 Case #1: 1
1 2 3 4
4 3 Case #2: 3
1 2 1 2 3
2 3 Case #3: 3
1 4 2 3 4

3 3
Page 11 of 17
The 2008 Al-khawarizmi Programming Competition
Hosted by International Islamic University, Malaysia

1 2
2 3
3 1

4 4
1 2
2 3
3 4
4 2

Page 12 of 17
The 2008 Al-khawarizmi Programming Competition
Hosted by International Islamic University, Malaysia

NIM Chess
J Input: Standard Input
Output: Standard Output

In recent days alice and bob are really bored with playing regular chess. Bob is much stronger chess
player than alice and win every time. Although alice is a very good computer scientist but she can not
apply her skill in the game because of large state space of this game. So alice has created a new
variation of chess. This game is played in a very large board (an inifinite quarterplane). Each of the
square can be represented by a pair of integer(r,c) denoting that the square is in row r and column c.
Rows are numbered from 0 from up to down. Columns are numbered from 0 from left to right.
0,0 0,1 0,2 0,3 0,4 0,5 0,6, 0,7 0,8 ….
1,0 1,1 1,2 1,3 1,4 1,5 1,6, 1,7 1,8 ….
2,0 2,1 2,2 2,3 2,4 2,5 2,6, 2,7 2,8 ….
3,0 3,1 3,2 3,3 3,4 3,5 3,6, 3,7 3,8 ….
4,0 4,1 4,2 4,3 4,4 4,5 4,6, 4,7 4,8 ….
5,0 5,1 5,2 5,3 5,4 5,5 5,6, 5,7 5,8 ….
6,0 6,1 6,2 6,3 6,4 6,5 6,6, 6,7 6,8 ….
… … … … .. … .. … … ….

The rules of the game are that there are lots of pieces in the board. In her turn player will select a piece
and will move that piece to another square according to the move rule of that piece. The player who
can not move any piece in her turn loses the game. Alice is the first player to move. The description
and move rules of the pieces are described below.
White Rook
This piece can go any square up or any square left. When this piece reaches the square (0,0) it can not
be moved further. In the following diagram the white rook piece WR can move to the square marked
with X only.
X
X
X
X
X X X X WR

Black Rook
Although this piece looks like a rook and moves up or left only. But it has a weird rule. From square
(r,c) it follows the following algorithm to move.
Let mx = max(r,c) and mn = min(r,c).
Then the reachable square are (mn,mx-k*mn) or (mx-k*mn,mn) where k is a positive integer and (mx-
k*mn) is also a positive integer. When the piece move any square on row 0 or column 0 or any square
in the (0,0) dddiagonal it can not be moved further. Instead of BlackRook some people called it euclid
rook.
Page 13 of 17
The 2008 Al-khawarizmi Programming Competition
Hosted by International Islamic University, Malaysia

In the following diagram Black Rook piece BR can only move to the square marked with X

X X X BR

White King
This piece can move one square at a time. From the position (r,c) it can move to the squares (r-1,c),
(r,c-1) and (r-1,c-1). It can not move to any square with negative row or negative column. When it
reaches the square (0,0) it can not make any further move. In the following diagram White King piece
WK can move the squares only marked with X.

X X
X WK

Black King
Black king can move one square at a time. From the position (r,c) it can move to the squares (r-
1,c),(r,c-1),(r-1,c-1) and (r-1,c+1). It can not move to any square with negative row or negative
column. When it reaches the square (0,0) it can not make any further move. In the following Diagram
Black King piece BK can move to the square only marked with X.

X X X
X BK

WhiteKnight
From the square (r,c) white knight can move to the square (r-2,c-1) and (r-1,c-2). It can not move to
any square with negative row or negative column. When it reaches row 0 or column 0 or square(1,1)
it can not make any further move. In the following diagram White Knight piece WN can move to the
squares only marked with X.

X
X
WN

Page 14 of 17
The 2008 Al-khawarizmi Programming Competition
Hosted by International Islamic University, Malaysia

Black Knight
From the square(r,c) black knight can move to the square(r+1,c-2),(r-2,c-1),(r-1,c-2) and (r-2,c+1). It
can not move to any square with negative row or negative column. When it reaches any of the square
(0,0),(0,1),(1,0) and (1,1) it can not make any further move. In the following diagram Black Knight
piece BN can move to the squares only marked with X.

X X
X
BN
X

Bishop
Bishop can move diagonaly up only. When it reaches any square at row 0 it can not make any further
move. In the following diagram Bishop Piece B can move to square only marked with X.
X X
X X
X X
X X
B

Queen
Queen can move any square up, any square left and any square diagonally to left-up. When it reaches
the square(0,0) it can not make any further move. In the following diagram the Queen piece Q can
move to squares only marked with X.
X X
X X
X X
X X
X X X X Q

Here are some additional rules of the game.


-In any state of the game a square can hold multiple number of pieces.
-The initial location of all the pieces except the queen will be between (1,1) to (10000,10000).
-The initial location of queen pieces will be between (1,1) to (100,100).

Page 15 of 17
The 2008 Al-khawarizmi Programming Competition
Hosted by International Islamic University, Malaysia

-In each turn a player can select exactly one piece and can move that piece according to the
move rule of that piece. The selected piece has to be moved. It can not be kept in its own place.
Now you are given the initial position of the game. It is the turn of alice. Help her to determine if the
position is winning or losing. In case the position is winning also calculate how many winning moves
are available for alice in her first move.

Input
Input contains multiple number of test cases. First line of the input contains T(1≤T≤50) the number of
test cases. T test cases follow then. Each test case starts with an integer n (1≤n≤10000) the number of
pieces in the board. Each of the next n lines contain a string s and 2 integers (r and c) seperated by a
single space. Each of these lines contains the information about a piece. The string denotes the name
of the piece. The only possible values of the s can be “whiterook”, ”blackrook”, ”whiteking”,
”blackking”, ”whiteknight”,”blackknight”,”bishop” and “queen”. The quotes are given for clarity
only. When the value of s is “queen” then r and c will be between 1 and 100. In other cases the values
of r and c will be between 1 and 100000.

Output
For each test case output contains one line. If the game is a losing position then it will contains only
“LOSING”. Otherwise it will contain “WINNING” followed by the number of winning moves.
“WINNING” and the number of winning moves will be separated a single space. See the sample
output for further clarifications.

Sample Input Output for Sample Input


4 LOSING
2 WINNING 3
whiterook 3 5 WINNING 5
whiterook 5 3 WINNING 1
3
whiterook 3 5
whiterook 5 3
whiterook 1 2
3
whiterook 7 15
whiterook 15 7
whiterook 1 2
4
whiterook 63 127
blackrook 22 189
whiteking 55 65
blackking 110 245

Page 16 of 17

S-ar putea să vă placă și