This was our second Code Jam to I/O for Women. This year over 200 contestants attempted at least one problem. Every problem was solved by at least 10 contestants, and five contestants achieved perfect scores.
This year's problems skewed a bit mathematical; even the hardest problem, Googlander, which was superficially a graph problem, had an underlying formulaic solution. To earn one of the 100 coveted tickets to I/O, it turned out to be necessary (but not sufficient) to solve any two inputs, most often I/O Error and the small of Dreary Design.
We hope you had fun with this problem set. We look forward to seeing some of you at I/O and all of you back next year!
Cast
Problem A. I/O Error written and prepared by Ian Tullis.
Problem B. Dreary Design written by Ian Tullis and Ahmed Aly. Prepared by Ahmed Aly.
Problem C. Power Levels written and prepared by Ian Tullis.
Problem D. Googlander written and prepared by Ian Tullis.
Solutions and other problem preparations and reviews by Ahmed Aly, Marni Merksamer Levasseur, Lauren Mancino, Igor Naverniouk, Chieu Nguyen, Janette Siu, and Ian Tullis.
Contest analysis written by Ian Tullis.
Our computers are so excited about the upcoming Google I/O that they've started storing their ones as capital letter Is and their zeroes as capital letter Os! For example, the character A
, which is 65 in ASCII, would normally be stored as the byte 01000001
, but our computers are storing it as OIOOOOOI
.
Given a string of 8-character "bytes" consisting of I
s and O
s, can you translate it using ASCII? Every "byte" is guaranteed to translate to a printable character (a decimal value between 32 and 126, inclusive). Note that one of these characters (the one with decimal value 32) is a space. No translated message will begin or end with a space, but there may be internal space characters.
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 of each test case contains an integer representing the number B of "bytes" in the string to be translated. The second line of each test case contains 8 * B characters, all of which are either I
or O
.
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 translated message.
1 ≤ T ≤ 100.
Time limit: 20 seconds.
Memory limit: 1 GB.
1 ≤ B ≤ 1000.
Input |
|
2 2 OIOOIIIIOIOOIOII 21 OIOOIOOIOOIOOOOOOOIOOIIIOOIIIIOOOOIIOOIIOOIOOIIIOOIOOOOOOOIOOOIOOIOOOOIIOOIIOOOOOIIOOIOOOOIIOOIIOOIOOOOOOIOOIOIOOOIIOIOOOIIOIIOIOOIOOOIOOOIOOOOIOOIOOOOOOOIIIOIOOOIOIOOI |
|
|
|
Output
|
|
Case #1: OK Case #2: I '<3' "C0d3 J4m"! :) |
A multifactorial -- that is, a number N followed by some nonzero number E of exclamation points -- is defined as the product of all positive numbers (N - K*E) for which K is a nonnegative integer. Normal factorials meet the definition of multifactorials, for example:
5! = 5 * (5-1) * (5-2) * (5-3) * (5-4) = 120
Here are the other multifactorials of 5:
5!! equals 5 * (5-2) * (5-4) = 15
5!!! equals 5 * (5-3) = 10
5!!!! equals 5 * (5-4) = 5
5 followed by five or more !s = 5
5 with no exclamation points is not considered a multifactorial.
The villainous Anima and her underling Minera love three things: multifactorials, yelling "IT'S OVER 9000" followed by some number of exclamation points, and going around the universe looking for people to fight.
When Anima and Minera encounter a potential opponent, Anima asks Minera to use her scanner device to determine the opponent's power level, which is always a positive integer that does not begin with any leading zeroes. Today, the display on Minera's scanner is blurry, and she can only tell Anima the number of digits D in the opponent's power level, and not what any of those digits are.
Anima wants to yell IT'S OVER followed by a multifactorial of 9000 to accurately describe the opponent's power level. She will never yell something that might not be true, and she will never use more exclamation points than she needs to. For example, if D = 31682, Anima can't yell IT'S OVER 9000!, because she knows that 9000! also has 31682 digits and the opponent's power level might be a 31682-digit number that is less than or equal to 9000!. However, since 9000!! has fewer than 31682 digits, she can be confident that the opponent's power level is greater than 9000!!, so IT'S OVER 9000!! is guaranteed to be true. Although the opponent's power level is also definitely greater than 9000!!!, 9000!!!!, and so on, she won't use more exclamation points than she needs to. So, in this example, she will yell IT'S OVER 9000!!
If there is no multifactorial of 9000 that is definitely less than the opponent's power level, Anima will remain dramatically silent. We represent dramatic silence as an ellipsis: ...
What should Anima say?
The first line of the input gives the number of test cases, T. T lines follow. Each contains a positive integer D, the number of digits in an opponent's power level.
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 ...
if Anima must remain silent, or IT'S OVER 9000
followed by some positive number of exclamation points. Make sure your output exactly matches these specifications. You may wish to copy our apostrophe character into your code to avoid possible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!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Case #3: IT'S OVER 9000!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Case #4: IT'S OVER 9000!! Case #5: IT'S OVER 9000! |
Eric Googlander is a fashion model who performs by walking around on a stage made of squares that form a grid with R rows and C columns. He begins at the leftmost bottom square, facing toward the top edge of the stage, and he will perform by making a series of moves. Googlander knows only the following two moves:
1. Take one step forward in the direction he is currently facing
2. Make a single 90 degree turn to the right, then take one step forward in the new direction he is facing following the turn
(Note that Googlander does not know how to make a 90 degree left turn.)
If a move would take Googlander off of the stage or onto a square he has already visited, that move is unfashionable. Whenever Googlander is in a position for which neither of the two possible moves is unfashionable, he is free to choose either move (independently of any other choices he has made in the past), but he must choose one of them. Whenever one of the possible moves he can make is unfashionable, he must make the other move. If at any point both of the possible moves are unfashionable, the show immediately ends without Googlander making another move. Note that Googlander cannot stop the show early -- he must keep moving until both possible moves become unfashionable.
How many different paths is it possible for Googlander to walk? (Two paths are the same if and only if they visit the same squares in the same order.)
The first line of the input gives the number of test cases, T. T lines follow; each consists of two space-separated integers R and C.
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 paths that Googlander can walk.
1 ≤ T ≤ 100.
Time limit: 20 seconds per test set.
Memory limit: 1 GB.
1 ≤ R, C ≤ 10.
The limits ensure that the answer will always fit in a 32-bit signed integer.
1 ≤ R, C ≤ 25.
The limits ensure that the answer will always fit in a 64-bit signed integer.
Input |
Output |
3 1 1 1 3 3 3 |
Case #1: 1 Case #2: 1 Case #3: 6 |
One way to represent a color is as a triple of component values (each of which can range from 0 to K, inclusive) representing levels of red, green, and blue. For example, in the color system where K = 3, (0, 2, 3) and (0, 3, 2) would be two of the possible distinct colors.
We will consider a color to be bland if and only if all pairs of its component values differ by no more than V. For example, in a system with K = 2 and V = 1, the color (2, 1, 1) is bland, because its red and green components differ by 1, its red and blue components differ by 1, and its green and blue components differ by 0, and none of these differences exceeds 1. But (2, 1, 0) is not bland, because the red and blue components differ by more than 1.
Mr. Turner loves to create gloomy landscape images and wants to design a color system in which there are many bland colors available. Given values for K and V, can you tell him how many distinct bland colors are there?
The first line of the input gives the number of test cases, T. T lines follow. Each contains two space-separated integers K and V.
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 distinct bland colors.
1 ≤ T ≤ 100.
Time limit: 20 seconds per test set.
Memory limit: 1 GB.
V ≤ K.
0 ≤ K ≤ 255.
0 ≤ V ≤ 100.
All answers are guaranteed to fit in a 32-bit signed integer.
0 ≤ K ≤ 2,555.
0 ≤ V ≤ 555.
All answers are guaranteed to fit in a 32-bit signed integer.
0 ≤ K ≤ 2,000,000,000.
0 ≤ V ≤ 1,000.
All answers are guaranteed to fit in a 64-bit signed integer.
Input |
Output |
4 1 1 1 0 255 0 0 0 |
Case #1: 8 Case #2: 2 Case #3: 256 Case #4: 1 |
This was intended as a warm-up implementation problem to enable first-time Code Jammers to become familiar with the contest’s format and environment. There are three steps to the solution:
1. Convert “bytes” like OIOIOOOI to binary values like 01010001.
2. Convert those binary values to decimal values.
3. Convert those decimal values to characters.
Nearly all programming languages have a built-in solution for Step 3 (for example, chr() in Python.) Steps 1 and 2 required some custom code. Here’s a sample Python solution:
def chrfromio(b): s = [(1 if c == 'I' else 0) for c in b] v = 0 exp = 0 for i in range(len(s)-1, -1, -1): v += (2**exp) * s[i] exp += 1 return chr(v) def fromio(inp): ans = '' for i in range(len(inp)/8): b = inp[8*i:8*(i+1)] ans += chrfromio(b) return ans t = int(raw_input().strip()) for case in range(1, t+1): d = int(raw_input().strip()) p = raw_input().strip() print "Case #" + str(case) + ": " + fromio(p)
Some of the possible characters in this problem (single quote, double quote, backslash) were potentially troublesome because of their special interpretations, but there was no need to handle them differently since they were being produced and not interpreted.
Incidentally, the translated output for the Small input was mostly from Chapter 35 of Pride and Prejudice, one of the author’s favorite books.
This problem was a somewhat contrived mathematical interpretation of a popular Internet meme. We know that it was a little awkward to have outputs with hundreds of exclamation points per line, but we couldn't resist. (The definition of multifactorials in the problem is real, but multifactorials don’t come up too often in practice!)
Like I/O Error, this is mostly an implementation problem. There are only 9000 different multifactorials of 9000. You can calculate them all, find how many digits are in each one, and choose the one with the largest number of digits that is still smaller than the number of digits in the opponent’s power level. But for the Large data set, most of those multifactorials are far too large to fit in the standard data types found in languages like C++ and Java. There were several workarounds for this:
1. Use a language extension like Java’s BigInteger that handles large numbers.
2. Use a language like Python that naturally handles arbitrarily large integers.
3. Take advantage of a useful programming contest trick: to multiply large numbers without overflowing the result, you can instead add the logarithms of those numbers and then raise the logarithm base to that power. In our case, since we only want the number of digits, you can use log base 10 and then take the ceiling of the sum. This method brings about the usual danger of small errors introduced by floating-point addition, but no multifactorial of 9000 is close enough to a power of 10 for this to pose a problem.
Here’s a sample Python solution that demonstrates the log addition method.
import math def multifactorial9000(num_exc): ans = math.log10(9000) curr = 9000 curr -= num_exc while curr > 0: ans += math.log10(curr) curr -= num_exc return math.ceil(ans) # from number of exclamation points to number of digits digits = [-1]*9001 for num_exc in range(1, 9001): digits[num_exc] = multifactorial9000(num_exc) def solve(numdigits): i = 1 while i <= 9000 and numdigits <= digits[i]: i += 1 if i > 9000: return "..." return "IT'S OVER 9000" + "!"*i t = int(raw_input().strip()) for case in range(1, t+1): d = int(raw_input().strip()) print "Case #" + str(case) + ": " + str(solve(d))
By the way, the title is a subtle pun: the problem revolves around numbers of digits, which are closely related to powers of 10.
Like Power Levels, this problem was inspired by pop culture: the movie Zoolander and its main character, who is unable to turn left. As he puts it: “I’m not an ambi-turner.”
One way to approach the problem is to construct all possible paths using, for example, a breadth-first search. Since the answer for a 10x10 grid is only 48620, this works fine for the Small data set, but it’s too slow for the Large. Googlander can walk 32247603683100 paths in a 25 x 25 grid, and that’s just too many paths to keep track of! So we need a better way.
An important observation is that once Googlander leaves the line of cells he’s currently walking along (without loss of generality, we’ll say it’s a column) by turning right, it’s impossible for him to return to it (or to any cell in the grid beyond the row into which he turns). This means that once he has taken his first step out of the column, he is in a new instance of the same problem, but smaller and rotated 90 degrees to the right. This suggests a recursive solution that accounts for the fact that he can make his turn anywhere in the column:
answer(R, C) = sum from i = 1 to R of answer(C-1, i)
answer(1, anything) = answer(anything, 1) = 1
If you use memoization to avoid solving the same subproblem more than once, this recursive strategy is fast enough for the Large data set. Here’s a sample solution:
memo = [[-1 for i in range(26)] for j in range(26)] def solve(r, c): if r == 1 or c == 1: return 1 if memo[r][c] != -1: return memo[r][c] ans = 0 for i in range(1, r+1): ans += solve(c-1, i) memo[r][c] = ans return ans t = int(raw_input().strip()) for case in range(1, t+1): r, c = [int(a) for a in raw_input().strip().split()] print "Case #" + str(case) + ": " + str(solve(r, c))
Some of you may have noticed that the solutions to this problem are entries in Pascal’s triangle (which are also the binomial coefficients). We leave it as an exercise to the reader to dig into that relationship.
The small input is small enough to be solved by checking every possible triple of colors to see whether it meets the definition:
def solve1(k, v): total = 0 for r in range(0, k+1): for g in range(0, k+1): for b in range(0, k+1): if abs(b-r) <= v and abs(b-g) <= v and abs(g-r) <= v: total += 1 return total
The first large input is too big for this, but can still be solved using nested for loops if you incorporate some observations:
1. The tolerance values can be used to bound the range of the for statements.
2. In that case, once R and G are fixed, there’s no need to look at every possible value of B; you can instead figure out the number of valid values directly.
def solve2(maxvalue, tolerance): total = 0 for r in range(0, maxvalue+1): for g in range(max(r-tolerance, 0), min(r+tolerance, maxvalue)+1): minb = max(r-tolerance, g-tolerance, 0) maxb = min(r+tolerance, g+tolerance, maxvalue) total += (maxb - minb + 1) return total
But this is far too slow for the second Large input, in which K can range as high as 2 billion! How can we proceed?
As is often the case in programming contests, the specified limits provide a clue. There’s no way to iterate over 2 billion values in time, so there must be a formulaic element to the answer. Indeed, if you look at the numbers of valid values beginning with each possible value for R, a pattern emerges. For example, consider this data for K = 10, V = 3:
R | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
Valid colors | 16 | 23 | 30 | 37 | 37 | 37 | 37 | 37 | 30 | 23 | 16 |
Once we’re at least V away from either end of the range of possible values for R, the number of valid colors is always the same! This makes sense because the number of valid colors is determined by an interaction between V and the boundaries of the range. And once we’re far enough away from the boundaries, that number depends only on V. Moreover, the problem is symmetric. The situation at the beginning of the range is exactly the same as at the end of the range.
These observations suggest a modification that will work for the second Large input:
def solve3(k, v): total = 0 for r in range(0, v+1): subtotal = 0 for g in range(max(r-v, 0), min(r+v, k)+1): minb = max(r-v, g-v, 0) maxb = min(r+v, g+v, k) subtotal += (maxb - minb + 1) if r == v: total *= 2 # take advantage of symmetry total += subtotal * (k - 2*v + 1) else: total += subtotal return total
One of our contestants went even farther than that! Please check out this writeup, which derives a simple, elegant formula for the answer.