You are on page 1of 10

ACM ICPC 20142015, Northeastern European Regional Contest

St. Petersburg Barnaul Tbilisi Tashkent, December 7, 2014

Problem G. Gomoku
Input le:
Output le:

standard input
standard output

This is an interactive problem.


Gomoku is a two-player game on a two-dimensional grid. Each cell of the grid can be either empty, contain
the rst players mark (black), or contain the second players mark (white), but not both. Initially the
entire grid is empty. Two players make alternating moves, starting with the rst player. At each move,
a player can put her mark into exactly one empty cell. The rst player to have her ve adjacent marks
in a single row wins. The winning row can be either vertical, horizontal or diagonal.
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

Position where the second player (white marks) had won.


The players use a 19 19 grid in this problem. If the entire grid gets lled with marks but no player has
won, the game is declared a draw.
The rst player uses the following strategy: as the rst move, she puts her mark into the center cell of
the grid. At every other move, she picks such a move that maximizes the score of the resulting position.
In order to nd the score of a position, the rst player considers all possible places where the winning
combination might eventually form in other words, all horizonal, vertical and diagonal rows of ve
consecutive cells on the board (of course, they may overlap each other). If such a row contains both the
rst players marks and the second players marks, it is disregarded. If such a row contains no marks, it
is disregarded as well. For each row with exactly k (1 k 5) marks of the rst player and no marks
of the second player, add 502k1 to the score of the position. For each row with exactly k marks of the
second player and no marks of the rst player, subtract 502k from the score of the position. Finally, add
a random integer number between 0 and 502 1 to the score. This random number is chosen uniformly.
In case when several moves of the rst player have equal scores (such ties are quite rare because of the
random addition mentioned above), the rst player picks the one with the smallest x-coordinate, and in
case of equal x-coordinates, the one with the smallest y-coordinate.
Your task is to write a program that plays the second player and beats this strategy.
Your program will play 100 games against the strategy described above, with dierent seeds of random
generator. Your program must win all these games.
Page 9 of 15

ACM ICPC 20142015, Northeastern European Regional Contest


St. Petersburg Barnaul Tbilisi Tashkent, December 7, 2014

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.

Sample input and output


In the example below the rst player does not use the strategy from the problem statement. The
example is given only to illustrate the interaction format.
standard input
10 10
10 11
10 12
10 13
9 10
9 11
9 9
11 13
-1 -1

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

Final position from the example.

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

ACM-ICPC 2014-2015, Moscow Subregional


Russia, Moscow, October 2014

Problem I. Interactive Primes Guessing


Input file:
Output file:
Time limit:
Memory limit:

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

ACM-ICPC 2014-2015, Moscow Subregional


Russia, Moscow, October 2014
In C++, use cout.flush();
In Java, use System.out.flush();
In Python, use sys.stdout.flush().
If your program receives an EOF (end-of-file) condition on the standard input, it MUST exit immediately
with exit code 0. Failure to comply with this requirement may result in Time Limit Exceeded error.

Page 11 of 17

ACM ICPC 20132014, Northeastern European Regional Contest


St. Petersburg Barnaul Tbilisi Tashkent, December 1, 2013

Problem I. Interactive Interception


Input le:
Output le:

standard input
standard output

This is an interactive problem.


North Eastern Emergency Rocket Control agency (NEERC) has developed a new radar control system
that is designed to better control ballistic rocket interception. To test the new system NEERC agency
had developed a mathematical model that is intended to show this systems abilities.
Let us represent a rocket as a point on a line. Initially the point is at some unknown integer location
between 0 and p, inclusive. It has some unknown speed of q which is an integer between 0 and v, inclusive.
Each second the following happens. First, the control system makes a query to the radar of a form
check L R and gets an answer whether the point is currently between L and R, inclusive, or not.
After that, the points coordinate increases by q.
The goal of the radar control system is to learn the exact location of the point at the beginning of some
second. When it does learn the points location, then instead of making a query to the radar, it gives a
command to intercept the point at that location.
You have to implement the control system that locates and intercepts the point while making at most
100 queries to the radar.

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).

Sample input and output


standard input
2 2
Yes
No
Yes
Yes

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

ACM ICPC 20122013, Northeastern European Regional Contest


St. Petersburg Barnaul Tashkent Tbilisi, December 2, 2012

Problem B. Blind Problem Solving


Input le:
Output le:

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.

Sample input and output


standard input
2 5 3
2
5

standard output
decline
accept
stop

Page 2 of 14

ACM ICPC 20112012, Northeastern European Regional Contest


St. Petersburg Barnaul Tashkent Tbilisi, November 27, 2011

Problem I. Interactive Permutation Guessing


Input le:
Output le:

standard input
standard output

There is a permutation a of size n that you have to guess interactively.


You are allowed to make queries of the following kind. You output any permutation b of size n. The
information given back to you is the length of the longest common subsequence of permutations a and b.

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.

Sample input and output


standard input
4
3
2
2
4

standard output
1
1
4
3

2
3
1
1

3
4
2
2

4
2
3
4

Page 12 of 16

ACM ICPC 20102011, Northeastern European Regional Contest


St Petersburg Barnaul Tashkent Tbilisi, November 24, 2010

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.

Sample input and output


standard input
2
1
2
2
3
3
1

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

ACM ICPC 20092010, Northeastern European Regional Contest


St Petersburg Barnaul Tashkent Tbilisi, November 11, 2009

Problem C. Central Element


Input file:
Output file:

standard input
standard output

This is an interactive problem.


There is a permutation P of numbers 1 through n, not known to you, P = hP1 , P2 , ..., Pn i. You can
ask the following type of questions: Given three distinct positions i, j and k, which of Pi , Pj and Pk is
central? Element is central if it is neither minimal nor maximal.
For example, if the permutation is h2, 1, 4, 3i, and you ask about positions 1, 2, and 3, you receive 2,
because 2 is the central element of the set {P1 , P2 , P3 } = {2, 1, 4}. Note that you dont get the information
at which position among 1, 2, and 3 it is located.
Your task is to find the permutation P . Actually, for each permutation P there is a set S(P ) of permutations that cannot be distinguished from P using the allowed questions. You must find any permutation
from this set.

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.

Sample input and output


standard input
4
2
3
2
3

standard output
1 2 3
2 3 4
1 2 4
1 3 4
OK 2 1 4 3

Page 3 of 13

ACM ICPC 20082009, Northeastern European Regional Contest


St Petersburg Barnaul Tashkent Batumi, November 26, 2008

Problem B. Blind Walk


Input file:
Output file:

standard input
standard output

This is an interactive problem.


Your task is to write a program that controls a robot which blindly walks through a maze. The maze is
n m (1 n, m 30) rectangular grid that consists of square cells. Each cell is either empty or blocked.
All cells on the border of the maze are blocked. The robot starts in an empty cell. It can move south,
west, north, or east to an adjacent empty cell. The robot is blind and has only bump sensors, so when
it attempts to move it can either succeed or bump into blocked cell and fail.
The robot has to visit all empty cells in the maze. All cells are guaranteed to be reachable.

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.

Sample input and output


standard output
NORTH
EAST
SOUTH
EAST
SOUTH
WEST
SOUTH
WEST
NORTH
WEST
WEST
NORTH
EAST
NORTH
DONE

standard input
BLOCKED
BLOCKED
EMPTY
BLOCKED
BLOCKED
EMPTY
BLOCKED
BLOCKED
EMPTY
EMPTY
BLOCKED
BLOCKED
EMPTY
BLOCKED

Page 3 of 14

You might also like