You are on page 1of 75

1:Problem

You have a special set of N six-sided dice, each of which has six
different positive integers on its faces. Different dice may have different
numberings.

You want to arrange some or all of the dice in a row such that the faces
on top form a straight (that is, they show consecutive integers). For each
die, you can choose which face is on top.

How long is the longest straight that can be formed in this way?

Input

The first line of the input gives the number of test cases, T. T test cases
follow. Each test case begins with one line with N, the number of dice.
Then, N more lines follow; each of them has six positive integers Dij. The
j-th number on the i-th of these lines gives the number on the j-th face of
the i-th die.

Output

For each test case, output one line containing Case #x: y, where x is
the test case number (starting from 1) and y is the length of the longest
straight that can be formed.

Limits

1 ≤ T ≤ 100.
1 ≤ Dij ≤ 106 for all i, j.

Small dataset

1 ≤ N ≤ 100.

Large dataset

1 ≤ N ≤ 50000.
The sum of N across all test cases ≤ 200000.

Sample
Input Output

3 Case #1: 4
4 Case #2: 1
4 8 15 16 23 42 Case #3: 3
8 6 7 5 30 9
1 2 3 4 55 6
2 10 18 36 54 86
2
1 2 3 4 5 6
60 50 40 30 20 10
3
1 2 3 4 5 6
1 2 3 4 5 6
1 4 2 6 5 3

In sample case #1, a straight of length 4 can be formed by taking the 2


from the fourth die, the 3 from the third die, the 4 from the first die, and
the 5 from the second die.

In sample case #2, there is no way to form a straight larger than the
trivial straight of length 1.

In sample case #3, you can take a 1 from one die, a 2 from another, and
a 3 from the remaining unused die. Notice that this case demonstrates
that there can be multiple dice with the same set of values on their
faces.

2:Problem
Chemists work with periodic table elements, but here at Code Jam, we
have been using our advanced number smasher to study googlements.
A googlement is a substance that can be represented by a string of at
most nine digits. A googlement of length L must contain only decimal
digits in the range 0 through L, inclusive, and it must contain at least one
digit greater than 0. Leading zeroes are allowed. For
example, 103 and 001 are valid googlements of length 3. 400 (which
contains a digit, 4, greater than the length of the googlement, 3)
and 000 (which contains no digit greater than 0) are not.

Any valid googlement can appear in the world at any time, but it will
eventually decay into another googlement in a deterministic way, as
follows. For a googlement of length L, count the number of 1s in the
googlement (which could be 0) and write down that value, then count the
number of 2s in the googlement (which could be 0) and write down that
value to the right of the previous value, and so on, until you finally count
and write down the number of Ls. The new string generated in this way
represents the new googlement, and it will also have length L. It is even
possible for a googlement to decay into itself!

For example, suppose that the googlement 0414 has just appeared.
This has one 1, zero 2s, zero 3s, and two 4s, so it will decay into the
googlement 1002. This has one 1, one 2, zero 3s, and zero 4s, so it will
decay into 1100, which will decay into 2000, which will decay into 0100,
which will decay into 1000, which will continuously decay into itself.

You have just observed a googlement G. This googlement might have


just appeared in the world, or it might be the result of one or more decay
steps. What is the total number of possible googlements it could have
been when it originally appeared in the world?

Input

The first line of the input gives the number of test cases, T. T test cases
follow. Each consists of one line with a string G, representing a
googlement.

Output

For each test case, output one line containing Case #x: y, where x is
the test case number (starting from 1) and y is the number of different
googlements that the observed googlement could have been when it first
appeared in the world.

Limits

1 ≤ T ≤ 100.
Each digit in G is a decimal digit between 0 and the length of G,
inclusive.
G contains at least one non-zero digit.
Small dataset

1 ≤ the length of G ≤ 5.

Large dataset

1 ≤ the length of G ≤ 9.

Sample

Input Output

3 Case #1: 4
20 Case #2: 1
1 Case #3: 1
123

In sample case #1, the googlement could have originally been 20, or it
could have decayed from 11, which could have itself decayed
from 12 or 21. Neither of the latter two could have been a product of
decay. So there are four possibilities in total.

In sample case #2, the googlement must have originally been 1, which is
the only possible googlement of length 1.

In sample case #3, the googlement must have been 123; no other
googlement could have decayed into it.

3:Problem
You are the public relations manager for a chocolate manufacturer.
Unfortunately, the company's image has suffered because customers
think the owner is cheap and miserly. You hope to undo that impression
by offering a free factory tour and chocolate tasting.

Soon after starting the new project, you realized that the company
owner's reputation is well-deserved: he only agreed to give away free
chocolate if you would minimize the cost. The chocolate to be given
away comes in packs of P pieces. You would like to open new packs for
each tour group, but the owner insists that if there are leftover pieces
from one group, they must be used with the next tour group before
opening up any new packs.

For instance, suppose that each pack contains P=3 pieces, and that a
tour group with 5 people comes. You will open two packs to give one
piece to each person, and you will have one piece left over. Suppose
that after that, another tour group with 6 people comes. They will receive
the leftover piece, and then you will open two more packs to finish giving
them their samples, and so you will have one piece left over again. If two
groups with 4 people each come right after, the first of those will get the
leftover piece plus a full pack, and the last 4 person group will get their
pieces from two newly opened packs. Notice that you cannot open new
packs until all leftovers have been used up, even if you plan on using all
of the newly opened pack immediately.

In the example above, 2 out of the 4 groups (the first and last groups)
got all of their chocolate from freshly opened packs. The other 2 groups
got some fresh chocolate and some leftovers. You know that giving out
leftovers is not the best way to undo the owner's miserly image, but you
had to accept this system in order to get your cheap boss to agree to the
project. Despite the unfavorable context, you are committed to doing a
good job.

You have requests from N groups, and each group has specified the
number of people that will come into the factory. Groups will come in one
at a time. You want to bring them in in an order that maximizes the
number of groups that get only fresh chocolate and no leftovers. You
cannot reject groups, nor have a group get chocolate more than once,
and you need to give exactly one piece to each person in each group.

In the example above, if instead of 5, 6, 4, 4, the order were 4, 5, 6, 4, a


total of 3 groups (all but the 5 person group) would get only fresh
chocolate. For that set of groups, it is not possible to do better, as no
arrangement would cause all groups to get only fresh chocolate.

Input

The first line of the input gives the number of test cases, T. T test cases
follow. Each test case consists of two lines. The first line contains two
integers N, the number of groups coming for a tour, and P, the number
of pieces of chocolate per pack. The second line
contains N integers G1, G2, ..., GN, the number of people in each of the
groups.

Output

For each test case, output one line containing Case #x: y, where x is
the test case number (starting from 1) and y is the number of groups that
will receive only fresh chocolate if you bring them in in an order that
maximizes that number.

Limits

1 ≤ T ≤ 100.
1 ≤ N ≤ 100.
1 ≤ Gi ≤ 100, for all i.

Small dataset

2 ≤ P ≤ 3.

Large dataset

2 ≤ P ≤ 4.

Sample

Input Output

3 Case #1: 3
4 3 Case #2: 4
4 5 6 4 Case #3: 1
4 2
4 5 6 4
3 3
1 1 1

Sample Case #1 is the one explained in the statement. Besides the


possible optimal order given above, other orders like 6, 5, 4, 4 also
maximize the number of groups with only fresh chocolate, although the
groups that get the fresh chocolate are not necesarily the same. Notice
that we only care about the number of groups that get the best
experience, not the total number of people in them.
In Sample Case #2, the groups are the same as in Case #1, but the
packs contain two pieces each. In this case, several ways of ordering
them — for instance, 4, 4, 6, 5 — make all groups get only fresh
chocolate.

In Sample Case #3, all groups are single individuals, and they will all eat
from the same pack. Of course, only the first one to come in is going to
get a freshly opened pack.

4:Problem
The kitchen at the Infinite House of Pancakes has just received an order
for a stack of K pancakes! The chef currently has N pancakes available,
where N ≥ K. Each pancake is a cylinder, and different pancakes may
have different radii and heights.

As the sous-chef, you must choose K out of the N available pancakes,


discard the others, and arrange those K pancakes in a stack on a plate
as follows. First, take the pancake that has the largest radius, and lay it
on the plate on one of its circular faces. (If multiple pancakes have the
same radius, you can use any of them.) Then, take the remaining
pancake with the next largest radius and lay it on top of that pancake,
and so on, until all K pancakes are in the stack and the centers of the
circular faces are aligned in a line perpendicular to the plate, as
illustrated by this example:

You know that there is only one thing your diners love as much as they
love pancakes: syrup! It is best to maximize the total amount of exposed
pancake surface area in the stack, since more exposed pancake surface
area means more places to pour on delicious syrup. Any part of a
pancake that is not touching part of another pancake or the plate is
considered to be exposed.

If you choose the K pancakes optimally, what is the largest total


exposed pancake surface area you can achieve?
Input

The first line of the input gives the number of test cases, T. T test cases
follow. Each begins with one line with two integers N and K: the total
number of available pancakes, and the size of the stack that the diner
has ordered. Then, N more lines follow. Each contains two
integers Ri and Hi: the radius and height of the i-th pancake, in
millimeters.

Output

For each test case, output one line containing Case #x: y, where x is
the test case number (starting from 1) and y is the maximum possible
total exposed pancake surface area, in millimeters squared. y will be
considered correct if it is within an absolute or relative error of 10-6 of the
correct answer. See the FAQ for an explanation of what that means, and
what formats of real numbers we accept.

Limits

1 ≤ T ≤ 100.
1 ≤ K ≤ N.
1 ≤ Ri ≤ 106, for all i.
1 ≤ Hi ≤ 106, for all i.

Small dataset

1 ≤ N ≤ 10.

Large dataset

1 ≤ N ≤ 1000.

Sample

Input Output

4 Case #1: 138230.076757951


2 1 Case #2: 150796.447372310
100 20 Case #3: 43982.297150257
200 10 Case #4: 625.176938064
2 2
100 20
200 10
3 2
100 10
100 10
100 10
4 2
9 3
7 1
10 1
8 4

In Sample Case #1, the "stack" consists only of one pancake. A stack of
just the first pancake would have an exposed area of π × R02 + 2 × π
* R0 × H0 = 14000π mm2. A stack of just the second pancake would have
an exposed area of 44000π mm2. So it is better to use the second
pancake.

In Sample Case #2, we can use both of the same pancakes from case
#1. The first pancake contributes its top area and its side, for a total of
14000π mm2. The second pancake contributes some of its top area (the
part not covered by the first pancake) and its side, for a total of 34000π
mm2. The combined exposed surface area is 48000π mm2.

In Sample Case #3, all of the pancakes have radius 100 and height 10.
If we stack two of these together, we effectively have a single new
cylinder of radius 100 and height 20. The exposed surface area is
14000π mm2.

In Sample Case #4, the optimal stack uses the pancakes with radii of 8
and 9.

5:Problem
Annie is a bus driver with a high-stress job. She tried to unwind by going
on a Caribbean cruise, but that also turned out to be stressful, so she
has recently taken up horseback riding.

Today, Annie is riding her horse to the east along a long and narrow
one-way road that runs west to east. She is currently at kilometer 0 of
the road, and her destination is at kilometer D; kilometers along the road
are numbered from west to east.

There are N other horses traveling east on the same road; all of them
will go on traveling forever, and all of them are currently between Annie's
horse and her destination. The i-th of these horses is initially at
kilometer Ki and is traveling at its maximum speed of Si kilometers per
hour.

Horses are very polite, and a horse H1 will not pass (move ahead of)
another horse H2 that started off ahead of H1. (Two or more horses can
share the same position for any amount of time; you may consider the
horses to be single points.) Horses (other than Annie's) travel at their
maximum speeds, except that whenever a horse H1 catches up to
another slower horse H2, H1 reduces its speed to match the speed of H2.

Annie's horse, on the other hand, does not have a maximum speed and
can travel at any speed that Annie chooses, as long as it does not pass
another horse. To ensure a smooth ride for her and her horse, Annie
wants to choose a single constant "cruise control" speed for her horse
for the entire trip, from her current position to the destination, such that
her horse will not pass any other horses. What is the maximum such
speed that she can choose?

Input

The first line of the input gives the number of test cases, T; T test cases
follow. Each test case begins with two integers D and N: the destination
position of all of the horses (in kilometers) and the number of other
horses on the road. Then, N lines follow. The i-th of those lines has two
integers Ki and Si: the initial position (in kilometers) and maximum speed
(in kilometers per hour) of the i-th of the other horses on the road.

Output

For each test case, output one line containing Case #x: y, where x is
the test case number (starting from 1) and y is the maximum constant
speed (in kilometers per hour) that Annie can use without colliding with
other horses. y will be considered correct if it is within an absolute or
relative error of 10-6 of the correct answer. See the FAQ for an
explanation of what that means, and what formats of real numbers we
accept.

Limits
1 ≤ T ≤ 100.
0 < Ki < D ≤ 109, for all i.
Ki ≠ Kj, for all i ≠ j. (No two horses start in the same position.)
1 ≤ Si ≤ 10000.

Small dataset

1 ≤ N ≤ 2.

Large dataset

1 ≤ N ≤ 1000.

Sample

Input Output

3 Case #1: 101.000000


2525 1 Case #2: 100.000000
2400 5 Case #3: 33.333333
300 2
120 60
60 90
100 2
80 100
70 10

In sample case #1, there is one other (very slow!) horse on the road; it
will reach Annie's destination after 25 hours. Anything faster than 101
kilometers per hour would cause Annie to pass the horse before
reaching the destination.

In sample case #2, there are two other horses on the road. The faster
horse will catch up to the slower horse at kilometer 240 after 2 hours.
Both horses will then go at the slower horse's speed for 1 more hour,
until the horses reach Annie's destination at kilometer 300. The
maximum speed that Annie can choose without passing another horse is
100 kilometers per hour.

6:Problem
You are catering a party for some children, and you are serving them a
cake in the shape of a grid with R rows and C columns. Your assistant
has started to decorate the cake by writing every child's initial in icing on
exactly one cell of the cake. Each cell contains at most one initial, and
since no two children share the same initial, no initial appears more than
once on the cake.

Each child wants a single rectangular (grid-aligned) piece of cake that


has their initial and no other child's initial(s). Can you find a way to
assign every blank cell of the cake to one child, such that this goal is
accomplished? It is guaranteed that this is always possible. There is no
need to split the cake evenly among the children, and one or more of
them may even get a 1-by-1 piece; this will be a valuable life lesson
about unfairness.

Input

The first line of the input gives the number of test cases, T. T test cases
follow. Each begins with one line with two integers R and C. Then, there
are R more lines of C characters each, representing the cake. Each
character is either an uppercase English letter (which means that your
assistant has already added that letter to that cell) or ? (which means
that that cell is blank).

Output

For each test case, output one line containing Case #x: and nothing
else. Then output R more lines of C characters each. Your output grid
must be identical to the input grid, but with every ? replaced with an
uppercase English letter, representing that that cell appears in the slice
for the child who has that initial. You may not add letters that did not
originally appear in the input. In your grid, for each letter, the region
formed by all the cells containing that letter must be a single grid-aligned
rectangle.

If there are multiple possible answers, you may output any of them.

Limits

1 ≤ T ≤ 100.
There is at least one letter in the input grid.
No letter appears in more than one cell in the input grid.
It is guaranteed that at least one answer exists for each test case.
Small dataset

1 ≤ R ≤ 12.
1 ≤ C ≤ 12.
R × C ≤ 12.

Large dataset

1 ≤ R ≤ 25.
1 ≤ C ≤ 25.

Sample

Input Output

3 Case #1:
3 3 GGJ
G?? CCJ
?C? CCJ
??J Case #2:
3 4 CODE
CODE COAE
???? JJAM
?JAM Case #3:
2 2 CA
CA KE
KE

The sample output displays one set of answers to the sample cases.
Other answers may be possible.

7:Problem
Last year, the Infinite House of Pancakes introduced a new kind of
pancake. It has a happy face made of chocolate chips on one side (the
"happy side"), and nothing on the other side (the "blank side").

You are the head cook on duty. The pancakes are cooked in a single
row over a hot surface. As part of its infinite efforts to maximize
efficiency, the House has recently given you an oversized pancake
flipper that flips exactly K consecutive pancakes. That is, in that range
of K pancakes, it changes every happy-side pancake to a blank-side
pancake, and vice versa; it does not change the left-to-right order of
those pancakes.

You cannot flip fewer than K pancakes at a time with the flipper, even at
the ends of the row (since there are raised borders on both sides of the
cooking surface). For example, you can flip the first K pancakes, but not
the first K - 1 pancakes.

Your apprentice cook, who is still learning the job, just used the old-
fashioned single-pancake flipper to flip some individual pancakes and
then ran to the restroom with it, right before the time when customers
come to visit the kitchen. You only have the oversized pancake flipper
left, and you need to use it quickly to leave all the cooking pancakes
happy side up, so that the customers leave feeling happy with their visit.

Given the current state of the pancakes, calculate the minimum number
of uses of the oversized pancake flipper needed to leave all pancakes
happy side up, or state that there is no way to do it.

Input

The first line of the input gives the number of test cases, T. T test cases
follow. Each consists of one line with a string S and an
integer K. S represents the row of pancakes: each of its characters is
either + (which represents a pancake that is initially happy side up)
or - (which represents a pancake that is initially blank side up).

Output

For each test case, output one line containing Case #x: y, where x is
the test case number (starting from 1) and y is either IMPOSSIBLE if
there is no way to get all the pancakes happy side up, or an integer
representing the the minimum number of times you will need to use the
oversized pancake flipper to do it.

Limits

1 ≤ T ≤ 100.
Every character in S is either + or -.
2 ≤ K ≤ length of S.

Small dataset
2 ≤ length of S ≤ 10.

Large dataset

2 ≤ length of S ≤ 1000.

Sample

Input Output

3 Case #1: 3
---+-++- 3 Case #2: 0
+++++ 4 Case #3: IMPOSSIBLE
-+-+- 4

In Case #1, you can get all the pancakes happy side up by first flipping
the leftmost 3 pancakes, getting to ++++-++-, then the rightmost 3,
getting to ++++---+, and finally the 3 pancakes that remain blank side
up. There are other ways to do it with 3 flips or more, but none with
fewer than 3 flips.

In Case #2, all of the pancakes are already happy side up, so there is no
need to flip any of them.

In Case #3, there is no way to make the second and third pancakes from
the left have the same side up, because any flip flips them both.
Therefore, there is no way to make all of the pancakes happy side up.

8:Problem
In this problem, a valid regular expression is one of the following. In the
following descriptions, E1, E2, etc. denote (not necessarily different) valid
regular expressions.

• A decimal digit: that is, one of 0 1 2 3 4 5 6 7 8 9.


• Concatenation: E1E2.
• Disjunction: (E1|E2 |...|EN), for at least two expressions. Note
that the outer parentheses are required.
• Repetition: (E1)*. Note that the outer parentheses are required.

For example, 7, 23, (7)*, (45)*, (1|2|3), ((2)*|3), (1|2|3),


and ((0|1))* are valid expressions. (7), 4|5, 4*, (1|),
and (0|1)* are not.

We say that an expression E matches a string of digits D if and only if at


least one of the following is true:

• E = D.
• E = E1E2 and there exist D1 and D2 such
that D = D1D2 and Ei matches Di.
• E = (E1| E2|...| EN) and at least one of the Ei matches D.
• E = (E1)* and there exist D1, D2, ..., DN for some non-negative
integer N such that D = D1D2...DN and E1 matches each of the Di.
In particular, note that (E1)* matches the empty string.

For example, the expression ((1|2))*3 matches 3, 13, 123,


and 2221123, among other strings. However, it
does not match 1234, 3123, 12, or 33, among other strings.

Given a valid regular expression R, for how many integers


between A and B, inclusive, does R match the integer's base 10
representation (with no leading zeroes)?

Input

The first line of the input gives the number of test cases, T. T test cases
follow; each consists of two lines. The first line has two positive
integers A and B: the inclusive limits of the integer range we are
interested in. The second has a string R consisting only of characters in
the set 0123456789()|*, which is guaranteed to be a valid regular
expression as described in the statement above.

Output

For each test case, output one line containing Case #x: y, where x is
the test case number (starting from 1) and y is the number of integers in
the inclusive range [A, B] that the the regular expression R matches.

Limits
1 ≤ T ≤ 100.
1 ≤ A ≤ B ≤ 1018.
1 ≤ length of R ≤ 30.

Small dataset

R contains no | characters.

Large dataset

No additional limits.

Sample

Input Output

8 Case #1: 4
1 1000 Case #2: 1
(0)*1(0)* Case #3: 5
379009 379009 Case #4: 0
379009 Case #5: 4
1 10000 Case #6: 2
(12)*(34)* Case #7:
4 5 1000000000000000000
45 Case #8: 6
1 100
((0|1))*
1 50
(01|23|45|67|23)
1 1000000000000000000
((0|1|2|3|4|5|6|7|8|9))*
1 1000
1(56|(((7|8))*9)*)

Note that sample cases 5 through 8 would not appear in the Small
dataset.

In sample case 1, the matches in range are 1, 10, 100, and 1000.

In sample case 2, the match in range is 379009.


In sample case 3, the matches in range are 12, 34, 1212, 1234, and
3434.

In sample case 4, there are no matches in range.

In sample case 5, the matches in range are 1, 10, 11, and 100.

In sample case 6, the matches in range are 23 and 45.

In sample case 7, it is possible to form any number in the range.

In sample case 8, the matches in range are 1, 19, 156, 179, 189, and
199.

9:Problem
You are taking a programming course which is graded using problem
sets of different types. The course goes for a positive even number of
days. You start the course with no problem sets. On each day of the
course, you must do exactly one of the following:

• Request a "Coding" problem set.


• Request a "Jamming" problem set.
• Submit a problem set for grading. You must have at least one
problem set to choose this option. If you have multiple problem
sets, you must submit the one among those that was requested
most recently, regardless of its type.

All problem sets are different. There is no requirement on how many


sets of each type must be submitted. Once you submit a set, you no
longer have that set. Any problem sets that you have not submitted
before the end of the course get you no points.

The problem sets are requested from and submitted to an artificially-


intelligent teaching assistant. Strangely, the assistant has different
moods — on each day it is in the mood for either "Coding" or "Jamming".

• When you request a problem set:


o If the requested topic matches the assistant's mood, it
assigns a problem set worth a maximum of 10 points.
o If the requested topic does not match its mood, it assigns a
problem set worth a maximum of 5 points.
• When you submit a problem set:
o If the topic of the submitted set matches the assistant's mood
that day, it gives you the maximum number of points for that
set.
o If the topic of the submitted set does not match its mood that
day, it gives you 5 points fewer than the maximum number of
points for that set.

For example:

• If you request a "Coding" problem set on a day in which the


assistant is in a "Coding" mood, and submit it on a day in which it
is in a "Jamming" mood, you will earn 5 points: the problem set is
worth a maximum of 10, but the assistant gives 5 points fewer than
that.
• If you request a "Jamming" problem set on a day in which the
assistant is in a "Coding" mood, and submit it on a day in which it
is in a "Jamming" mood, you will earn 5 points: the set is worth a
maximum of 5, and the assistant gives you the maximum number
of points.

Thanks to some help from a senior colleague who understands the


assistant very well, you know what sort of mood the assistant will be in
on each day of the course. What is the maximum total score that you will
be able to obtain?

Input

The first line of the input gives the number of test cases, T; T test cases
follow. Each test case consists of one line with a
string S of C and/or J characters. The i-th character of S denotes the
assistant's mood on the i-th day of the course. If it is C, it is in the mood
for "Coding"; if it is J, it is in the mood for "Jamming".

Output
For each test case, output one line containing Case #x: y, where x is
the test case number (starting from 1) and y is the maximum number of
points you can obtain for that case.

Limits

1 ≤ T ≤ 100.
The length of S is even.

Small dataset

2 ≤ the length of S ≤ 50.

Large dataset

2 ≤ the length of S ≤ 20000.


The sum of lengths of all S in the dataset is at most 150000.

Sample

Input Output

5 Case #1: 20
CCJJ Case #2: 10
CJCJ Case #3: 20
CJJC Case #4: 15
CJJJ Case #5: 30
CCCCCC

This strategy is optimal for sample case #1:


Day 1: Request a "Coding" problem set (call it C1).
Day 2: Submit C1.
Day 3: Request a "Jamming" problem set (call it J1).
Day 4: Submit J1.

The following strategy is optimal for sample cases #2, #3, and #4:
request C1, request J1, submit J1, submit C1.

For case #2, for example, note that you could not request C1, request
J1, and then submit C1. Only the most recently requested problem set
can be submitted.
In sample case #5, you can alternate between requesting a "Coding"
problem set on one day, and submitting it on the next day.

10:Problem
You've been asked to organize a Rock-Paper-Scissors tournament. The
tournament will have a single-elimination format and will run
for N rounds; 2N players will participate.

Initially, the players will be lined up from left to right in some order that
you specify. In each round, the first and second players in the lineup
(starting from the left) will play a match against each other, and the third
and fourth players in the lineup (if they exist) will play a match against
each other, and so on; all of these matches will occur simultaneously.
The winners of these matches will remain in the lineup, in the same
relative order, and the losers will leave the lineup and go home. Then a
new round will begin. This will continue until only one player remains in
the lineup; that player will be declared the winner.

In each Rock-Paper-Scissors match, each of the two players secretly


chooses one of Rock, Paper, or Scissors, and then they compare their
choices. Rock beats Scissors, Scissors beats Paper, and Paper beats
Rock. If one player's choice beats the other players's choice, then that
player wins and the match is over. However, if the players make the
same choice, then it is a tie, and they must choose again and keep
playing until there is a winner.

You know that the players this year are stubborn and not very strategic.
Each one has a preferred move and will only play that move in every
match, regardless of what the opponent does. Because of this, if two
players with the same move go up against each other, they will keep
tying and their match will go on forever! If this happens, the tournament
will never end and you will be a laughingstock.

This year, there are R players who prefer Rock, P players who prefer
Paper, and S players who prefer Scissors. Knowing this, you want to
create a lineup that guarantees that the tournament will go to completion
and produce a single winner — that is, no match will ever be a tie. Your
boss has asked you to produce a list of all such lineups (written in left to
right order, with R, P, and S standing for players who prefer Rock, Paper,
and Scissors, respectively), and then put that list in alphabetical order.

You know that the boss will lazily pick the first lineup on the list; what will
that be? Or do you have to tell your boss that it is IMPOSSIBLE to
prevent a tie?

Input

The first line of the input gives the number of test cases, T. T lines
follow; each represents one test case. Each test case consists of four
integers: N, R, P, and S, as described in the statement above.

Output

For each test case, output one line containing Case #x: y, where x is
the test case number (starting from 1) and y is either IMPOSSIBLE or a
string of length 2N representing the alphabetically earliest starting lineup
that solves the problem. Every character in a lineup must be R, P, or S,
and there must be R Rs, P Ps, and S Ss.

Limits

R + P + S = 2N.
0 ≤ R ≤ 2N.
0 ≤ P ≤ 2N.
0 ≤ S ≤ 2N.

Small dataset

1 ≤ T ≤ 25.
1 ≤ N ≤ 3.

Large dataset

1 ≤ T ≤ 75.
1 ≤ N ≤ 12.

Sample

Input Output

4 Case #1: PR
1 1 1 0 Case #2: IMPOSSIBLE
1 2 0 0 Case #3: PSRS
2 1 1 2 Case #4: IMPOSSIBLE
2 2 0 2

In sample case #1, there are only two players and the tournament will
consist of one round. It doesn't matter what order the two line up in; the
Paper-using player will defeat the Rock-using player. You will give your
boss the alphabetically ordered list PR, RP, and the first element is PR.

In sample case #2, the only two players both play Rock, so a tie is
unavoidable.

In sample case #3, there are four players and the tournament will go on
for two rounds. In the first round, the first player (Paper) will lose to the
second player (Scissors), and the third player (Rock) will defeat the
fourth player (Scissors). The second round lineup will be PR, and the first
remaining player (Paper) will defeat the other remaining player (Rock),
so the tournament will end with a winner and no ties.

Here is an illustration of the tournament for sample case #3:

Other lineups such as PSSR will appear on the list you give to your boss,
but PSRS is alphabetically first.

In sample case #4, the only way to organize the first round such that
there are no ties is to create two matches with one Rock player and one
Scissors player. But both of those matches will have a Rock winner, and
when these two winners go on to face each other, there will be a tie.
11:Problem
A small fire started in the senate room, and it needs to be evacuated!

There are some senators in the senate room, each of whom belongs to
of one of N political parties. Those parties are named after the
first N (uppercase) letters of the English alphabet.

The emergency door is wide enough for up to two senators, so in each


step of the evacuation, you may choose to remove either one or two
senators from the room.

The senate rules indicate the senators in the room may vote on any bill
at any time, even in the middle of an evacuation! So, the senators must
be evacuated in a way that ensures that no party ever has an absolute
majority. That is, it can never be the case after any evacuation step that
more than half of the senators in the senate room belong to the same
party.

Can you construct an evacuation plan? The senate is counting on you!

Input

The first line of the input gives the number of test cases, T. T test cases
follow. Each test case consists of two lines. The first line contains a
single integer N, the number of parties. The second line
contains N integers, P1, P2, ..., PN, where Pi represents the number of
senators of the party named after the i-th letter of the alphabet.

Output

For each test case, output one line containing Case #x: y, where x is
the test case number (starting from 1) and y is the evacuation plan. The
plan must be a space-separated list of instructions, in the order in which
they are to be carried out, where each instruction is either one or two
characters, representing the parties of the senators to evacuate in each
step.

It is guaranteed that at least one valid evacuation plan will exist. If


multiple evacuation plans are valid, you may output any of them.

Limits
1 ≤ T ≤ 50.
No party will have an absolute majority before the start of the
evacuation.
1 ≤ Pi ≤ 1000, for all i.

Small dataset

2 ≤ N ≤ 3.
sum of all Pi ≤ 9.

Large dataset

2 ≤ N ≤ 26.
sum of all Pi ≤ 1000.

Sample

Input Output

4 Case #1: AB BA
2 Case #2: AA BC C BA
2 2 Case #3: C C AB
3 Case #4: BA BB CA
3 2 2
3
1 1 2
3
2 3 1

The sample output displays one set of answers to the sample cases.
Other answers may be possible.

In Case #1, there are two senators from each of the parties A and B. If
we remove one from each party every time, the perfect balance is
maintained until evacuation is complete.

Case #2 proceeds as follows:

Initially in the room: 3 A, 2 B, 2 C.


Evacuate AA. Still in the room: 1 A, 2 B, 2 C.
Evacuate BC. Still in the room: 1 A, 1 B, 1 C.
Evacuate C. Still in the room: 1 A, 1 B.
Evacuate AB. Evacuation complete!

Note that it would not be valid to begin the evacuation with BC, which
would leave 3 A, 1 B, and 1 C in the room; party A would have an
absolute majority (3 out of 5 = 60%).

For Case #3, note that CC AB would also be a valid answer, and C C
AB is also valid even though it requires three evacuation steps instead of
two.

12:Problem
You just made a new friend at an international puzzle conference, and
you asked for a way to keep in touch. You found the following note
slipped under your hotel room door the next day:

"Salutations, new friend! I have replaced every digit of my phone number


with its spelled-out uppercase English representation ("ZERO", "ONE",
"TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"
for the digits 0 through 9, in that order), and then reordered all of those
letters in some way to produce a string S. It's up to you to use S to figure
out how many digits are in my phone number and what those digits are,
but I will tell you that my phone number consists of those digits in
nondecreasing order. Give me a call... if you can!"

You would to like to call your friend to tell him that this is an obnoxious
way to give someone a phone number, but you need the phone number
to do that! What is it?

Input

The first line of the input gives the number of test cases, T. T test cases
follow. Each consists of one line with a string S of uppercase English
letters.

Output
For each test case, output one line containing Case #x: y, where x is
the test case number (starting from 1) and y is a string of digits: the
phone number.

Limits

1 ≤ T ≤ 100.
A unique answer is guaranteed to exist.

Small dataset

3 ≤ length of S ≤ 20.

Large dataset

3 ≤ length of S ≤ 2000.

Sample

Input Output

4 Case #1: 012


OZONETOWER Case #2: 2468
WEIGHFOXTOURIST Case #3: 114
OURNEONFOE Case #4: 3
ETHER

13:Problem

On the game show The Last Word, the host begins a round by showing
the contestant a string S of uppercase English letters. The contestant
has a whiteboard which is initially blank. The host will then present the
contestant with the letters of S, one by one, in the order in which they
appear in S. When the host presents the first letter, the contestant writes
it on the whiteboard; this counts as the first word in the game (even
though it is only one letter long). After that, each time the host presents a
letter, the contestant must write it at the beginning or the end of the word
on the whiteboard before the host moves on to the next letter (or to the
end of the game, if there are no more letters).
For example, for S = CAB, after writing the word C on the whiteboard, the
contestant could make one of the following four sets of choices:

• put the A before C to form AC, then put the B before AC to form BAC
• put the A before C to form AC, then put the B after AC to form ACB
• put the A after C to form CA, then put the B before CA to form BCA
• put the A after C to form CA, then put the B after CA to form CAB

The word is called the last word when the contestant finishes writing all
of the letters from S, under the given rules. The contestant wins the
game if their last word is the last of an alphabetically sorted list of all of
the possible last words that could have been produced. For the example
above, the winning last word is CAB (which happens to be the same as
the original word). For a game with S = JAM, the winning last word
is MJA.

You are the next contestant on this show, and the host has just showed
you the string S. What's the winning last word that you should produce?

Input

The first line of the input gives the number of test cases, T. T test cases
follow. Each consists of one line with a string S.

Output

For each test case, output one line containing Case #x: y, where x is
the test case number (starting from 1) and y is the winning last word, as
described in the statement.

Limits

1 ≤ T ≤ 100.

Small dataset

1 ≤ length of S ≤ 15.

Large dataset

1 ≤ length of S ≤ 1000.

Sample
Input Output

7 Case #1: CAB


CAB Case #2: MJA
JAM Case #3: OCDE
CODE Case #4: BBAAA
ABAAB Case #5: CCCABBBAB
CABCBBABC Case #6: CCCBAABAB
ABCABCABC Case #7: ZXCASDQWE
ZXCASDQWE

14:Problem
This problem is not part of the 2018 World Finals. You will not earn
points in the 2018 World Finals for submitting for this problem.

Bleatrix Trotter the sheep has devised a strategy that helps her fall
asleep faster. First, she picks a number N. Then she starts naming N, 2
× N, 3 × N, and so on. Whenever she names a number, she thinks about
all of the digits in that number. She keeps track of which digits (0, 1, 2, 3,
4, 5, 6, 7, 8, and 9) she has seen at least once so far as part of any
number she has named. Once she has seen each of the ten digits at
least once, she will fall asleep.

Bleatrix must start with N and must always name (i + 1) × N directly


after i × N. For example, suppose that Bleatrix picks N = 1692. She
would count as follows:

• N = 1692. Now she has seen the digits 1, 2, 6, and 9.


• 2N = 3384. Now she has seen the digits 1, 2, 3, 4, 6, 8, and 9.
• 3N = 5076. Now she has seen all ten digits, and falls asleep.

What is the last number that she will name before falling asleep? If she
will count forever, print INSOMNIA instead.

Input

The first line of the input gives the number of test cases, T. T test cases
follow. Each consists of one line with a single integer N, the number
Bleatrix has chosen.
Output

For each test case, output one line containing Case #x: y, where x is
the test case number (starting from 1) and y is the last number that
Bleatrix will name before falling asleep, according to the rules described
in the statement.

Limits

1 ≤ T ≤ 100.

Small dataset

0 ≤ N ≤ 200.

Large dataset

0 ≤ N ≤ 106.

Sample

Input Output

5 Case #1: INSOMNIA


0 Case #2: 10
1 Case #3: 90
2 Case #4: 110
11 Case #5: 5076
1692

In Case #1, since 2 × 0 = 0, 3 × 0 = 0, and so on, Bleatrix will never see


any digit other than 0, and so she will count forever and never fall
asleep. Poor sheep!

In Case #2, Bleatrix will name 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. The 0 will be


the last digit needed, and so she will fall asleep after 10.

In Case #3, Bleatrix will name 2, 4, 6... and so on. She will not see the
digit 9 in any number until 90, at which point she will fall asleep. By that
point, she will have already seen the digits 0, 1, 2, 3, 4, 5, 6, 7, and 8,
which will have appeared for the first time in the numbers 10, 10, 2, 30,
4, 50, 6, 70, and 8, respectively.
In Case #4, Bleatrix will name 11, 22, 33, 44, 55, 66, 77, 88, 99, 110 and

then fall asleep.

Case #5 is the one described in the problem statement. Note that it


would only show up in the Large dataset, and not in the Small dataset.

15:Problem
You were asked to implement arguably the most important algorithm of
all: binary search. More precisely, you have a sorted array of objects,
and a new object that you want to insert into the array. In order to find
the insertion position, you can compare your object with the objects in
the array. Each comparison can return either "greater", meaning that
your object should be inserted to the right of the compared object, or
"less", meaning that your object should be inserted to the left of the
compared object. For simplicity, comparisons never return "equal" in this
problem. It is guaranteed that when your object is greater than some
object in the array, it is also greater than all objects to the left of that
object; similarly, when your object is less than some object of the array,
it is also less than all objects to the right of that object. If the array
has n elements, there are n+1 possible outcomes for your algorithm.

In this problem, not all comparisons have the same cost. More precisely,
comparing your object with i-th object in the array costs ai, an integer
between 1 and 9, inclusive.

What will be the total cost, in the worst case, of your binary search?
Assume you follow an optimal strategy and try to minimize the total cost
in the worst case.

Input

The first line of the input gives the number of test cases, T. T lines
follow. Each of those lines contains one sequence of digits describing
the comparison costs ai for one testcase. The size of the array n is given
by the length of this sequence.

Output
For each test case, output one line containing "Case #x: y", where x is
the test case number (starting from 1) and y is the total binary search
cost in the worst case.

Limits

1 ≤ T ≤ 50.
All digits are between 1 and 9, inclusive.
There are no spaces between digits on one line.

Small dataset

1 ≤ n ≤ 104.

Large dataset

1 ≤ n ≤ 106.

Sample

Input Output

4 Case #1: 2
111 Case #2: 3
1111 Case #3: 3
1111111 Case #4: 10
1111119

16:Problem
The country of Fairland has very strict laws governing how companies
organize and pay their employees:

1. Each company must have exactly one CEO, who has no manager.
2. Every employee except for the CEO must have exactly one
manager. (This means that the org chart showing all of the
employees in a company is a tree, without cycles.)
3. As long as an employee is working for the company, their manager
must never change. This means that if a manager leaves, then all
of the employees reporting to that manager must also leave.
4. The CEO must never leave the company.
5. Every employee receives a salary -- some amount of Fairland
dollars per year. An employee's salary must never change.
6. Different employees may have different salaries, and an
employee's salary is not necessarily correlated with where in the
org chart that employee is.
The government of Fairland has just passed one additional law:

7. The difference between the largest salary and the smallest salary
in the whole company must be at most D Fairland dollars.

Marie is the CEO of the Fairland General Stuff Corporation, and she has
to ensure that her company complies with the new law. This may require
laying off some employees. She has the list of the company's
employees, their managers, and their salaries. What is the largest
number of employees she can keep, including herself?

Input

The first line of the input gives the number of test cases, T. T test cases
follow. Each case begins with one line containing two space-separated
integers N (the number of employees) and D (the maximum allowed
salary difference). This is followed by one line with four space-separated
integers (S0, As, Cs, Rs) and then another line with four more space-
separated integers (M0, Am, Cm and Rm). These last eight integers define
the following sequences:

• Si+1 = (Si * As + Cs) mod Rs


• Mi+1 = (Mi * Am + Cm) mod Rm
Marie's employee ID is 0, and all other employees have IDs from 1 to N -
1, inclusive. The salary of employee i is Si. For every employee i other
than Marie, the manager is Mi mod i. (Note that this means that M0 does
not affect Marie's manager -- she has none!)

Output

For each test case, output one line containing "Case #x: y", where x is
the test case number (starting from 1) and y is the largest number of
employees Marie can keep at the company, including herself, such that
all of laws 1-7 are obeyed.

Limits
1 ≤ T ≤ 100.
0 ≤ S0 < Rs.
0 ≤ M0 < Rm.
0 ≤ As, Am ≤ 1000.
0 ≤ Cs, Cm ≤ 109.

Small dataset

1 ≤ N ≤ 1000.
1 ≤ D ≤ 1000.
1 ≤ Rs, Rm ≤ 1000.

Large dataset

1 ≤ N ≤ 106.
1 ≤ D ≤ 106.
1 ≤ Rs, Rm ≤ 106.

Sample

Input Output

3 Case #1: 1
1 395 Case #2: 3
18 246 615815 60 Case #3: 5
73 228 14618 195
6 5
10 1 3 17
5 2 7 19
10 13
28 931 601463 36
231 539 556432 258

In Case #1, the company has only a CEO and no other employees, but it
does not violate any of the laws, so no changes need to be made.

Here is the org chart for Case #2:


The optimal strategy is to save employees 0, 1, and 5 (who have
salaries of 10, 13, and 8, respectively). It is not possible to save
employee 2, for example, because her salary is more than 5 away from
employee 0's salary of 10; since employee 0 cannot be laid off,
employee 2 must be laid off (along with all employees who report to her).

If you want to check your sequences for employees 1 through 5, they


are:

S: 13, 16, 2, 5, 8
M: 17, 3, 13, 14, 16
Manager numbers: 17 % 1 = 0, 3 % 2 = 1, 13 % 3 = 1, 14 % 4 = 2, 16 %
5=1

17:Problem
While using Google Street View, you may have picked up and dropped
the character Pegman before. Today, a mischievous user is going to
place Pegman in some cell of a rectangular grid of unit cells with R rows
and C columns. Each of the cells in this grid might be blank, or it might
be labeled with an arrow pointing in one of four possible directions: up,
right, down, or left.

When Pegman is placed on a grid cell, if that cell is blank, Pegman


stands still forever. However, if that cell has an arrow, Pegman starts to
walk in that direction. As he walks, whenever he encounters a blank cell,
he just keeps walking in his current direction, but whenever he
encounters another arrow, he changes to the direction of that arrow and
then keeps walking.

You know that it is possible that Pegman might keep happily walking
around and around the grid forever, but it is also possible that Pegman's
walk will take him over the edge of the grid! You may be able to prevent
this and save him by changing the direction of one or more arrows.
(Each arrow's direction can only be changed to one of the other three
possible directions; arrows can only be changed, not added or
removed.)

What is the smallest number of arrows you will need to change to ensure
that Pegman will not walk off the edge, no matter where on the grid he is
initially placed?

Input

The first line of the input gives the number of test cases, T. T test cases
follow. Each begins with one line with two space-separated
integers R, C. This line is followed by R lines, each of which
has C characters, each of which describes a grid cell and is one of the
following:

. period = no arrow
^ caret = up arrow
> greater than = right arrow
v lowercase v = down arrow
< less than = left arrow

Output

For each test case, output one line containing "Case #x: y", where x is
the test case number (starting from 1) and y is the minimum number of
arrows that must be changed to ensure that Pegman will not leave the
grid no matter where he is initially placed, or the text IMPOSSIBLE if it is
not possible to ensure this, no matter how many arrows you change.

Limits

1 ≤ T ≤ 100.

Small dataset

1 ≤ R, C ≤ 4.

Large dataset

1 ≤ R, C ≤ 100.
Sample

Input Output

4 Case #1: 1
2 1 Case #2: 0
^ Case #3: IMPOSSIBLE
^ Case #4: 0
2 2
>v
^<
3 3
...
.^.
...
1 1
.

In Case #1, Pegman is guaranteed to walk off the top edge of the grid,
no matter where he is placed. You can prevent that by changing the
topmost arrow to point down, which will cause him to walk back and forth
between those two arrows forever.

In Case #2, no matter where Pegman is placed, he will walk around and
around the board clockwise in a circle. No arrows need to be changed.

In Case #3, the mischievous user might place Pegman on the up arrow
in the middle of the grid, in which case he will start walking and then
walk off the top edge of the grid. Changing the direction of this arrow
won't help: it would just make him walk off a different edge.

In Case #4, the only possible starting cell is blank, so Pegman will stand
still forever and is in no danger.

18:Problem
You're about to play a simplified "battleship" game with your little
brother. The board for this game is a rectangular grid with R rows
and C columns. At the start of the game, you will close your eyes, and
you will keep them closed until the end of the game. Your little brother
will take a single rectangular 1 x W ship and place
it horizontally somewhere on the board. The ship must always fit
entirely on the board, with each cell of the ship occupying exactly one of
the grid's cells, and it can never be rotated.

In each turn of the game, you name a cell on the board, and your little
brother tells you whether that is a hit (one of the cells occupied by the
ship) or a miss. (Your little brother doesn't say which part of the ship was
hit -- just that the cell you named has a part of the ship in it.) You have
perfect memory, and can keep track of all the information he has given
you. Once you have named all of the cells occupied by the ship, the
game is over (the ship is sunk), and your score is the number of turns
taken. Your goal is to minimize your score.

Although the ship is not supposed to be moved once it is placed, you


know that your little brother, who is a brat, plans to cheat by changing
the location of the ship whenever he wants, as long as the ship remains
horizontal and completely on the board, and the new location is
consistent with all the information he has given so far. For example, for a
1x4 board and 1x2 ship, your little brother could initially place the ship
such that it overlaps the leftmost two columns. If your first guess was
row 1, column 2, he could choose to secretly move the ship to the
rightmost two columns, and tell you that (1, 2) was a miss. If your next
guess after that was (1, 3), though, then he could not say that was also a
miss and move the ship back to its original location, since that would be
inconsistent with what he said about (1, 2) earlier.

Not only do you know that your little brother will cheat, he knows that
you know. If you both play optimally (you to minimize your score, him to
maximize it), what is the lowest score that you can guarantee you will
achieve, regardless of what your little brother does?

Input

The first line of the input gives the number of test cases, T. T lines
follow, each with three space-separated integers R, C, and W: the
number of rows and columns of the board, followed by the width of the
ship.

Output
For each test case, output one line containing "Case #x: y", where x is
the test case number (starting from 1) and y is the minimum score you
can guarantee.

Limits

1 ≤ W ≤ C.

Small dataset

T = 55.
R = 1.
1 ≤ C ≤ 10.

Large dataset

1 ≤ T ≤ 100.
1 ≤ R ≤ 20.
1 ≤ C ≤ 20.

Sample

Input Output

3 Case #1: 3
1 4 2 Case #2: 7
1 7 7 Case #3: 10
2 5 1

In Case #1, the board has one row and four columns, and the ship takes
up one row and two columns. One optimal strategy is for you to start by
naming cell (1, 2):

If your little brother says it is a hit, then the other cell of the 1x2 ship
must be in either (1, 1) or (1, 3), and you just have to name both. If you
happen to correctly name the cell where the other part of the ship is,
your little brother will just reposition the ship so that (1, 2) is still hit, but
your guess is a miss. Notice that your little brother can still move the
ship even after it has been hit, as long as the new position is not
inconsistent with the information he has already given.

If your little brother says it is a miss, then the only remaining consistent
scenario is that the ship is in (1, 3) and (1, 4), and your little brother will
be unable to change this from now on; you just need to name those two
cells.

So no matter what your little brother does after you say (1, 2), you can
finish the game in two more moves after that, for a total of three moves.

Moreover, a three-move solution is optimal, because it is impossible to


guarantee a finish in only two moves: without loss of generality, pick a
first move. No matter what you pick, there is still a 1x2 area open and
your little brother can just move the ship there and claim that you
missed. It is impossible for you to sink that ship, which has not yet been
hit, with only one more move.

In Case #2, the ship completely fills in the board and so your little
brother has only one place to put it. All you have to do is name every
cell.

In Case #3, your little brother can always move the 1x1 ship to a cell you
have not tried yet, so you must name all 10 cells, only finally getting a hit
(and immediately sinking the ship) on the last one.

19:Problem
In the Counting Poetry Slam, a performer takes the microphone,
chooses a number N, and counts aloud from 1 to N. That is, she starts
by saying 1, and then repeatedly says the number that is 1 greater than
the previous number she said, stopping after she has said N.

It's your turn to perform, but you find this process tedious, and you want
to add a twist to speed it up: sometimes, instead of adding 1 to the
previous number, you might reverse the digits of the number (removing
any leading zeroes that this creates). For example, after saying "16", you
could next say either "17" or "61"; after saying "2300", you could next
say either "2301" or "32". You may reverse as many times as you want
(or not at all) within a performance.

The first number you say must be 1; what is the fewest number of
numbers you will need to say in order to reach the number N? 1
and N count toward this total. If you say the same number multiple
times, each of those times counts separately.

Input

The first line of the input gives the number of test cases, T. T lines
follow. Each has one integer N, the number you must reach.

Output

For each test case, output one line containing "Case #x: y", where x is
the test case number (starting from 1) and y is the minimum number of
numbers you need to say.

Limits

1 ≤ T ≤ 100.

Small dataset

1 ≤ N ≤ 106.

Large dataset

1 ≤ N ≤ 1014.

Sample

Input Output

3 Case #1: 1
1 Case #2: 19
19 Case #3: 15
23

In Case #2, flipping does not help and the optimal strategy is to just
count up to 19.

In Case #3, the optimal strategy is to count up to 12, flip to 21, and then
continue counting up to 23. That is, the numbers you will say are 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 21, 22,
20:Problem
Kaylin loves mushrooms. Put them on her plate and she'll eat them up!
In this problem she's eating a plate of mushrooms, and Bartholomew is
putting more pieces on her plate.

In this problem, we'll look at how many pieces of mushroom are on her
plate at 10-second intervals. Bartholomew could put any non-negative
integer number of mushroom pieces down at any time, and the only way
they can leave the plate is by being eaten.

Figure out the minimum number of mushrooms that Kaylin could have
eaten using two different methods of computation:

1. Assume Kaylin could eat any number of mushroom pieces at any


time.
2. Assume that, starting with the first time we look at the plate, Kaylin
eats mushrooms at a constant rate whenever there are
mushrooms on her plate.

For example, if the input is 10 5 15 5:

With the first method, Kaylin must have eaten at least 15 mushroom
pieces: first she eats 5, then 10 more are put on her plate, then she eats
another 10. There's no way she could have eaten fewer pieces.

With the second method, Kaylin must have eaten at least 25 mushroom
pieces. We can determine that she must eat mushrooms at a rate of at
least 1 piece per second. She starts with 10 pieces on her plate. In the
first 10 seconds, she eats 10 pieces, and 5 more are put on her plate. In
the next 5 seconds, she eats 5 pieces, then her plate stays empty for 5
seconds, and then Bartholomew puts 15 more pieces on her plate. Then
she eats 10 pieces in the last 10 seconds.

Input

The first line of the input gives the number of test cases, T. T test cases
follow. Each will consist of one line containing a single integer N,
followed by a line containing N space-separated integers mi; the number
of mushrooms on Kaylin's plate at the start, and at 10-second intervals.

Output
For each test case, output one line containing "Case #x: y z", where x is
the test case number (starting from 1), y is the minimum number of
mushrooms Kaylin could have eaten using the first method of
computation, and z is the minimum number of mushrooms Kaylin could
have eaten using the second method of computation.

Limits

1 ≤ T ≤ 100.

Small dataset

2 ≤ N ≤ 10.
0 ≤ mi ≤ 100.

Large dataset

2 ≤ N ≤ 1000.
0 ≤ mi ≤ 10000.

Sample

Input Output

4 Case #1: 15 25
4 Case #2: 0 0
10 5 15 5 Case #3: 81 567
2 Case #4: 181 244
100 100
8
81 81 81 81 81 81 81 0
6
23 90 40 0 100 9

21:Problem
It's opening night at the opera, and your friend is the prima donna (the
lead female singer). You will not be in the audience, but you want to
make sure she receives a standing ovation -- with every audience
member standing up and clapping their hands for her.

Initially, the entire audience is seated. Everyone in the audience has


a shyness level. An audience member with shyness level Si will wait until
at least Si other audience members have already stood up to clap, and if
so, she will immediately stand up and clap. If Si = 0, then the audience
member will always stand up and clap immediately, regardless of what
anyone else does. For example, an audience member with Si = 2 will be
seated at the beginning, but will stand up to clap later after she sees at
least two other people standing and clapping.

You know the shyness level of everyone in the audience, and you are
prepared to invite additional friends of the prima donna to be in the
audience to ensure that everyone in the crowd stands up and claps in
the end. Each of these friends may have any shyness value that you
wish, not necessarily the same. What is the minimum number of friends
that you need to invite to guarantee a standing ovation?

Input

The first line of the input gives the number of test cases, T. T test cases
follow. Each consists of one line with Smax, the maximum shyness level
of the shyest person in the audience, followed by a string of Smax +
1 single digits. The kth digit of this string (counting starting from 0)
represents how many people in the audience have shyness level k. For
example, the string "409" would mean that there were four audience
members with Si = 0 and nine audience members with Si = 2 (and none
with Si = 1 or any other value). Note that there will initially always be
between 0 and 9 people with each shyness level.

The string will never end in a 0. Note that this implies that there will
always be at least one person in the audience.

Output

For each test case, output one line containing "Case #x: y", where x is
the test case number (starting from 1) and y is the minimum number of
friends you must invite.

Limits

1 ≤ T ≤ 100.

Small dataset
0 ≤ Smax ≤ 6.

Large dataset

0 ≤ Smax ≤ 1000.

Sample

Input Output

4 Case #1: 0
4 11111 Case #2: 1
1 09 Case #3: 2
5 110011 Case #4: 0
0 1

In Case #1, the audience will eventually produce a standing ovation on


its own, without you needing to add anyone -- first the audience member
with Si = 0 will stand up, then the audience member with Si = 1 will stand
up, etc.

In Case #2, a friend with Si = 0 must be invited, but that is enough to get
the entire audience to stand up.

In Case #3, one optimal solution is to add two audience members


with Si = 2.

In Case #4, there is only one audience member and he will stand up
immediately. No friends need to be invited.

22:Problem
When she is bored, Mija sometimes likes to play a game with matrices.
She tries to transform one matrix into another with the fewest moves.
For Mija, one move is swapping any two rows of the matrix or any two
columns of the matrix.

Today, Mija has a very special matrix M. M is a 2N by 2N matrix where


every entry is either a 0 or a 1. Mija decides to try and transform M into
a checkerboard matrix where the entries alternate between 0 and 1
along each row and column. Can you help Mija find the minimum
number of moves to transform M into a checkerboard matrix?

Input

The first line of the input gives the number of test cases, T. T test cases
follow. Each test case starts with a line containing a single integer: N.
The next 2N lines each contain 2N characters which are the rows of M;
each character is a 0 or 1.

Output

For each test case, output one line containing "Case #x: y", where x is
the test case number (starting from 1) and y is the minimum number of
row swaps and column swaps required to turn M into a checkerboard
matrix. If it is impossible to turn M into a checkerboard matrix, y should
be "IMPOSSIBLE".

Limits

1 ≤ T ≤ 100.

Small dataset

1 ≤ N ≤ 10.

Large dataset

1 ≤ N ≤ 103.

Sample

Input Output

3 Case #1: 0
1 Case #2: 2
01 Case #3: IMPOSSIBLE
10
2
1001
0110
0110
1001
1
00
00

In the first sample case, M is already a checkerboard matrix.

In the second sample case, Mija can turn M into a checkerboard


matrix by swapping columns 1 and 2 and then swapping rows 1 and 2.

In the third sample case, Mija can never turn M into a checkerboard
matrix; it doesn't have enough 1s.

23:Problem
The mysterious owner of an electronics factory has decided to do
something very intriguing. She has hidden golden transistors inside
seven electronic devices, and the people who buy those devices will be
invited to a magical, marvelous tour of the factory.

Arnar and Solveig have received a tip that there is a golden transistor
hidden inside one device in their local electronics store. First they pooled
their money together and bought all the devices, then placed them in a
straight line, numbering the devices 0 to N-1. Each device has some
number of transistors in it. Then they agreed on a strategy to decide who
gets the golden transistor:

First, Arnar will select a range [a, b] (inclusive) of the devices,


where 0 ≤ a ≤ b < N. Next, Solveig will choose which one set of
devices she wants to take:

• If a > 0, she may take all the devices in the range [0, a-1].
• If b < N-1, she may take all the devices in the range [b+1, N-1].

• She may always choose to take all the devices in the range [a,
b].
Once Solveig has chosen one of the sets of devices, Arnar takes all the
devices she did not take.

For example, if there are 3 devices and Arnar selects the range [1, 1],
Solveig may choose to take the range [0, 0], the range [1, 1] or the
range [2, 2]. On the other hand, if Arnar selects the range [1, 2],
then Solveig may choose to take the range [0, 0] or the range [1,
2].

Given how many transistors are in each device, and that Arnar and
Solveig will each try to maximize their probability of getting the golden
transistor (which is maximized by taking electronics with the maximum
number of transistors), what is Arnar's probability of getting the golden
transistor and thus winning the magical, marvelous tour?

Input

The first line of the input gives the number of test cases, T. T lines
follow. Each line contains five numbers: N, p, q, r and s. This indicates
that there are N devices, and the ith device contains ((i * p + q)
MOD r + s) transistors. Remember that the devices are numbered from
0 to N-1.

Output

For each test case, output one line containing "Case #x: y", where x is
the test case number (starting from 1) and y is Arnar's probability of
winning the magical, marvelous tour.

y will be considered correct if it is within an absolute or relative error of


10-9 of the correct answer. See the FAQ for an explanation of what that
means, and what formats of real numbers we accept.

Limits

1 ≤ T ≤ 100.
1 ≤ p ≤ 106.
1 ≤ q ≤ 106.
1 ≤ r ≤ 106.
1 ≤ s ≤ 106.

Small dataset

1 ≤ N ≤ 1000.

Large dataset

1 ≤ N ≤ 106.

Sample
Input

8
1 1 1 1 1
10 17 1 7 1
2 100 100 200 1
20 17 3 23 100
10 999999 999999 1000000 1000000
2 1 1 1 1
3 1 99 100 1
999999 1000000 999999 1000000 1000000

Output

Case #1: 0.0000000000


Case #2: 0.6111111111
Case #3: 0.0098039216
Case #4: 0.6471920290
Case #5: 0.6000006000
Case #6: 0.5000000000
Case #7: 0.0291262136
Case #8: 0.6666666667

Note that the last sample case does not meet the limits for the Small
dataset. You could have a correct solution for the Small dataset that
returns the wrong answer, or runs for a very long time, on the last
sample case.

Explanation of Sample Cases

In the first sample case, there is one electronic device with one
transistor. Arnar must select the range [0, 0], and Solveig must choose
to take all the devices in the range [0, 0]. Arnar can't possibly win the
magical, marvelous tour.

In the second sample case, there are ten electronic devices, with the
following numbers of transistors: [2, 5, 1, 4, 7, 3, 6, 2, 5,
1]. Arnar will choose the range [4, 5], which contains the devices with 7
and 3 transistors. Solveig will choose the range [6, 9], which contains the
devices with 6, 2, 5 and 1 transistors, leaving Arnar with the first six
devices, and a probability of 22/36 of winning the tour.

In the third sample case, the devices have 101 and 1 transistors.

In the fourth sample case, the devices have the following numbers of
transistors: [103, 120, 114, 108, 102, 119, 113, 107, 101,
118, 112, 106, 100, 117, 111, 105, 122, 116, 110,
104].

In the fifth sample case, the devices have the following numbers of
transistors: [1999999, 1999998, 1999997, 1999996, 1999995,
1999994, 1999993, 1999992, 1999991, 1999990].

In the sixth sample case, the devices both have 1 transistor.

In the seventh sample case, the devices have the following numbers of
transistors: [100, 1, 2].

24:Problem
Adam, being a well-organized man, has always been keenly interested
in organizing all his stuff. In particular, he fondly remembers the many
hours of his youth that were spent moving files from his computer onto
Compact Discs.

There were two very important rules involved in this procedure. First, in
order to ensure that all discs could be labeled clearly, Adam would never
place more than two files on the same disc. Second, he would never
divide a single file over multiple discs. Happily, the discs he was using
were always large enough to make this possible.

Thinking back, Adam is now wondering whether he arranged his files in


the best way, or whether he ended up wasting some Compact Discs. He
will provide you with the capacity of the discs he used (all his discs had
the same capacity) as well as a list of the sizes of the files that he
stored. Please help Adam out by determining the minimum number of
discs needed to store all his files—following the two very important rules,
of course.
Input

The first line of the input gives the number of test cases, T. T test cases
follow. Each test case begins with a line containing two integers: the
number of files to be stored N, and the capacity of the discs to be
used X (in MBs). The next line contains the N integers representing the
sizes of the files Si (in MBs), separated by single spaces.

Output

For each test case, output one line containing "Case #x: y", where x is
the case number (starting from 1) and y is the minimum number of discs
needed to store the given files.

Limits

1 ≤ T ≤ 100.
1 ≤ X ≤ 700.
1 ≤ Si ≤ X.

Small dataset

1 ≤ N ≤ 10.

Large dataset

1 ≤ N ≤ 104

Sample

Input Output

3 Case #1:
3 100 2
10 20 70 Case #2:
4 100 2
30 40 60 70 Case #3:
5 100 3
10 20 30 40
60
25:Problem
Vida says she's part Elf: that at least one of her ancestors was an Elf.
But she doesn't know if it was a parent (1 generation ago), a
grandparent (2 generations ago), or someone from even more
generations ago. Help her out!

Being part Elf works the way you probably expect. People who are
Elves, Humans and part-Elves are all created in the same way: two
parents get together and have a baby. If one parent is A/B Elf, and the
other parent is C/D Elf, then their baby will be (A/B + C/D) / 2 Elf.
For example, if someone who is 0/1 Elf and someone who is 1/2 Elf
have a baby, that baby will be 1/4 Elf.

Vida is certain about one thing: 40 generations ago, she had 240 different
ancestors, and each one of them was 1/1 Elf or 0/1 Elf.

Vida says she's P/Q Elf. Tell her what is the minimum number of
generations ago that there could have been a 1/1 Elf in her family. If it is
not possible for her to be P/Q Elf, tell her that she must be wrong!

Input

The first line of the input gives the number of test cases, T. T lines
follow. Each contains a fraction of the form P/Q, where P and Q are
integers.

Output

For each test case, output one line containing "Case #x: y", where x is
the test case number (starting from 1) and y is the minimum number of
generations ago a 1/1 Elf in her family could have been if she is P/Q Elf.
If it's impossible that Vida could be P/Q Elf, y should be the string
"impossible" (without the quotes).

Limits

1 ≤ T ≤ 100.

Small dataset

1 ≤ P < Q ≤ 1000.
P and Q have no common factors. That means P/Q is a fraction
in lowest terms.
Large dataset

1 ≤ P < Q ≤ 1012.
P and Q may have common factors. P/Q is not guaranteed to be a
fraction in lowest terms.

Sample

Input Output

5 Case #1: 1
1/2 Case #2: 1
3/4 Case #3: 2
1/4 Case #4: impossible
2/23 Case #5: 8
123/31488

Note that the fifth sample case does not meet the limits for the Small
input. Even if you don't solve it correctly, you might still have solved the
Small input correctly.

Explanation of sample cases

In the first sample case, Vida could have a 1/1 Elf parent and a 0/1 Elf
parent. That means she could have had a 1/1 Elf one generation ago, so
the answer is 1.

In the second sample case, Vida could have had a 1/1 Elf parent and
a 1/2 Elf parent. That means she could have had a 1/1 Elf one
generation ago, so the answer is 1.

In the third sample case, Vida could have had a 0/1 Elf parent and
a 1/2 Elf parent. The 1/2 Elf parent could have had a 1/1 Elf parent
and a 0/1 Elf parent. That means she could have had a 1/1 Elf two
generations ago, so the answer is 2.

In the fourth sample case, it's impossible to be exactly 2/23 Elf if your
ancestors 40 generations ago were all 0/1 Elf or 1/1 Elf.

Note
Yes, Vida has a lot of ancestors. If that is the part of the problem that
seems the most unrealistic to you, please re-read the part about Elves.

26:Problem
Fegla and Omar like to play games every day. But now they are bored of
all games, and they would like to play a new game. So they decided to
invent their own game called "The Repeater".

They invented a 2 player game. Fegla writes down N strings. Omar's


task is to make all the strings identical, if possible, using the minimum
number of actions (possibly 0 actions) of the following two types:

• Select any character in any of the strings and repeat it (add


another instance of this character exactly after it). For example, in
a single move Omar can change "abc" to "abbc" (by repeating the
character 'b').
• Select any two adjacent and identical characters in any of the
strings, and delete one of them. For example, in a single move
Omar can change "abbc" to "abc" (delete one of the 'b' characters),
but can't convert it to "bbc".

The 2 actions are independent; it's not necessary that an action of the
first type should be followed by an action of the second type (or vice
versa).

Help Omar to win this game by writing a program to find if it is possible


to make the given strings identical, and to find the minimum number of
moves if it is possible.

Input

The first line of the input gives the number of test cases, T. T test cases
follow. Each test case starts with a line containing an integer N which is
the number of strings. Followed by N lines, each line contains a non-
empty string (each string will consist of lower case English characters
only, from 'a' to 'z').

Output

For each test case, output one line containing "Case #x: y", where x is
the test case number (starting from 1) and y is the minimum number of
moves to make the strings identical. If there is no possible way to make
all strings identical, print "Fegla Won" (quotes for clarity).

Limits

1 ≤ T ≤ 100.
1 ≤ length of each string ≤ 100.

Small dataset

N = 2.

Large dataset

2 ≤ N ≤ 100.

Sample

Input Output

5 Case #1: 1
2 Case #2: Fegla Won
mmaw Case #3: 4
maw Case #4: 0
2 Case #5: 3
gcj
cj
3
aaabbb
ab
aabb
2
abc
abc
3
aabc
abbc
abcc

27:Problem
Shota the farmer has a problem. He has just moved into his newly built
farmhouse, but it turns out that the outlets haven't been configured
correctly for all of his devices. Being a modern farmer, Shota owns a
large number of smartphones and laptops, and even owns a tablet for
his favorite cow Wagyu to use. In total, he owns N different devices.

As these devices have different specifications and are made by a variety


of companies, they each require a different electric flow to charge.
Similarly, each outlet in the house outputs a specific electric flow. An
electric flow can be represented by a string of 0s and 1s of length L.

Shota would like to be able to charge all N of his devices at the same
time. Coincidentally, there are exactly N outlets in his new house. In
order to configure the electric flow from the outlets, there is a master
control panel with L switches. The ith switch flips the ith bit of the electric
flow from each outlet in the house. For example, if the electric flow from
the outlets is:

Outlet 0: 10

Outlet 1: 01

Outlet 2: 11

Then flipping the second switch will reconfigure the electric flow to:

Outlet 0: 11

Outlet 1: 00

Outlet 2: 10

If Shota has a smartphone that needs flow "11" to charge, a tablet that
needs flow "10" to charge, and a laptop that needs flow "00" to charge,
then flipping the second switch will make him very happy!

Misaki has been hired by Shota to help him solve this problem. She has
measured the electric flows from the outlets in the house, and noticed
that they are all different. Decide if it is possible for Shota to charge all of
his devices at the same time, and if it is possible, figure out the minimum
number of switches that needs to be flipped, because the switches are
big and heavy and Misaki doesn't want to flip more than what she needs
to.

Input

The first line of the input gives the number of test cases, T. T test cases
follow. Each test case consists of three lines. The first line contains two
space-separated integers N and L. The second line contains N space-
separated strings of length L, representing the initial electric flow from
the outlets. The third line also contains N space-separated strings of
length L, representing the electric flow required by Shota's devices.

Output

For each test case, output one line containing "Case #x: y", where x is
the case number (starting from 1) and y is the minimum number of
switches to be flipped in order for Shota to charge all his devices. If it is
impossible, y should be the string "NOT POSSIBLE" (without the
quotes). Please note that our judge is not case-sensitive, but it is strict in
other ways: so although "not possible" will be judged correct, any
misspelling will be judged wrong. We suggest copying/pasting the string
NOT POSSIBLE into your code.

Limits

1 ≤ T ≤ 100.
No two outlets will be producing the same electric flow, initially.
No two devices will require the same electric flow.

Small dataset

1 ≤ N ≤ 10.
2 ≤ L ≤ 10.

Large dataset

1 ≤ N ≤ 150.
10 ≤ L ≤ 40.

Sample
Input Output

3 Case #1: 1
3 2 Case #2: NOT POSSIBLE
01 11 10 Case #3: 0
11 00 10
2 3
101 111
010 001
2 2
01 10
10 01

Explanation

In the first example case, Misaki can flip the second switch once. The
electric flow from the outlets becomes:

Outlet 0: 00

Outlet 1: 10

Outlet 2: 11

Then Shota can use the outlet 0 to charge device 1, the outlet 1 to
charge device 2, outlet 2 to charge device 0. This is also a solution that
requires the minimum amount number of switches to be flipped.

28:Problem
Recently you went to a magic show. You were very impressed by one of
the tricks, so you decided to try to figure out the secret behind it!

The magician starts by arranging 16 cards in a square grid: 4 rows of


cards, with 4 cards in each row. Each card has a different number from 1
to 16 written on the side that is showing. Next, the magician asks a
volunteer to choose a card, and to tell him which row that card is in.
Finally, the magician arranges the 16 cards in a square grid again,
possibly in a different order. Once again, he asks the volunteer which
row her card is in. With only the answers to these two questions, the
magician then correctly determines which card the volunteer chose.
Amazing, right?

You decide to write a program to help you understand the magician's


technique. The program will be given the two arrangements of the cards,
and the volunteer's answers to the two questions: the row number of the
selected card in the first arrangement, and the row number of the
selected card in the second arrangement. The rows are numbered 1 to 4
from top to bottom.

Your program should determine which card the volunteer chose; or if


there is more than one card the volunteer might have chosen (the
magician did a bad job); or if there's no card consistent with the
volunteer's answers (the volunteer cheated).

Solving this problem

Usually, Google Code Jam problems have 1 Small input and 1 Large
input. This problem has only 1 Small input. Once you have solved the
Small input, you have finished solving this problem.

Input

The first line of the input gives the number of test cases, T. T test cases
follow. Each test case starts with a line containing an integer: the answer
to the first question. The next 4 lines represent the first arrangement of
the cards: each contains 4 integers, separated by a single space. The
next line contains the answer to the second question, and the following
four lines contain the second arrangement in the same format.

Output

For each test case, output one line containing "Case #x: y", where x is
the test case number (starting from 1).

If there is a single card the volunteer could have chosen, y should be the
number on the card. If there are multiple cards the volunteer could have
chosen, y should be "Bad magician!", without the quotes. If there are no
cards consistent with the volunteer's answers, y should be "Volunteer
cheated!", without the quotes. The text needs to be exactly right, so
consider copying/pasting it from here.
Limits

1 ≤ T ≤ 100.
1 ≤ both answers ≤ 4.
Each number from 1 to 16 will appear exactly once in each arrangement.

Sample

Input Output

3 Case #1: 7
2 Case #2: Bad magician!
1 2 3 4 Case #3: Volunteer cheated!
5 6 7 8
9 10 11 12
13 14 15 16
3
1 2 5 4
3 11 6 15
9 10 7 12
13 14 8 16
2
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
2
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
2
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
3
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
29:Problem

Before graduating from Awesome Programmer University, students


traditionally perform certain "graduation requirements". One of these is
driving around a traffic circle backwards. For most people, this is crazy
enough, but as an extra challenge, you want to see if you can go
backwards around the traffic circle multiple times without stopping.

The traffic circle consists of N intersections, spaced evenly around the


circle. A car would normally enter the traffic circle at one intersection,
and then every second, it will move to the next counter-clockwise
intersection, until eventually it reaches its destination and leaves.

You have been watching cars enter and leave the traffic circle
for X seconds. For each car, you record the time it enters the circle, as
well as the intersections it enters and leaves at. All cars are moving
counter-clockwise at the rate of 1 intersection per second. Each car you
watched exited the circle before coming back to the intersection it
entered at. There are multiple lanes on the traffic circle, so multiple cars
can occupy the same position at the same time.

If you had planned it just right, how long could you have driven clockwise
in the traffic circle during this time? You must enter the circle at some
integer time >= 0, leave at time <= X, and once you leave, you are not
allowed to come back. When in the traffic circle, you must travel
clockwise at the rate of 1 intersection per second. You want to play it
safe (well, as safe as driving backwards on a traffic circle can be), so
you must never touch or pass by another car. In particular, you cannot
leave the circle at an intersection at which another car is entering at the
same moment, and you cannot enter the circle at an intersection at
which another car is leaving at the same moment. You can choose when
and where to enter and leave the circle.

Input

The first line of the input gives the number of test cases, T. T test cases
follow. The first line of any test case describes the number C of cars you
observed. The second line contains two integers, X and N — the time (in
seconds) for which you observed the circle, and the number of
intersections on the circle. Next C lines describe the cars you have seen.
Each of those lines contains three integers si, ei and ti — the intersection
at which the car entered the circle, the intersection on which it left and
the time at which it entered. The intersections are numbered from 1 to N,
counterclockwise (that is, the intersection number 2 is the next
intersection counterclockwise from number 1).

Output

For each test case, output one line containing "Case #x: y", where x is
the case number (starting from 1) and y is the maximum number of
seconds you can travel on the circle. Note that y can be zero both in the
case where you cannot enter the circle at all and in the case when you
can enter it, but can't travel even one intersection.

Remember that you are required to enter the circle at a time expressed
as an integer number of seconds — you must enter at an integer time,
and thus arrive at each intersection at an integer time.

Limits

1 ≤ T ≤ 100
1 ≤ si, ei ≤ N
si ≠ ei
0 ≤ ti
Each observed car leaves the circle at time X or earlier.

Small dataset
3 ≤ N ≤ 10
1 ≤ X ≤ 10
0 ≤ C ≤ 10

Large dataset

3 ≤ N ≤ 1010
1 ≤ X ≤ 1010
0 ≤ C ≤ 1000

Sample

Input Output

5 Case #1: 1
1 Case #2: 2
3 4 Case #3: 0
1 4 0 Case #4: 6
6 Case #5: 0
3 5
5 2 0
5 1 2
1 3 0
1 2 2
2 3 0
3 4 0
3
2 3
1 3 0
2 1 0
3 2 0
0
6 4
1
2 3
1 3 0

In the first sample case, we have one car, going as in the picture in the
statement. There are a number of ways allowing us to travel backwards
for one second — for instance, we can enter at intersection 1 at time 1
(we can't enter at time zero, because the other car is there), and travel to
intersection 4 (we can't go on to intersection 3, as we would pass the
other car which will be going from 3 to 4). Another option is to enter at
intersection 4 at time 0, and travel to intersection 3 (and then exit).

In the second sample case, we can travel for two seconds by entering at
intersection 5 at time 1, and traveling backwards to intersection 3. In the
third sample case, we can't even enter the circle - there are cars at all

intersections at every full second. In the fourth case there are no cars,
so we can just enter the circle at any point at time 0 and travel round and
round till time 6. In the fifth case we can enter the circle, but since there
are only three intersections, we will always collide with the other car if
we try to move to the next one.

Note: Driving against the direction of the traffic on a traffic circle is


typically not a wise thing to do and may cause harm to you or other
people. Google (and Google Code Jam in particular) encourages you
not to try this.

30:Problem
You've been playing roulette for a while in a local casino. Roulette is a
simple casino game in which multiple players place bets on one or more
numbers between 0 and 36 (inclusive). Next, a wheel is spun in one
direction with a ball spinning in the other direction. The roulette wheel
contains the same numbers 0 to 36. Some real roulette wheels also
have a space labeled 00, but ours does not. Eventually, the ball falls on
one of the numbers. If a player placed a bet on that particular number,
he receives 36 times his bet (so the profit of that bet is 35 times the bet).
All bets placed on other numbers lose.
Unfortunately, luck hasn't been on your side, and you have been losing
all night long. At one point, you started to wonder whether the roulette
game was fair or not, and after observing the game some more, you
noticed a pattern that must be profitable for the casino: the ball always
lands on one of the numbers that has the least total money bet on it! If
multiple numbers tie for the least total money bet, the ball lands on one
of those uniformly at random.

Of course, you'll be notifying the authorities about this foul play, but first
you want to win your money back by exploiting your new-found
knowledge. To do so, you wait until all other players have placed their
bets and then place bets of your own. Unfortunately, you have a limited
budget left, so you cannot bet more than that. You are allowed to bet on
zero or more different numbers, and each of those bets can be any
positive integer amount (perhaps with different amounts for different
numbers), so as long as the sum of your bets does not exceed your
budget. What is the maximum expected profit you can make?

Input

The first line of input gives the number of cases, T. T test cases follow.
Each test case consists of two lines. The first line contains two integers:
the budget you still have, B, and the number of numbers other players
have placed bets on, N. The second line contains N integers Xi, the total
amounts of money bet by other players on each of those different
numbers.

Output

For each test case, output one line containing "Case #x: " followed by
the maximum expected profit that you make if you place your bets
optimally. A profit will be considered correct if it is within an absolute or
relative error of 10-6 of the correct answer. See the FAQ for an
explanation of what that means, and what formats of floating-point
numbers we accept.

Limits

1 <= T <= 100.


1 <= N <= 37.

Small dataset

1 <= B, Xi <= 1,000.


Large dataset

1 <= B, Xi <= 1012.

Sample

Input Output

3 Case #1: 0
100 1 Case #2: 2
10 Case #3: 0.9428571429
34 3
5 6 7
34 4
1 1 10 10

In example 2, bet 1 on each of the 34 empty numbers for a guaranteed


payback of 36, and a profit of 36 - 34 = 2. In example 3, bet 1 on each of
the 33 empty numbers, so that you win 36 with probability 33/35. The
gives an expected profit of 33/35 * 36 - 33.

31:Problem
The city has built its first subway line, with a grand total of N stations,
and introduced a new way of paying for travel. Instead of just paying for
one ticket and making an arbitrary journey, the price you pay is now
based on entry cards.

When entering the subway, each passenger collects an entry card,


which specifies the station the passenger entered at. When leaving the
subway, the passenger has to give up the entry card, and is charged
depending on the distance (in stations traveled) between the entry
station specified on the entry card, and the exit station on which the
entry card is surrendered. The payment depends on the distance
between these stations as follows:

• if they are the same station, you don't pay;


• if they are adjacent stations, you pay N pounds;
• if the distance is two stations, you pay 2N - 1: a charge N for the
first stop and N - 1 for the second;
• the third station costs N-2 (so you pay 3N - 3 for a three-station-
long trip), the fourth stop N-3, and the ith stop N + 1-i;
• thus, if you travel from one end of the subway to the other (a
distance of N-1 stations), you pay 2 pounds for the last station
traveled, and a grand total of (N2 + N - 2) / 2 in total.

After introducing this system the city noticed their gains are not as big as
they expected. They figured out this might be due to people swapping
their entry cards — so, for instance, if one person enters at station A,
travels two stations to B and exits, while another person enters at B,
travels three stations to C and exits, they would normally pay (in
total) 2N - 1 + 3N - 3 = 5N - 4. But if the two people swapped their entry
cards at station B, then the first one would travel for free (as he would
surrender an entry card specifying the station B while exiting a station B,
and so register a distance of zero); while the second person will exit at
station C and surrender an entry card specifying station A, which is 5
stations away, and pays 5N - 10, at a net loss of six pounds to the city!

The city now wants to learn how much they can possibly lose if this
practice becomes widespread. We will consider only one direction (from
station 1 to station N, passing through all the stations in order) of the
subway, and only one train on this line. We assume a passenger
travelling from o to e obtains an entry card at o, can swap her entry card
any number of times with any other passengers anywhere
between o and e, including swapping with people who leave at o or
those who enter at e, and then exit the train at e with some entry card (it
is necessary to surrender some entry card to exit the subway). We also
assume the passenger will not exit the train in the meantime (that is, will
not surrender the currently held card and obtain a new one).

You are given a map of traffic (specifying how many passengers travel
this train from which station to which), and you should calculate the city's
financial loss, assuming passengers swap their cards to maximize this
loss.

Input

The first line of the input gives the number of test cases, T. T test cases
follow. Each test case contains the number N of stops (the stops are
numbered 1 to N), and the number M of origin-endpoint pairs given. The
next M lines contain three numbers each: the origin stop oi, the end
stop ei and pi: the number of passengers that make this journey.

Output

For each test case, output one line containing "Case #x: y", where x is
the case number (starting from 1) and y is the total loss the city can
observe due to ticket swapping, modulo 1000002013.

Limits

1 ≤ T ≤ 20.
1 ≤ oi < ei ≤ N

Small dataset

2 ≤ N ≤ 100.
1 ≤ M ≤ 100.
1 ≤ pi ≤ 100.

Large dataset

2 ≤ N ≤ 109.
1 ≤ M ≤ 1000.
1 ≤ pi ≤ 109.

Sample

Input Output

3 Case #1: 6
6 2 Case #2: 0
1 3 1 Case #3: 10
3 6 1
6 2
1 3 2
4 6 1
10 2
1 7 2
6 9 1

The first test case is the case described in the problem statement - two
passengers meet at station 3 and swap tickets. In the second test case
the two passengers don't meet at all, so they can't swap tickets (and so
the city incurs no loss). In the third case, only one of the early
passengers can swap tickets with the later passenger.

32:Problem
In English, there are 26 letters that are either vowels or consonants. In
this problem, we consider a, e, i, o, and u to be vowels, and the other 21
letters to be consonants.

A tribe living in the Greatest Colorful Jungle has a tradition of naming


their members using English letters. But it is not easy to come up with a
good name for a new member because it reflects the member's social
status within the tribe. It is believed that the less common the name he
or she is given, the more socially privileged he or she is.

The leader of the tribe is a professional linguist. He notices that hard-to-


pronounce names are uncommon, and the reason is that they have too
many consecutive consonants. Therefore, he announces that the
social status of a member in the tribe is determined by its n-value, which
is the number of substrings with at least n consecutive consonants in the
name. For example, when n = 3, the name "quartz" has the n-value of 4
because the substrings quartz, uartz, artz, and rtz have at least 3
consecutive consonants each. A greater n-value means a greater social
status in the tribe. Two substrings are considered different if they begin
or end at a different point (even if they consist of the same letters), for
instance "tsetse" contains 11 substrings with two consecutive
consonants, even though some of them (like "tsetse" and "tsetse")
contain the same letters.

All members in the tribe must have their names and n given by the
leader. Although the leader is a linguist and able to ensure that the given
names are meaningful, he is not good at calculating the n-values.
Please help the leader determine the n-value of each name. Note that
different names may have different values of n associated with them.

Input

The first line of the input gives the number of test cases, T. T test cases
follow. The first line of each test case gives the name of a member as a
string of length L, and an integer n. Each name consists of one or more
lower-case English letters.

Output

For each test case, output one line containing "Case #x: y", where x is
the case number (starting from 1) and y is the n-value of the member's
name.

Limits

1 ≤ T ≤ 100.
0 < n ≤ L.

Small dataset

1 ≤ L ≤ 100.

Large dataset

1 ≤ L ≤ 106.
The input file will be no larger than 6MB.

Sample

Input Output

4 Case #1: 4
quartz 3 Case #2: 11
straight 3 Case #3: 3
gcj 2 Case #4: 11
tsetse 2

33:Problem
Armin is playing Osmos, a physics-based puzzle game developed by
Hemisphere Games. In this game, he plays a "mote", moving around
and absorbing smaller motes.
A "mote" in English is a small particle. In this game, it's a thing that
absorbs (or is absorbed by) other things! The game in this problem has
a similar idea to Osmos, but does not assume you have played the
game.

When Armin's mote absorbs a smaller mote, his mote becomes bigger
by the smaller mote's size. Now that it's bigger, it might be able to
absorb even more motes. For example: suppose Armin's mote has size
10, and there are other motes of sizes 9, 13 and 19. At the start, Armin's
mote can only absorb the mote of size 9. When it absorbs that, it will
have size 19. Then it can only absorb the mote of size 13. When it
absorbs that, it'll have size 32. Now Armin's mote can absorb the last
mote.

Note that Armin's mote can absorb another mote if and only if the other
mote is smaller. If the other mote is the same size as his, his mote can't
absorb it.

You are responsible for the program that creates motes for Armin to
absorb. The program has already created some motes, of various sizes,
and has created Armin's mote. Unfortunately, given his mote's size and
the list of other motes, it's possible that there's no way for Armin's mote
to absorb them all.

You want to fix that. There are two kinds of operations you can perform,
in any order, any number of times: you can add a mote of any positive
integer size to the game, or you can remove any one of the existing
motes. What is the minimum number of times you can perform those
operations in order to make it possible for Armin's mote to absorb every
other mote?

For example, suppose Armin's mote is of size 10 and the other motes
are of sizes [9, 20, 25, 100]. This game isn't currently solvable, but by
adding a mote of size 3 and removing the mote of size 100, you can
make it solvable in only 2 operations. The answer here is 2.

Input

The first line of the input gives the number of test cases, T. T test cases
follow. The first line of each test case gives the size of Armin's mote, A,
and the number of other motes, N. The second line contains the N sizes
of the other motes. All the mote sizes given will be integers.

Output
For each test case, output one line containing "Case #x: y", where x is
the case number (starting from 1) and y is the minimum number of
operations needed to make the game solvable.

Limits

1 ≤ T ≤ 100.

Small dataset

1 ≤ A ≤ 100.
1 ≤ all mote sizes ≤ 100.
1 ≤ N ≤ 10.

Large dataset

1 ≤ A ≤ 106.
1 ≤ all mote sizes ≤ 106.
1 ≤ N ≤ 100.

Sample

Input Output

4 Case #1: 0
2 2 Case #2: 1
2 1 Case #3: 2
2 4 Case #4: 4
2 1 1 6
10 4
25 20 9 100
1 4
1 1 1 1

Notes

Although the size of motes is limited in the input files, Armin's mote may
grow larger than the provided limits by absorbing other motes.

Osmos was created by Hemisphere Games. Hemisphere Games does


not endorse and has no involvement with Google Code Jam.
34:Problem
Maria has been hired by the Ghastly Chemicals Junkies (GCJ) company
to help them manufacture bullseyes. A bullseye consists of a number
of concentric rings (rings that are centered at the same point), and it
usually represents an archery target. GCJ is interested in manufacturing
black-and-white bullseyes.

Maria starts with t millilitres of black paint, which she will use to draw
rings of thickness 1cm (one centimetre). A ring of thickness 1cm is the
space between two concentric circles whose radii differ by 1cm.

Maria draws the first black ring around a white circle of radius r cm. Then
she repeats the following process for as long as she has enough paint to
do so:

1. Maria imagines a white ring of thickness 1cm around the last black
ring.
2. Then she draws a new black ring of thickness 1cm around that
white ring.
Note that each "white ring" is simply the space between two black rings.

The area of a disk with radius 1cm is π cm2. One millilitre of paint is
required to cover area π cm2. What is the maximum number of black
rings that Maria can draw? Please note that:

• Maria only draws complete rings. If the remaining paint is not


enough to draw a complete black ring, she stops painting
immediately.
• There will always be enough paint to draw at least one black ring.
Input

The first line of the input gives the number of test cases, T. T test cases
follow. Each test case consists of a line containing two space separated
integers: r and t.

Output

For each test case, output one line containing "Case #x: y", where x is
the case number (starting from 1) and y is the maximum number of
black rings that Maria can draw.

Limits

Small dataset

1 ≤ T ≤ 1000.
1 ≤ r, t ≤ 1000.

Large dataset

1 ≤ T ≤ 6000.
1 ≤ r ≤ 1018.
1 ≤ t ≤ 2 × 1018.

Sample

Input

5
1 9
1 10
3 40
1 1000000000000000000
10000000000000000 1000000000000000000

Output

Case #1: 1
Case #2: 2
Case #3: 3
Case #4: 707106780
Case #5: 49

You might also like