# Facebook Hacker Cup 2012 Round 1

By: hxteam on Jan 28th, 2012  |  syntax: None  |  size: 7.43 KB  |  views: 805  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. ==========
2. Checkpoint
3. ==========
4.
5. You are racing on a 2D lattice grid starting from the origin (0,0) towards a goal (M,N) where M and N are positive integers such that 0 < M <= N. There is a checkpoint that's neither on the origin nor on the goal with coordinates (m,n) such that 0 <= m <= M and 0 <= n <= N. You must clear the checkpoint before you reach the goal. The shortest path takes T = M + N steps.
6.
7. At each point, you can move to the four immediate neighbors at a fixed speed, but since you don't want to lose the race, you are only going to take either a step to the right or to the top.
8.
9. Even though there are many ways to reach the goal while clearing the checkpoint, the race is completely pointless since it is relatively easy to figure out the shortest route. To make the race more interesting, we change the rules. Instead of racing to the same goal (M,N), the racers get to pick a goal (x,y) and place the checkpoint to their liking so that there are exactly S distinct shortest paths.
10. For example, given S = 4, consider the following two goal and checkpoint configurations
11. Placing the checkpoint at (1, 3) and the goal at (2,3). There are 4 ways to get from the origin to the checkpoint depending on when you move to the right. Once you are at the checkpoint, there is only one way to reach the goal with minimal number of steps. This gives a total of 4 distinct shortest paths, and takes T = 2 + 3 = 5 steps. However, you can do better.
12. Placing the checkpoint at (1, 1) and the goal at (2,2). There are two ways to get from the origin to the checkpoint depending on whether you move to the right first or later. Similarly, there are two ways to get to the goal, which gives a total of 4 distinct shortest paths. This time, you only need T = 2 + 2 = 4 steps.
13.
14. As a Hacker Cup racer, you want to figure out how to place the checkpoint and the goal so that you cannot possibly lose. Given S, find the smallest possible T, over all possible checkpoint and goal configurations, such that there are exactly S distinct shortest paths clearing the checkpoint.
15. Input
16. As input for the race you will receive a text file containing an integer R, the number of races you will participate in. This will be followed by R lines, each describing a race by a single number S. Lines are separated using Unix-style ("\n") line endings.
17.
18. Output
19. Your submission should contain the smallest possible length of the shortest path, T for each corresponding race, one per line and in order.
20.
21. Constraints
22. 5 <= R <= 20
23. 1 <= S <= 10000000
24.
25. Example input:
26.
27. 5
28. 4
29. 5
30. 12
31. 14
32. 1
33.
34. Example output:
35.
36. Case #1: 4
37. Case #2: 6
38. Case #3: 6
39. Case #4: 9
40. Case #5: 2
41.
42. ====================
43. Recover the Sequence
44. ====================
45.
46. Merge sort is one of the classic sorting algorithms. It divides the input array into two halves, recursively sorts each half, then merges the two sorted halves.
47.
48. In this problem merge sort is used to sort an array of integers in ascending order. The exact behavior is given by the following pseudo-code:
49.
50. function merge_sort(arr):
51.     n = arr.length()
52.     if n <= 1:
53.         return arr
54.
55.     // arr is indexed 0 through n-1, inclusive
56.     mid = floor(n/2)
57.
58.     first_half = merge_sort(arr[0..mid-1])
59.     second_half = merge_sort(arr[mid..n-1])
60.     return merge(first_half, second_half)
61.
62. function merge(arr1, arr2):
63.     result = []
64.     while arr1.length() > 0 and arr2.length() > 0:
65.         if arr1[0] < arr2[0]:
66.             print '1' // for debugging
67.             result.append(arr1[0])
68.             arr1.remove_first()
69.         else:
70.             print '2' // for debugging
71.             result.append(arr2[0])
72.             arr2.remove_first()
73.
74.     result.append(arr1)
75.     result.append(arr2)
76.     return result
77.
78. A very important permutation of the integers 1 through N was lost to a hard drive failure. Luckily, that sequence had been sorted by the above algorithm and the debug sequence of 1s and 2s was recorded on a different disk. You will be given the length N of the original sequence, and the debug sequence. Recover the original sequence of integers.
79. Input
80.
81. The first line of the input file contains an integer T. This is followed by T test cases, each of which has two lines. The first line of each test case contains the length of the original sequence, N. The second line contains a string of 1s and 2s, the debug sequence produced by merge sort while sorting the original sequence. Lines are separated using Unix-style ("\n") line endings.
82. Output
83.
84. To avoid having to upload the entire original sequence, output an integer checksum of the original sequence, calculated by the following algorithm:
85.
86. function checksum(arr):
87.     result = 1
88.     for i=0 to arr.length()-1:
89.         result = (31 * result + arr[i]) mod 1000003
90.     return result
91.
92. Constraints
93.
94. 5 ≤ T ≤ 20
95.
96. 2 ≤ N ≤ 10000
97. Examples
98.
99. In the first example, N is 2 and the debug sequence is 1. The original sequence was 1 2 or 2 1. The debug sequence tells us that the first number was smaller than the second so we know the sequence was 1 2. The checksum is 994.
100.
101. In the second example, N is 2 and the debug sequence is 2. This time the original sequence is 2 1.
102.
103. In the third example, N is 4 and the debug sequence is 12212. The original sequence is 2 4 3 1.
104.
105. Example input:
106.
107. 5
108. 2
109. 1
110. 2
111. 2
112. 4
113. 12212
114. 5
115. 1122211
116. 10
117. 121221212111122121212
118.
119. Example output:
120.
121. Case #1: 994
122. Case #2: 1024
123. Case #3: 987041
124. Case #4: 570316
125. Case #5: 940812
126.
127. ===============
128. Squished Status
129. ===============
130.
131. Some engineers got tired of dealing with all the different ways of encoding status messages, so they decided to invent their own. In their new scheme, an encoded status message consists of a sequence of integers representing the characters in the message, separated by spaces. Each integer is between 1 and M, inclusive. The integers do not have leading zeroes. Unfortunately they decided to compress the encoded status messages by removing all the spaces!
132.
133. Your task is to figure out how many different encoded status messages a given compressed status message could have originally been. Because this number can be very large, you should return the answer modulo 4207849484 (0xfaceb00c in hex).
134.
135. For example, if the compressed status message is "12" it might have originally been "1 2", or it might have originally been "12". The compressed status messages are between 1 and 1000 characters long, inclusive. Due to database corruption, a compressed status may contain sequences of digits that could not result from removing the spaces in an encoded status message.
136. Input
137.
138. The input begins with a single integer, N, the number of compressed status messages you must analyze. This will be followed by N compressed status messages, each consisting of an integer M, the highest character code for that database, then the compressed status message, which will be a string of digits each in the range '0' to '9', inclusive. All tokens in the input will be separated by some whitespace.
139. Output
140.
141. For each of the test cases numbered in order from 1 to N, output "Case #i: " followed by a single integer containing the number of different encoded status messages that could be represented by the corresponding compressed sequence modulo 4207849484. If none are possible, output a 0.
142. Constraints
143.
144. 5 <= N <= 25
145. 2 <= M <= 255
146. 1 <= length of encoded status <= 1000
147.
148. Example input:
149.
150. 5
151. 12
152. 12
153. 255
154. 219
155. 30
156. 1234321
157. 2
158. 101
159. 70 8675309
160.
161. Example output:
162.
163. Case #1: 2
164. Case #2: 4
165. Case #3: 6
166. Case #4: 0
167. Case #5: 2
clone this paste RAW Paste Data