Professional Documents
Culture Documents
Problem G. Gomoku
Input le:
Output le:
standard input
standard output
Interaction protocol
On each step, your program must:
1. Read numbers x and y from the input.
2. If both these numbers are equal to 1 then the game is over and your program must exit.
3. Otherwise these numbers are the coordinates of the rst players move (1 x, y 19).
4. Print the coordinates of the move of the second player, followed by line end. Dont forget to ush
the output buer.
standard output
11 10
11 11
10 9
10 14
8 9
11 9
11 12
11 8
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Note
There are many variations of Gomoku rules in the world. Please only consider the rules described in this
problem statement.
Page 10 of 15
stdin
stdout
2 seconds
256 megabytes
The jury has chosen the numbers X0 and P (1 X0 < P N , P is a prime number). You are given the
number N : the upper limit for P .
Your task is to guess the number P .
In order to do that, your program will ask the questions in the form of Ai (2 Ai N , Ai is a prime
number) where i = 1, 2, . . . is the index of the question.
After each question, the answer comes. In case the i-th question does not guess the number P , the jury
calculates Xi = (Xi1 Ai ) mod P and returns your the result of comparison of Xi and Xi1 (>, <
or =).
Your program must guess the number P by asking not more than 42 questions.
Input
The first line of input contains a single integer N (2 N 3 105 ). The answers for the questions of your
program are given on separate lines.
If the i-th question is the number P , then the string OK is given as an answer. In other cases, the result
of comparison between Xi and Xi1 is returned. If Xi > Xi1 then the answer is >, if Xi < Xi1 then
the answer is <, otherwise the answer is =.
After receiving the answer OK, your program must stop sending questions.
Output
Your program must output a single question on a separate line. Each question must consist of a single
prime number Ai (2 Ai N ).
Examples
stdin
100
<
>
>
>
>
>
OK
stdout
29
43
23
17
29
17
73
Note
Among all possible pairs (X0 , P ) where P 100, only the pair (41, 73) satisfies the following equations
for the questions given in the first example test: X1 < X0 , X2 > X1 , X3 > X2 , X4 > X3 , X5 > X4 ,
X6 > X5 (X0 = 41, X1 = 21, X2 = 27, X3 = 37, X4 = 45, X5 = 64, X6 = 66).
The pipe from your program to the interactor program and the pipe back have limited size. Your
program must read from the standard input to avoid deadlock. Deadlock condition is reported as
Time Limit Exceeded.
To flush the standard output stream, use the following statements:
In C, use fflush(stdout);
Page 10 of 17
Page 11 of 17
standard input
standard output
Interaction protocol
Interaction starts with your program reading two integers the values of p and v from the standard
input (1 p 105 , 1 v 105 ).
After that your program must print commands to the standard output. Each command must be one of
the following two.
check L R make a query to the radar to get an answer whether the point is currently
between L and R, inclusive, or not. The answer must be read from the standard input and is either
Yes or No. After that the points coordinate is increased by q. L and R must be integers
(0 L R 109 ).
There must be at most 100 check commands.
answer x the exact coordinate x of the point is known, and you order to intercept the point.
After printing this command your program must exit.
Your program must write end-of-line sequence and ush the standard output after each command, including the last command answer x (end-of-line must be written and ushed before exiting).
standard output
check 1 3
check 3 5
check 2 4
check 4 5
answer 5
In the given example the point was initially at location 1 and is moving at a speed q = 1.
Page 12 of 14
standard input
standard output
This is an interactive problem. You are to solve a subset sum problem, a variant of a knapsack problem.
You are given n items, each weighing wi grams, and a knapsack with the maximum capacity of c grams.
You are to nd a subset of these items whose total weight does not exceed c and is maximal possible.
Easy? But you have to do it blindly!
At the beginning you know only two numbers n and c the number of items and the maximal capacity
of a knapsack. There are positive weights wi and two bit strings inside the jury program: A and B, each
having n bits. Each bit string represents a candidate solution to the problem: if the i-th bit is set to 1,
then the i-th item is a part of the solution. A stores your previously selected candidate solution. B is a
candidate solution that is proposed to you on each turn by ipping a random bit in A. You can either
accept or decline this proposal. Your task is to stop when B encodes the optimal solution.
Initially, A is initialized with some value that is xed for each test case but is not known to you.
At each turn the value of A is copied to B, and then
n a bit at a uniformly random position of B is ipped.
You are given the weight of B which is equal to i=1 wi B(i), where B(i) is the value of i-th bit in B.
As a reply, your program is allowed to perform one of the following three actions:
stop this is the nal action. This means that B encodes the optimal solution. Your program
must exit after performing this action.
accept the value of B is copied to A.
decline the value of B is thrown out.
After each non-nal action next turn starts. You are allowed to perform at most 1000 actions.
Interaction protocol
Interaction starts with your program reading three integer numbers the values of n, c and the weight
of B for the rst turn from the rst line of the standard input. Then your program must write its action
to the standard output, wait for the jury program to write the weight of the new value of B to the
standard input and so on.
Your program must exit after writing the last action (stop) to the standard output. Your program must
write end-of-line sequence and ush the standard output after each action, including the last one.
It is guaranteed that a jury program chooses the bits to ip randomly, i. e. using a random number
generation algorithm with an initial seed xed for each test case.
Input
The rst line of the standard input contains n (1 n 20), c (0 c 109 ) and the weight of B. It is
guaranteed that 1 wi 108 for any i.
Each of the following lines will contain one integer number the weight of B at the corresponding turn.
Output
The standard output consists of the actions your program performs. Each action is represented with a
single line containing a single word: stop, accept, or decline.
standard output
decline
accept
stop
Page 2 of 14
standard input
standard output
Interaction protocol
First, your program must read from the standard input one line with integer n, the size of the permutation
you have to guess.
Your program must then write to the standard output one line with a permutation and wait for a line in
the standard input with a response, then write next query and read next response, and so on until you
know a.
Once you receive response n (which means youve found a), youre done and your program must exit.
Input
The rst line of the standard input contains integer n, the size of the permutation (1 n 40).
Each of the next lines of the standard input contains response to your query the length of the longest
common subsequence of the permutation queried by you and the permutation a.
Output
Each line of the standard output should contain a space-separated list of integers that form a permutation
youre querying.
Your can make at most 5n2 queries.
You must ush the standard output after printing each line. You must not print any lines after you
receive the response n, just exit.
standard output
1
1
4
3
2
3
1
1
3
4
2
2
4
2
3
4
Page 12 of 16
Problem G. Game of 10
Input file:
Output file:
standard input
standard output
The Game of 10 is played by two players on a 4 4 field. Initially all 16 cells of the field are empty.
Players make alternating moves. On each move a player writes a number from 1 to 4 into an empty cell.
The first player that makes any row or column filled with four numbers with a sum of 10 wins. If all cells
are filled but no row or column has a sum of 10, then a draw is declared.
You have to write a program that plays for the second player and always wins.
The table below shows the field after the sample game that is shown in the Sample input and output
section. Subscripts denote the number of the move in the game starting from the first one, with 14-th
being the last and winning move by the second player. The last move in the game had filled the second
column with a sum of 10.
13
17
311
312
413
21
114
36
28
210
25
49
44
32
Interaction protocol
The interaction starts with your program reading the first players move from the standard input. Then
your program must write its move to the standard output, wait for the first players move in the standard
input and so on.
Your program must exit after writing the last, winning move to the standard output. Your program must
write end-of-line sequence and flush the standard output after each move, including the last, winning
move.
Input
The standard input consists of the first players moves. Each move is represented by a single line that
contains three integer numbers r, c, and k (1 r, c, k 4) separated by spaces, where r and c are row
and column numbers, and k is the number that the first player writes into the cell (r, c).
Output
The standard output consists of the second players moves. Each move is represented by a single line
of the same format as in the standard input. The last, winning move, shall have an extra word WIN
(without quotes) on the same line after the three numbers that denote the move, separated from them
by a space.
2
1
4
1
4
1
2
2
1
2
1
4
3
4
standard output
3
4
4
1
1
4
3
3
4
2
3
4
1
2
3
4
3
2
2
3
1 WIN
Page 8 of 13
standard input
standard output
Interaction protocol
First, your program must read from the standard input one line with the integer n, the size of the
permutation.
The program must write to the standard output one line with three positions that you ask a question
about and wait for a line in the standard input with a response, then write next question and read next
response, and so on until you know the permutation P up to S(P ).
Once you know the answer, output one line with the word OK and the permutation P .
Input
The first line of the standard input contains n, the size of the permutation (3 n 200).
Each of the next lines of the standard input contains response to your question the number that is
central among the numbers at the asked positions.
Output
When youre asking questions, each line of the standard output should contain three different integers
from the range of 1 to n, space-separated. You can ask at most 2 000 questions.
When youre stating the answer, the line of the standard output should contain the word OK, and the
numbers P1 , P2 , . . . , Pn , all space-separated. After printing this line your program must exit.
You must flush standard output after printing each line.
standard output
1 2 3
2 3 4
1 2 4
1 3 4
OK 2 1 4 3
Page 3 of 13
standard input
standard output
The picture shows sample maze where blocked cells are, filled and initial robots location is designated
with a circle.
Interaction protocol
The program must write to the standard output one line with robots action and wait for a line in the
standard input with a response, then write next action and read next response, and so on until all empty
cells in the maze had been visited. The program must exit only when all cells have been visited. Empty
cells may be visited multiple times. It is acceptable to move even after all cells had been visited.
Output
Each line of the standard output represents robots action. It is one of the following five strings: SOUTH,
WEST, NORTH, EAST, or DONE. DONE must be printed when the robot has visited all empty cells. After
printing DONE your program must exit. You must flush standard output after printing each action.
Input
Each line of the standard input represents response on robots action. It is either a string EMPTY if
robot has successfully moved in the specified direction to an adjacent cell or a string BLOCKED if robots
movement has failed because the corresponding adjacent cell was blocked.
standard input
BLOCKED
BLOCKED
EMPTY
BLOCKED
BLOCKED
EMPTY
BLOCKED
BLOCKED
EMPTY
EMPTY
BLOCKED
BLOCKED
EMPTY
BLOCKED
Page 3 of 14