alexjercan/bugnet huggingface dataset:
Problem E: Reverse a Road

Andrew R. Klein resides in the city of Yanwoe, and goes to his working place in this city every weekday.
He has been totally annoyed with the road traffic of this city. All the roads in this city are one-way, so he
has to drive a longer way than he thinks he need.

One day, the following thought has come up to Andrew’s mind: “How about making the sign of one road
indicate the opposite direction? I think my act won’t be out as long as I change just one sign. Well, of
course I want to make my route to the working place shorter as much as possible. Which road should I
alter the direction of?” What a clever guy he is.

You are asked by Andrew to write a program that finds the shortest route when the direction of up to
one road is allowed to be altered. You don’t have to worry about the penalty for complicity, because you
resides in a different country from Andrew and cannot be punished by the law of his country. So just help
him!

Input

The input consists of a series of datasets, each of which is formatted as follows:

N
S T
M
A1 B1
A2 B2
...
AM BM

N denotes the number of points. S and T indicate the points where Andrew’s home and working place are
located respectively. M denotes the number of roads. Finally, Ai and Bi indicate the starting and ending
points of the i-th road respectively. Each point is identified by a unique number from 1 to N. Some roads
may start and end at the same point. Also, there may be more than one road connecting the same pair of
starting and ending points.

You may assume all the following: 1 ≤ N ≤ 1000, 1 ≤ M ≤ 10000, and S ≠ T.

The input is terminated by a line that contains a single zero. This is not part of any dataset, and hence
should not be processed.

Output

For each dataset, print a line that contains the shortest distance (counted by the number of passed roads)
and the road number whose direction should be altered. If there are multiple ways to obtain the shortest
distance, choose one with the smallest road number. If no direction change results in a shorter route, print
0 as the road number.

Separate the distance and the road number by a single space. No extra characters are allowed.

Sample Input

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

Output for the Sample Input

1 4
Problem F: Webby Subway

You are an officer of the Department of Land and Transport in Oykot City. The department has a plan to
build a subway network in the city central of Oykot.

In the plan, n subway lines are built, and each line has two or more stations. Because of technical
problems, a rail track between two stations should be straight and should not have any slope. To make
things worse, no rail track can contact with another rail track, even on a station. In other words, two
subways on the same floor cannot have any intersection.

Your job is to calculate the least number of required floors in their plan.

Input

The input consists of multiple datasets. Each dataset is formatted as follows:

N
Line1
Line2
...
LineN

Here, N is a positive integer that indicates the number of subway lines to be built in the plan (N ≤ 22),
and Linei is the description of the i-th subway line with the following format:

S
X1 Y1
X2 Y2
...
XS YS

S is a positive integer that indicates the number of stations in the line (S ≤ 30), and (Xi, Yi) indicates
the coordinates of the i-th station of the line (-10000 ≤ Xi, Yi ≤ 10000). The rail tracks are going to
be built between two consecutive stations in the description. No stations of the same line have the same
coordinates.

The input is terminated by a dataset of N = 0, and it should not be processed.

Output

For each dataset, you should output the least number of required floors.

Sample Input

2
2
0 0
10 0
2
0 10
10 10
2
2
0 0
10 10
2
0 10
10 0
3
2
0 0
10 10
2
0 10
10 0
2
1 0
1 10
0

Output for the Sample Input

1
2
3
Problem G: Time Trial

Some people like finishing computer games in an extremely short time. Terry A. Smith is one of such
and prefers role playing games particularly.

He is now trying to find a shorter play for one of the key events in a role playing game. In this event,
a player is presented a kind of puzzle on a grid map with three rocks and three marked squares. The
objective is to have all the rocks placed on the marked squares by controlling the hero of this game
appropriately.

Figure 1: Example Map

The hero can move to the four adjacent squares, that is, to the north, east, south, and west, unless his move
is blocked by walls or rocks. He can never enter squares occupied by walls. On the other hand, when he
is moving to a square occupied by a rock, he pushes the rock in his moving direction. Nonetheless, he
cannot push the rock if the next square is occupied by a wall or another rock and his move is blocked in
this case. Also, he can only move one rock at a time. It is allowed to have rocks pass through marked
squares.

Terry thinks he can reduce his playing time by finding the optimal way to move the rocks and then playing
the event accordingly. However, it is too hard for him to find the solution of this puzzle by hand. So you
are asked by him to write a program that finds the smallest number of steps for the maps given as the
input. Here, each move from a square to its adjacent square is counted as one step.

Input

The input is a sequence of datasets. Each dataset has the following format:

W H
Row1
...
RowH

W and H are the width and height of the map (4 ≤ W, H ≤ 16). Rowi denotes the i-th row of the map
and consists of W characters. Each character represents a square and is one of the following: ‘#’ (wall), ‘.’ (floor), ‘*’ (rock), ‘_’ (marked square), and ‘@’ (hero). Each map contains exactly three rocks, three
marked squares, and one hero. The outermost squares are always occupied by walls. You may assume
that the number of non-wall squares does not exceed fifty. It is also guaranteed that there is at least one
solution for every map.

The input is terminated by a line with two zeros. This line is not part of any datasets and should not be
processed.

Output

For each dataset, print the smallest number of steps in a line.

Sample Input

7 6
#######
#.._..#
#.*.*.#
#.@.*.#
#_..._#
#######
10 13
##########
####___###
####...###
####...###
#####.####
#.....#..#
#.#*.*.*.#
#...###..#
###.#.#.##
###.#.#.##
###.....##
###..@..##
##########
0 0

Output for the Sample Input

15
118
Problem H: Vending Machine

There has been marketing warfare among beverage vendors, and they have been working hard for in-
crease of their sales. The Kola-Coqua Company is one of the most successful vendors among those:
their impressive advertisements toward the world has brought the overwhelming market share of their
representative product called Koque.

This time, Kola-Coqua is focusing on vending machines. They think cusomters will be more pleasant as
the machines respond more quickly, so they have improved many parts of the machines.

In particular, they have developed a new device of change return. The new device can give one or more
kinds of coins at a time (in a single operation), although it can give only one coin for each kind at once.
For example, suppose there are 500-yen, 100-yen, 50-yen and 10-yen coins, change of 6540 yen can be
made by four operations of giving 500-yen and 10-yen coins and nine operations of giving 500-yen coins.
In conclusion, 6540 yen can be returned by thirteen operations. It is supposed that the new device allows
customers to make their purchase more quickly and so helps Kola-Coqua’s market share grow up.

However, the project leader says “No, it’s not optimal yet.” His suggesion is as follows: the real opti-
mization is to minimize the number of operations. For example, change of 6540 yen should be made
with ten of 500-yen coins, ten of 100-yen coins, ten of 50-yen coins, and four of 10-yen coins. This way,
6540 yen can be returned only with ten operations. This allows full speed-up in giving back change, even
though it sometimes results in a huge amount of coins.

Given which kinds of coins are available and how much change should be given back, you are to write a
program that calculates the minimum number of operations according to the above suggestion. You may
assume that there are enough amount of coins inside the vending machines.

Input

The input consists of multiple data sets. Each dataset is described by two lines. The first line contains N
(N ≤ 10) and M (M ≤ 100000) indicating the number of kinds of coins and the amount of change to be
made, respectively. The second line contains N integers representing the value of each kind of coin.

The input is terminated by a dataset of N = M = 0. This dataset must not be processed.

Output

For each dataset, output in a line the minimum number of operations needed to give back exactly the
specified amount of change.

Sample Input

6 330
1 5 10 50 100 500
7 127
1 2 4 8 16 32 64
2 10000
1000 2000
0 0

Output for the Sample Input

2
1
4
Problem I: Memory Match

Memory match is a single-player game which employs a set of 2M cards. Each card is labeled with a
number between 1 and M on its face. For each number i (1 ≤ i ≤ M), there are exactly two cards which
have the number i. At the start of the game, all cards are shuffled and laid face down on a table. In each
turn you choose two cards and turn them face up. If two numbers on the cards are the same, they are
removed from the table. Otherwise, they are turned face down again (this is called a mismatch). When
you choose cards, you do not have to turn two cards simultaneously; you can choose the second card
after you see the number of the first card. The objective of the game is to remove all the cards with as
few mismatches as possible.

Royce A. Mitchell has extraordinary memory, so he can remember all the positions and the numbers of
the cards that he has already turned face up. Your task is to write a program that calculates the expected
number of mismatches, on average, when he plays the game optimally.

Input

The input consists of multiple datasets.

Each dataset consists of one even number N (2 ≤ N ≤ 1000) which denotes the number of cards in the
set.

The end of input is indicated by a line that contains a single zero. This is not part of the input and you
may not treat this line as a dataset.

Output

For each dataset, print the expected number of mismatches. Each output value may have an arbitrary
number of fractional digits, provided that the error is within 10-6.

Sample Input

2
4
6
8
10
52
0

Output for the Sample Input

0.0000000000
0.6666666667
1.3333333333
1.9238095238
2.5523809524
15.4435236099
Problem J: Tile Puzzle

You are visiting Ancient and Contemporary Museum. Today there is held an exhibition on the history of
natural science. You have seen many interesting exhibits about ancient, medieval, and modern science
and mathematics, and you are in a resting space now.

You have found a number of panels there. Each of them is equipped with N × N electric tiles arranged
in a square grid. Each tile is lit in one of the following colors: black (unlit), red, green, yellow, blue,
magenta, and cyan. Initially all the tiles are in black. When a tile is touched on, that tile and the eight
adjacent tiles will change their colors as follows: black -> red, red -> green, green -> yellow, yellow ->
blue, blue -> magenta, magenta -> cyan, and cyan -> black. Here, the leftmost and rightmost columns
are considered adjacent, and so as the uppermost and lowermost rows. There is a goal pattern for each
panel, and you are to change the colors of the tiles as presented in the goal pattern. For example, if you
are given the goal pattern shown in the figure below for a panel of 4 × 4, you will touch on the upper-left
tile once and then on the lower-right tile twice (note that this might not be the only way).

Since you are good at programming, you guess you can find the solution using your computer. So your
job in this problem is to write a program for it.

Figure 1: Example Goal Pattern

Input

The input contains a series of datasets. Each dataset is given in the following format:

N
Row1
...
RowN

N indicates the size (i.e. the number of rows and columns) of the electrical panel (3 ≤ N ≤ 15). Rowi
describes the goal pattern of the i-th row and contains exactly N numbers separated by a space. The j-th
number indicates the color of the j-th column, and it is one of the following: 0 (denoting black), 1 (red),
2 (green), 3 (yellow), 4 (blue), 5 (magenta), and 6 (cyan).

The input is terminated by a line containing a single zero. This line is not part of any datasets.

Output

For each dataset, your program should produce the output of N lines. The i-th line should correspond
to the i-th row and contain exactly N numbers separated by a space, where the j-th number should be
the number of touches on the tile of the j-th column. The number should be in the range from 0 to 6 inclusive.

If there is more than one solution, your program may output any of them. If it is impossible to make the
goal pattern, your program should output a single line containing “-1” (without quotes) instead of the N lines.

A blank line should follow the output for every dataset (including the last one).

Sample Input

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

Output for the Sample Input

1 0 0 0
0 0 0 0
0 0 0 0
0 0 0 2

0 0 0 0 0
0 3 0 0 0
0 0 0 0 0
0 0 0 4 0
0 0 0 0 0
Problem A: Girls' Party

Issac H. Ives hosted a party for girls. He had some nice goods and wanted to distribute them to the girls
as the presents. However, there were not enough number of presents and thus he needed to decide who
would take them. He held a game for that purpose.

Before the game, Issac had all the girls divided into two teams: he named his close friends Bella and
Gabriella as two leaders and asked other girls to join either Bella or Gabriella. After the two teams were
formed, Issac asked the girls to form one big circle.

The rule of the game was as follows. The game consisted of a number of rounds. In each round, the girls
called numbers from 1 to N in clockwise order (N was a number fixed before the game started). The girl
calling the number N was told to get out of the circle and excluded from the rest of the game. Then the
next round was started from the next girl, that is, the girls called the numbers again, and the one calling
N left the circle. This was repeated until only the members of either team remained. The remaining team
won the game.

As the game went on, Bella found far more girls of her team excluded than those of Gabriella’s team.
Bella complained it, and requested Issac to make the next round begin with a call of zero instead of one.
Issac liked her idea as he was a computer scientist, and accepted her request. After that round, many
girls of Gabriella’s team came to leave the circle, and eventually Bella’s team won the game and got the
presents from Issac.

Now let’s consider the following situation. There are two teams led by Bella and Gabriella respectively,
where they does not necessarily have the same numbers of members. Bella is allowed to change the
starting number from one to zero at up to one round (regardless the starting girl belongs to her team or
not). We want to know how many girls of Bella’s team at most can remain in the circle. You are requested
to write a program for it.

Input

The input is a sequence of datasets. The first line of the input contains the number of datasets. The
number of datasets does not exceed 200.

Each dataset consists of a line with a positive integer N (1 ≤ N ≤ 230) and a string that specifies the
clockwise order of the girls. Each character of the string is either ‘B’ (that denotes a member of Bella’s
team) or ‘G’ (Gabriella’s team). The first round begins with the girl indicated by the first character of the
string. The length of the string is between 2 and 200 inclusive.

Output

For each dataset, print in a line the maximum possible number of the girls of Bella’s team remaining in
the circle, or “0” (without quotes) if there are no ways for Bella’s team to win the game.

Sample Input

6
1 GB
3 GBGBBB
9 BBBGBBBGGG
9 GGBGBBGBBB
7 GBGGGGBGGG
3 BBBBBGBBBB

Output for the Sample Input

1
4
4
1
0
8
Problem B: Bitwise Kingdom

In the Bitwise Kingdom, located somewhere in the universe, there are exactly 2N citizens living and each
of them has a unique identification string that represents his or her class in the society. An identification
string is a binary string of length N which consists of characters ‘0’ or ‘1’. The order of classes is defined
among the citizens by the following criteria:

Citizens identified by a string containing a greater number of ones are ranked higher. For example,
“011” indicates a higher class than “100”.

Among those who have identification strings with the same number of ones, citizens identified by
a lexicographically greater identification string are ranked higher. For example, “110” indicates a
higher class than “101”.

For example, if N = 3, there are 8 (= 23) people in the country, and their identification strings are “000”,
“001”, “010”, “100”, “011”, “101”, “110”, and “111” (from the lowest class to the highest).

You are given two numbers N (1 ≤ N ≤ 60) and M (1 ≤ M ≤ 2N), and you want to resolve the
identification string of the person of the M-th lowest class among 2N citizens. Can you write a program
to solve this problem?

Input

The input consists of multiple datasets.

Each dataset consists of a line which contains two integers N and M in this order, separated with a single
space. The input does not contain any other extra characters such as leading or trailing spaces.

The end of input is indicated by a line with two zeros. This line is not part of any datasets.

Output

For each dataset, print the identification string of the person of the M-th lowest class in one line. Your
program may not omit any leading zeros in the answer.

Sample Input

3 3
3 5
0 0

Output for the Sample Input

010
011
Problem C: Adaptive Time Slicing Quantization

Nathan O. Davis is a student at the department of integrated systems. Today he learned digital quanti-
zation in a class. It is a process that approximates analog data (e.g. electrical pressure) by a finite set of
discrete values or integers.

He had an assignment to write a program that quantizes the sequence of real numbers each representing
the voltage measured at a time step by the voltmeter. Since it was not fun for him to implement normal
quantizer, he invented a new quantization method named Adaptive Time Slicing Quantization. This
quantization is done by the following steps:

Divide the given sequence of real numbers into arbitrary M consecutive subsequences called
frames. They do not have to be of the same size, but each frame must contain at least two ele-
ments. The later steps are performed independently for each frame.

Find the maximum value Vmax and the minimum value Vmin of the frame.

Define the set of quantized values. The set contains 2L equally spaced values of the interval
[Vmin, Vmax] including the both boundaries. Here, L is a given parameter called a quantization
level. In other words, the i-th quantized value qi (1 ≤ i ≤ 2L) is given by:

.
qi = Vmin + (i - 1){(Vmax - Vmin)/(2L - 1)}

Round the value of each element of the frame to the closest quantized value.

The key of this method is that we can obtain a better result as choosing more appropriate set of frames in
the step 1. The quality of a quantization is measured by the sum of the squares of the quantization errors
over all elements of the sequence: the less is the better. The quantization error of each element is the
absolute difference between the original and quantized values.

Unfortunately, Nathan caught a bad cold before he started writing the program and he is still down in his
bed. So he needs you help. Your task is to implement Adaptive Time Slicing Quantization instead. In
your program, the quantization should be performed with the best quality, that is, in such a way the sum
of square quantization errors is minimized.

Input

The input consists of multiple datasets. Each dataset contains two lines. The first line contains three
integers N (2 ≤ N ≤ 256), M (1 ≤ M ≤ N/2), and L (1 ≤ L ≤ 8), which represent the number of
elements in the sequence, the number of frames, and the quantization level. The second line contains N
real numbers ranging in [0, 1]. The input is terminated by the dataset with N = M = L = 0, which must not be processed.

Output

For each dataset, output the minimum sum of square quantization errors in one line. The answer with an
absolute error of less than or equal to 10-6 is considered to be correct.

Sample Input

5 2 1
0.1 0.2 0.3 0.4 0.5
6 2 2
0.1 0.2 0.3 0.4 0.5 0.6
0 0 0

Output for the Sample Input

0.01
0.00
Problem E: Magical Island

This is a story in the epoch of magic. A clan of magicians lived in an artificial island built by magic power.

One day, a crisis erupted on the island. An Empire ACM (Atlas Country of Magic) required unconditional
surrender to them, otherwise an imperial force attacked by magical missiles to the island. However, they
were so proud that they did not surrender to the ACM, and built a system to generate magical shield to
protect the clan from the threat of magical missiles. In this system, a crystal with different elements was
put on each corner of the island: the world consisted of four elements, namely Fire, Water, Air and Earth.
Each crystal generated magical shield with the element of the crystal by receiving magicians’ magic
power; it shielded the island from magical missiles of the same element: any magical missile consists of
one of the four elements. Magic shield covered a circular area; the crystal should be located on the center
the circular area. The crystal required R2 magic power to shield a circular area of radius R. However,
there was one restriction. Magicians should send exactly the same amount of magic power to all crystals,
otherwise the island was lost because of losing a balance between four elements.

They decided to live in an area which is shielded from any magical missile. Your job is to write a program
to calculate minimum amount of magic power to secure enough area for them to live in.

Input

The input consists of multiple datasets. Each dataset is a single line containing three integers W, H and
S, separated by a single space. The line containing three zeros separated by a single space indicates the
end of the input.

W and H are width and depth of the island, respectively. S is the area magicians needed to live in. You
may assume that 0 < W, H ≤ 100 and 0 < S ≤ W × H.

Output

For each dataset, output a separate line containing the total minimum necessary magic power. The value
may contain an error less than or equal to 0.001. You may print any number of digits after the decimal
point.

Sample Input

1 1 1
10 15 5
15 10 100
0 0 0

Output for the Sample Input

8.000
409.479
861.420