alexjercan/bugnet huggingface dataset:
Problem A: The Balance

Ms. Iyo Kiffa-Australis has a balance and only two kinds of weights to measure a dose of medicine.

For example, to measure 200mg of aspirin using 300mg weights and 700mg weights, she can put
one 700mg weight on the side of the medicine and three 300mg weights on the opposite side
(Figure 1). Although she could put four 300mg weights on the medicine side and two 700mg
weights on the other (Figure 2), she would not choose this solution because it is less convenient
to use more weights.

You are asked to help her by calculating how many weights are required.

Figure 1: To measure 200mg of aspirin using three 300mg weights and one 700mg weight

Figure 2: To measure 200mg of aspirin using four 300mg weights and two 700mg weights

Input

The input is a sequence of datasets. A dataset is a line containing three positive integers a, b,
and d separated by a space. The following relations hold: a ≠ b, a ≤ 10000, b ≤ 10000, and
d ≤ 50000. You may assume that it is possible to measure d mg using a combination of a mg
and b mg weights. In other words, you need not consider “no solution” cases.

The end of the input is indicated by a line containing three zeros separated by a space. It is not
a dataset.

Output

The output should be composed of lines, each corresponding to an input dataset (a, b, d). An
output line should contain two nonnegative integers x and y separated by a space. They should
satisfy the following three conditions.

You can measure d mg using x many a mg weights and y many b mg weights.

The total number of weights (x + y) is the smallest among those pairs of nonnegative
integers satisfying the previous condition.

The total mass of weights (ax + by) is the smallest among those pairs of nonnegative
integers satisfying the previous two conditions.

No extra characters (e.g. extra spaces) should appear in the output.

Sample Input

700 300 200
500 200 300
500 200 500
275 110 330
275 110 385
648 375 4002
3 1 10000
0 0 0

Output for the Sample Input

1 3
1 1
1 0
0 3
1 1
49 74
3333 1
Problem F: Dice Puzzle

Let’s try a dice puzzle. The rules of this puzzle are as follows.

Dice with six faces as shown in Figure 1 are used in the puzzle.

Figure 1: Faces of a die

With twenty seven such dice, a 3 × 3 × 3 cube is built as shown in Figure 2.

Figure 2: 3 × 3 × 3 cube

When building up a cube made of dice, the sum of the numbers marked on the faces of adjacent dice that are placed against each other must be seven (See Figure 3). For example, if one face of the pair is marked “2”, then the other face must be “5”.

Figure 3: A pair of faces placed against each other

The top and the front views of the cube are partially given, i.e. the numbers on faces of
some of the dice on the top and on the front are given.

Figure 4: Top and front views of the cube

The goal of the puzzle is to find all the plausible dice arrangements that are consistent
with the given top and front view information.

Your job is to write a program that solves this puzzle.

Input

The input consists of multiple datasets in the following format.

N
Dataset1
Dataset2
...
DatasetN

N is the number of the datasets.

The format of each dataset is as follows.

T11 T12 T13
T21 T22 T23
T31 T32 T33
F11 F12 F13
F21 F22 F23
F31 F32 F33

Tij and Fij (1 ≤ i ≤ 3, 1 ≤ j ≤ 3) are the faces of dice appearing on the top and front views,
as shown in Figure 2, or a zero. A zero means that the face at the corresponding position is
unknown.

Output

For each plausible arrangement of dice, compute the sum of the numbers marked on the nine
faces appearing on the right side of the cube, that is, with the notation given in Figure 2,

∑3i=1∑3j=1Rij.

For each dataset, you should output the right view sums for all the plausible arrangements, in
ascending order and without duplicates. Numbers should be separated by a single space.

When there are no plausible arrangements for a dataset, output a zero.

For example, suppose that the top and the front views are given as follows.

Figure 5: Example

There are four plausible right views as shown in Figure 6. The right view sums are 33, 36, 32,
and 33, respectively. After rearranging them into ascending order and eliminating duplicates,
the answer should be “32 33 36”.

Figure 6: Plausible right views

The output should be one line for each dataset. The output may have spaces at ends of lines.

Sample Input

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

Output for the Sample Input

27
24
32 33 36
0
Problem I: Crossing Prisms

Prof. Bocchan is a mathematician and a sculptor. He likes to create sculptures with mathematics.

His style to make sculptures is very unique. He uses two identical prisms. Crossing them at right
angles, he makes a polyhedron that is their intersection as a new work. Since he finishes it up
with painting, he needs to know the surface area of the polyhedron for estimating the amount
of pigment needed.

For example, let us consider the two identical prisms in Figure 1. The definition of their cross
section is given in Figure 2. The prisms are put at right angles with each other and their
intersection is the polyhedron depicted in Figure 3. An approximate value of its surface area
is 194.8255.

Figure 1: Two identical prisms at right angles

Given the shape of the cross section of the two identical prisms, your job is to calculate the
surface area of his sculpture.

Input

The input consists of multiple datasets, followed by a single line containing only a zero. The
first line of each dataset contains an integer n indicating the number of the following lines, each
of which contains two integers ai and bi (i = 1, ... , n).

Figure 2: Outline of the cross section

Figure 3: The intersection

A closed path formed by the given points (a1, b1), (a2, b2 ), ... , (an, bn), (an+1, bn+1)(= (a1, b1))
indicates the outline of the cross section of the prisms. The closed path is simple, that is, it does
not cross nor touch itself. The right-hand side of the line segment from (ai, bi) to (ai+1 , bi+1 ) is the inside of the section.

You may assume that 3 ≤ n ≤ 4, 0 ≤ ai ≤ 10 and 0 ≤ bi ≤ 10 (i = 1, ... , n).

One of the prisms is put along the x-axis so that the outline of its cross section at x = ζ is
indicated by points (xi, yi, zi ) = (ζ, ai, bi) (0 ≤ ζ ≤ 10, i = 1, ... , n). The other prism is put
along the y-axis so that its cross section at y = η is indicated by points (xi, yi, zi) = (ai, η, bi)
(0 ≤ η ≤ 10, i = 1, ... , n).

Output

The output should consist of a series of lines each containing a single decimal fraction. Each
number should indicate an approximate value of the surface area of the polyhedron defined by the corresponding dataset. The value may contain an error less than or equal to 0.0001. You
may print any number of digits below the decimal point.

Sample Input

4
5 0
0 10
7 5
10 5
4
7 5
10 5
5 0
0 10
4
0 10
10 10
10 0
0 0
3
0 0
0 10
10 0
4
0 10
10 5
0 0
9 5
4
5 0
0 10
5 5
10 10
4
0 5
5 10
10 5
5 0
4
7 1
4 1
0 1
9 5
0

Output for the Sample Input

194.8255
194.8255
600.0000
341.4214
42.9519
182.5141
282.8427
149.2470
Problem A: Sum of Consecutive Prime Numbers

Some positive integers can be represented by a sum of one or more consecutive prime numbers.
How many such representations does a given positive integer have? For example, the integer 53
has two representations 5 + 7 + 11 + 13 + 17 and 53. The integer 41 has three representations
2 + 3 + 5 + 7 + 11 + 13, 11 + 13 + 17, and 41. The integer 3 has only one representation, which is
3. The integer 20 has no such representations. Note that summands must be consecutive prime
numbers, so neither 7 + 13 nor 3 + 5 + 5 + 7 is a valid representation for the integer 20.

Your mission is to write a program that reports the number of representations for the given
positive integer.

Input

The input is a sequence of positive integers each in a separate line. The integers are between 2
and 10 000, inclusive. The end of the input is indicated by a zero.

Output

The output should be composed of lines each corresponding to an input line except the last zero.
An output line includes the number of representations for the input integer as the sum of one
or more consecutive prime numbers. No other characters should be inserted in the output.

Sample Input

2
3
17
41
20
666
12
53
0

Output for the Sample Input

1
1
2
3
0
0
1
2
Problem E: Mobile Computing

There is a mysterious planet called Yaen, whose space is 2-dimensional. There are many beautiful
stones on the planet, and the Yaen people love to collect them. They bring the stones back home
and make nice mobile arts of them to decorate their 2-dimensional living rooms.

In their 2-dimensional world, a mobile is defined recursively as follows:

a stone hung by a string, or

a rod of length 1 with two sub-mobiles at both ends; the rod
is hung by a string at the center of gravity of sub-mobiles.
When the weights of the sub-mobiles are n and m, and their
distances from the center of gravity are a and b respectively,
the equation n × a = m × b holds.

For example, if you got three stones with weights 1, 1, and 2, here are some possible mobiles
and their widths:

Given the weights of stones and the width of the room, your task is to design the widest possible
mobile satisfying both of the following conditions.

It uses all the stones.

Its width is less than the width of the room.

You should ignore the widths of stones.

In some cases two sub-mobiles hung from both ends of a rod might
overlap (see the figure on the below). Such mobiles are acceptable.
The width of the example is (1/3) + 1 + (1/4).

Input

The first line of the input gives the number of datasets. Then the specified number of datasets
follow. A dataset has the following format.

r
s
w1
.
.
.
ws

r is a decimal fraction representing the width of the room, which satisfies 0 < r < 10. s is the
number of the stones. You may assume 1 ≤ s ≤ 6. wi is the weight of the i-th stone, which is
an integer. You may assume 1 ≤ wi ≤ 1000.

You can assume that no mobiles whose widths are between r - 0.00001 and r + 0.00001 can be made of given stones.

Output

For each dataset in the input, one line containing a decimal fraction should be output. The
decimal fraction should give the width of the widest possible mobile as defined above. An
output line should not contain extra characters such as spaces.

In case there is no mobile which satisfies the requirement, answer -1 instead.

The answer should not have an error greater than 0.00000001. You may output any number of
digits after the decimal point, provided that the above accuracy condition is satisfied.

Sample Input

5
1.3
3
1
2
1
1.4
3
1
2
1
2.0
3
1
2
1
1.59
4
2
1
1
3
1.7143
4
1
2
3
5

Output for the Sample Input

-1
1.3333333333333335
1.6666666666666667
1.5833333333333335
1.7142857142857142
Problem F: Atomic Car Race

In the year 2020, a race of atomically energized cars will be held. Unlike today’s car races, fueling
is not a concern of racing teams. Cars can run throughout the course without any refueling.
Instead, the critical factor is tire (tyre). Teams should carefully plan where to change tires of
their cars.

The race is a road race having n checkpoints in the course. Their distances from the start are a1, a2, ... , and an (in kilometers). The n-th checkpoint is the goal. At the i-th checkpoint (i < n),
tires of a car can be changed. Of course, a team can choose whether to change or not to change
tires at each checkpoint. It takes b seconds to change tires (including overhead for braking and
accelerating). There is no time loss at a checkpoint if a team chooses not to change tires.

A car cannot run fast for a while after a tire change, because the temperature of tires is lower
than the designed optimum. After running long without any tire changes, on the other hand,
a car cannot run fast because worn tires cannot grip the road surface well. The time to run
an interval of one kilometer from x to x + 1 is given by the following expression (in seconds).
Here x is a nonnegative integer denoting the distance (in kilometers) from the latest checkpoint
where tires are changed (or the start). r, v, e and f are given constants.

1/(v - e × (x - r)) (if x ≥ r)
1/(v - f × (r - x)) (if x < r)

Your mission is to write a program to determine the best strategy of tire changes which minimizes
the total time to the goal.

Input

The input consists of multiple datasets each corresponding to a race situation. The format of a
dataset is as follows.

n
a1 a2 . . . an
b
r v e f

The meaning of each of the input items is given in the problem statement. If an input line
contains two or more input items, they are separated by a space.

n is a positive integer not exceeding 100. Each of a1, a2, ... , and an is a positive integer satisfying 0 < a1 < a2 < . . . < an ≤ 10000. b is a positive decimal fraction not exceeding 100.0. r is a
nonnegative integer satisfying 0 ≤ r ≤ an - 1. Each of v, e and f is a positive decimal fraction.
You can assume that v - e × (an - 1 - r) ≥ 0.01 and v - f × r ≥ 0.01.

The end of the input is indicated by a line with a single zero.

Output

For each dataset in the input, one line containing a decimal fraction should be output. The
decimal fraction should give the elapsed time at the goal (in seconds) when the best strategy is
taken. An output line should not contain extra characters such as spaces.

The answer should not have an error greater than 0.001. You may output any number of digits
after the decimal point, provided that the above accuracy condition is satisfied.

Sample Input

2
2 3
1.0
1 1.0 0.1 0.3
5
5 10 15 20 25
0.15
1 1.0 0.04 0.5
10
1783 3640 3991 4623 5465 5481 6369 6533 6865 8425
4.172
72 59.4705 0.0052834 0.0611224
0

Output for the Sample Input

3.5397
31.9249
168.6682
Problem G: Network Mess

Gilbert is the network admin of Ginkgo company. His boss is mad about the messy network
cables on the floor. He finally walked up to Gilbert and asked the lazy network admin to illustrate
how computers and switches are connected. Since he is a programmer, he is very reluctant to
move throughout the office and examine cables and switches with his eyes. He instead opted
to get this job done by measurement and a little bit of mathematical thinking, sitting down in
front of his computer all the time. Your job is to help him by writing a program to reconstruct
the network topology from measurements.

There are a known number of computers and an unknown number of switches. Each computer
is connected to one of the switches via a cable and to nothing else. Specifically, a computer
is never connected to another computer directly, or never connected to two or more switches.
Switches are connected via cables to form a tree (a connected undirected graph with no cycles).
No switches are ‘useless.’ In other words, each switch is on the path between at least one pair
of computers.

All in all, computers and switches together form a tree whose leaves are computers and whose
internal nodes switches (See Figure 9).

Gilbert measures the distances between all pairs of computers. The distance between two com-
puters is simply the number of switches on the path between the two, plus one. Or equivalently,
it is the number of cables used to connect them. You may wonder how Gilbert can actually
obtain these distances solely based on measurement. Well, he can do so by a very sophisticated
statistical processing technique he invented. Please do not ask the details.

You are therefore given a matrix describing distances between leaves of a tree. Your job is to
construct the tree from it.

Input

The input is a series of distance matrices, followed by a line consisting of a single '0'. Each
distance matrix is formatted as follows.

N
a11 a12 ... a1N
a21 a22 ... a2N
. . . .
. . . .
. . . .
aN1 aN2 ... aNN

N is the size, i.e. the number of rows and the number of columns, of the matrix. aij gives the
distance between the i-th leaf node (computer) and the j-th. You may assume 2 ≤ N ≤ 50 and
the matrix is symmetric whose diagonal elements are all zeros. That is, aii = 0 and aij = aji
for each i and j. Each non-diagonal element aij (i ≠ j) satisfies 2 ≤ aij ≤ 30. You may assume
there is always a solution. That is, there is a tree having the given distances between leaf nodes.

Output

For each distance matrix, find a tree having the given distances between leaf nodes. Then output
the degree of each internal node (i.e. the number of cables adjoining each switch), all in a single
line and in ascending order. Numbers in a line should be separated by a single space. A line
should not contain any other characters, including trailing spaces.

Sample Input

4
0 2 2 2
2 0 2 2
2 2 0 2
2 2 2 0
4
0 2 4 4
2 0 4 4
4 4 0 2
4 4 2 0
2
0 12
12 0
0

Output for the Sample Input

4
2 3 3
2 2 2 2 2 2 2 2 2 2 2
Problem H: Bingo

A Bingo game is played by one gamemaster and several players. At the beginning of a game,
each player is given a card with M × M numbers in a matrix (See Figure 10).

As the game proceeds, the gamemaster announces a series of numbers one by one. Each player
punches a hole in his card on the announced number, if any.

When at least one 'Bingo' is made on the card, the player wins and leaves the game. The 'Bingo'
means that all the M numbers in a line are punched vertically, horizontally or diagonally (See
Figure 11).

The gamemaster continues announcing numbers until all the players make a Bingo.

In the ordinary Bingo games, the gamemaster chooses numbers by a random process and has no
control on them. But in this problem the gamemaster knows all the cards at the beginning of
the game and controls the game by choosing the number sequence to be announced at his will.

Specifically, he controls the game to satisfy the following condition.

Cardi makes a Bingo no later than Cardj, for i < j. (*)

Figure 12 shows an example of how a game proceeds. The gamemaster cannot announce '5'
before '16', because Card4 makes a Bingo before Card2 and Card3, violating the condition (*).

Your job is to write a program which finds the minimum length of such sequence of numbers for
the given cards.

Input

The input consists of multiple datasets. The format of each dataset is as follows.

All data items are integers. P is the number of the cards, namely the number of the players. M is the number of rows and the number of columns of the matrix on each card. Nkij means the number written at the position (i, j) on the k-th card. If (i, j) ≠ (p, q), then Nkij ≠ Nkpq. The parameters P, M, and N satisfy the conditions 2 ≤ P ≤ 4, 3 ≤ M ≤ 4, and 0 ≤ Nkij ≤ 99.

The end of the input is indicated by a line containing two zeros separated by a space. It is not
a dataset.

Output

For each dataset, output the minimum length of the sequence of numbers which satisfy the
condition (*). Output a zero if there are no such sequences. Output for each dataset must be
printed on a separate line.

Sample Input

4 3
10 25 11 20 6 2 1 15 23
5 21 3 12 23 17 7 26 2
8 18 4 22 13 27 16 5 11
19 9 24 2 11 5 14 28 16
4 3
12 13 20 24 28 32 15 16 17
12 13 21 25 29 33 16 17 18
12 13 22 26 30 34 17 18 15
12 13 23 27 31 35 18 15 16
4 3
11 12 13 14 15 16 17 18 19
21 22 23 24 25 26 27 28 29
31 32 33 34 35 36 37 38 39
41 42 43 44 45 46 47 48 49
4 4
2 6 9 21 15 23 17 31 33 12 25 4 8 24 13 36
22 18 27 26 35 28 3 7 11 20 38 16 5 32 14 29
26 7 16 29 27 3 38 14 18 28 20 32 22 35 11 5
36 13 24 8 4 25 12 33 31 17 23 15 21 9 6 2
0 0

Output for the Sample Input

5
4
12
0

For your convenience, sequences satisfying the condition (*) for the first three datasets are shown
below. There may be other sequences of the same length satisfying the condition, but no shorter.

11, 2, 23, 16, 5
15, 16, 17, 18
11, 12, 13, 21, 22, 23, 31, 32, 33, 41, 42, 43
Problem I: Shy Polygons

You are given two solid polygons and their positions on the xy-plane. You can move one
of the two along the x-axis (they can overlap during the move). You cannot move it in other
directions. The goal is to place them as compactly as possible, subject to the following condition:
the distance between any point in one polygon and any point in the other must not be smaller
than a given minimum distance L.

We define the width of a placement as the difference between the maximum and the minimum
x-coordinates of all points in the two polygons.

Your job is to write a program to calculate the minimum width of placements satisfying the
above condition.

Let's see an example. If the polygons in Figure 13 are placed with L = 10.0, the result will be 100. Figure 14 shows one of the optimal placements.

Input

The input consists of multiple datasets. Each dataset is given in the following format.

L
Polygon1
Polygon2

L is a decimal fraction, which means the required distance of two polygons. L is greater than
0.1 and less than 50.0.

The format of each polygon is as follows.

n
x1 y1
x2 y2
.
.
.
xn yn

n is a positive integer, which represents the number of vertices of the polygon. n is greater than
2 and less than 15.

Remaining lines represent the vertices of the polygon. A vertex data line has a pair of nonneg-
ative integers which represent the x- and y-coordinates of a vertex. x- and y-coordinates are
separated by a single space, and y-coordinate is immediately followed by a newline. x and y are
less than 500.

Edges of the polygon connect vertices given in two adjacent vertex data lines, and vertices given
in the last and the first vertex data lines. You may assume that the vertices are given in the
counterclockwise order, and the contours of polygons are simple, i.e. they do not cross nor touch
themselves.

Also, you may assume that the result is not sensitive to errors. In concrete terms, for a given
pair of polygons, the minimum width is a function of the given minimum distance l. Let us
denote the function w(l). Then you can assume that |w(L ± 10-7) - w(L)| < 10-4.

The end of the input is indicated by a line that only contains a zero. It is not a part of a dataset.

Output

The output should consist of a series of lines each containing a single decimal fraction. Each
number should indicate the minimum width for the corresponding dataset. The answer should
not have an error greater than 0.0001. You may output any number of digits after the decimal
point, provided that the above accuracy condition is satisfied.

Sample Input

10.5235
3
0 0
100 100
0 100
4
0 50
20 50
20 80
0 80
10.0
4
120 45
140 35
140 65
120 55
8
0 0
100 0
100 100
0 100
0 55
80 90
80 10
0 45
10.0
3
0 0
1 0
0 1
3
0 100
1 101
0 101
10.0
3
0 0
1 0
0 100
3
0 50
100 50
0 51
0

Output for the Sample Input

114.882476
100
1
110.5005