Hong Kong Olympiad in Informatics 2014/15 Heat Event (Junior Group)
Official Solution
Statistics (N = 214)
Full mark = 45. Maximum = 39. Median = 14. Advance to Final = 16 marks or above.
Section A
Q A Explanation
1 F The advantage of SSD is that the random seek time is much shorter.
2 T We can perform binary search with any sorted array.
3 T double’s range is around ±10308 while the range of float is around ±1037 4 F break can be used to end a for loop or while loop.
5 T The compiler will report all syntax errors during compilation.
6 A First, two consecutive characters cannot be the same. The first and the third, the second and the fourth characters cannot be the same. Let the string be 𝑠1𝑠2𝑠3𝑠4
𝑠1 can be arbitrarily chosen (26 choices). 𝑠2 ≠ 𝑠1 (25 choices).
𝑠3 ≠ 𝑠1 and 𝑠3 ≠ 𝑠2 (24 choices). 𝑠4 ≠ 𝑠2 and 𝑠4 ≠ 𝑠3 (24 choices) Answer = 26 × 25 × 24 × 24 = 374400
7 C Compressed files cannot be compressed again effectively.
8 C The program implements bubble sort. counter stores the number of swaps performed.
1st iteration: 3 2 5 4 6 1 7 (5 swaps) 2nd iteration: 2 3 4 5 1 6 7 (3 swaps) 3rd iteration: 2 3 4 1 5 6 7 (1 swap)
Then 3 more iterations (1 swap each) to move 1 to the beginning. Total = 12 swaps.
9 B Let’s try to put an x in the empty on the first row. The chessboard would become xxoo
ooxx xxoo ooxx
Let’s also try to put an o instead. The chessboard would be similar to the given sample.
In both ways the chessboard is uniquely determined. Therefore the answer is 2.
10 D When i = 1, 4 # will be printed (j = 2, 4, 6, 8) i = 2, 3 # will be printed (j = 1, 4, 5)
i = 3, 1 # will be printed (j = 4)
i = 4, 3 # will be printed (j = 1, 2, 3) i = 5, 1 # will be printed (j = 2)
i = 6, 1 # will be printed (j = 1) i = 8, 1 # will be printed (j = 1)
Q A Explanation
11 D The program computes the sum of digits of all numbers from 1 to 2003.
Consider 1 to 1999 only. In the units, tens and hundreds digits, each number 0-9 appeared in each digit 200 times. In the thousands digit, 1 appeared 1000 times.
Therefore the answer is 45 × 200 × 3 + 1000 + 2 + 3 + 4 + 5 = 28014
12 C Both players do not want to lose the whole game, so they need to be the first player in round N. To be the first player in round N, they need to lose in round N-1. However we do not know whether there is a losing strategy, so the answer is C.
13 D Alice will choose to keep losing so that she can be the first player again and again. In the last round, Alice can use the winning strategy to win the whole game.
14 B From the first two inequalities we can obtain 𝑎 > 𝑏 > 𝑑. From the last two
inequalities we can obtain 𝑐 > 𝑏 > 𝑑. The relationship between a and c is unknown.
15 A We try to place frequently accessed documents as near the end as possible.
By placing document 1 and 5 at the ends, and document 4 in the middle. The minimum energy consumption is achieved.
Answer = 5 + 6 + 2 × 4 + 2 × 2 + 3 × 1 = 26
16 C A and B: When 𝑡 > 0 and 𝑝 does not divide 𝑏, the output is a positive number.
17 C We are interested in tmp modulo 5.
Initially, tmp ≡ 0 (mod 5)
After first loop, tmp ≡ 1 (mod 5) After second loop, tmp ≡ 4 (mod 5) After third loop, tmp ≡ 0 (mod 5) After forth loop, tmp ≡ 4 (mod 5) After fifth loop, tmp ≡ 0 (mod 5)
You can see that tmp is a multiple of 5 two times per cycle.
The loop is repeated 99 times so the answer is 19 × 2 + 1.
18 A Let’s examine a[tx][ty] + tx modulo 3.
tx\ty 0 1 2 3 4
0 11+0: 2 3+0: 0 7+0: 1 15+0: 0 1+0: 1
1 16+1: 2 62+1: 0 53+1: 0 44+1: 0 37+1: 2
2 10+2: 0 12+2: 2 11+2: 1 31+2: 0 22+2: 0
Examine also a[tx][ty] + ty modulo 5
tx\ty 0 1 2 3 4
0 11+0: 1 3+1: 4 7+2: 4 15+3: 3 1+4: 0
1 16+0: 1 62+1: 3 53+2: 0 44+3: 2 37+4: 1
2 10+0: 0 12+1: 3 11+2: 3 31+3: 4 22+4: 1
(𝑡𝑥, 𝑡𝑦) = (1,0) → (2,1) → (2,3) → (0,4) → (1,0) The cycle length is 4. The loop is repeated 127 times so it will ends at (0, 4)
Q A Explanation
19 D Note that the question says “donate dollars to the charity if she eat an ice cream and a salad”. It is possible that Alice would donate no matter what.
20 A Overflow may occur for i, ii, iv. Examples are i: a = 2000000000, b = -1000000000
ii: a = 2147483647, b = 0 iv: a = 0, b = -2147483648
In these examples, a > b is true but the expression returns false 21 B The robot moved an odd number of steps so 𝑥 + 𝑦 should be odd.
22 A Similarly, 𝑥 + 𝑦 + 𝑧 should be odd.
23 A Inside the flip function, the ith character will be flipped. After that, all the same character after it (after flipping) will be flipped.
1st iteration: ZBAAAYX (Z becomes A) 2nd iteration: ZYAAABX (Y becomes B) 3rd iteration: ZYZAABX (Z becomes A)
Similarly from the 4th to 7th iteration only the ith character is flipped.
The final result is ZYZZZYC
24 C (a OR b) and ((c OR d) AND e) are both false.
Since (a OR b) is false, (a NOR b) would be true.
25 A Program segment ii outputs IOIOIOIO while program segment iii outputs OOOOI
Section B
Answer and Explanation
A d+m*A+y*B where A, B satisfy the following conditions Consider Jan 31 and Feb 1, 31 < 𝐴 + 1 so 𝐴 > 30
Consider Dec 31 and Jan 1 of the following year 31 + 12𝐴 < 1 + 𝐴 + 𝐵 so 𝐵 > 30 + 11𝐴
Examples of correct answers: d+m*31+y*372 d+m*100+y*10000
B ((AQB)Q(AQB))
The required result is A AND B, which is equivalent to NOT(A NAND B)
C 1, 2, 5, 6, 8, 9
D (i=j)or(i=8-j) i==j||i==8-j
The diagonals are i=j and i=8-j
E i*j mod 10=0 i*j%10==0
There is a * at j=5 when i=2, 4, 6 and 8. There are *s at j=2, 4, 6, 8 when i = 5. Also, the border * happens when i or j is 0 or 10. Therefore, the formula is related to even numbers and 5 and 10. The smallest such number is 10.
F -f(-a,-b,-c)
After negating all the numbers, the largest number will become the smallest number.
G a+b+c>-2*f(-a,-b,-c)
The longest edge has cannot be longer than the sum of the other two edges.
The above inequality can be obtained by adding both sides by the longest edge.
H 13 52
I while(n mod k=0)do while(n%k==0){
If N has a prime factor k of any positive degree, the program will only k output once.
For example when n = 8, the program will output 2 * 4 instead of 2 * 2 * 2.
J 2000
Answer = 5 × 4 × 5 × 4 × 5
K 72367
The 1201st-1600th lucky number has 7 as the 10000s digit.
The 1201st-1300th lucky number has 2 as the 1000s digit.
The 1221st-1240th lucky number has 3 as the 100s digit.
The 1231st-1235th lucky number has 6 as the 10s digit.
The 1231st to 1235th lucky numbers are : 72361, 72363, 72365, 72367, 72369
L a[1]*a[3]*a[5] mod 2=1 a[1]*a[3]*a[5]%2==1
Check that a[1], a[3], a[5] are all odd. By multiplying them together, if any of them is even, the product would be even.