Hong Kong Olympiad in Informatics 2015/16 Heat Event (Senior Group)
Official Solution
Statistics (N = 193)
Full mark = 44. Maximum = 35. Median = 11. Advance to Final = 13 marks or above.
Section A
Q A Explanation
1 C Below is the calculation:
Loop a (before) a (after) b (before) b (after)
1 8 16 2016 2020
2 16 32 2020 2024
3 32 64 2024 2028
4 64 128 2028 2032
5 128 256 2032 2036
6 256 512 2036 2040
7 512 1024 2040 2044
8 1024 2048 2044 2048
9 2048 4096 2048 2052
So the answer is 2052.
2 B Consider the values of y when x is equal to different value.
x Range of y
0 0 – 0
1 0 – 1
2 0 – 3
3 0 – 7
4 0 – 15
5 0 – 31
6 0 – 63
7 0 – 127
8 0 – 255
So, the answer = 0 + 1 + … + 255 = 255 × 256 ÷ 2 = 32640
3 D The root of a tree is a node that does not have any parent. So the answer is D.
4 A There are total 9 depths since 1 + 2 + 3 + … + 9 = 45.
And the sum of all depths = 1 × 1 + 2 × 2 + … + 9 × 9 = 285 ≠ 72, so (ii) is incorrect.
The nodes at the bottom (ie. with depth 9) must be leaves. If the number of leaves is 9, then all the nodes with depth less than 9 have at least one child. According to the property of this tree, there are exactly one mode node at the next depth, so every node has at most 2 children, T will be a binary tree if the number of leaves is 9, so (i) is correct.
To obtain the max. no. of leaves, all nodes with depth (i + 1) should connect to a single node with depth i, in this case, the total no. of leaves = 0 + 1 + 2 +… + 7 + 9 (all the nodes at the bottom must be leaves) = 37, so (iii) is incorrect.
5 B All the n statements are contradictory, so at least n – 1 people are lying. Suppose all the n people are lying, in this case, Pn’s statement is correct, which violates our
assumption. So, only n – 1 people are lying, which means Pn-1 tells the truth.
6 D The possible outputs are -2, -1, 0, 1 and 2
7 A Consider only the worst case, the time complexity of merge sort is O(n lg n) while the three other sorting algorithm will give a time complexity of O(n2).
8 D We may only perform binary search on a sorted array or something with monotone.
9 D Since i is always less than n and i is not zero, so i % n will never be zero, so the program will always output “Prime” if n is valid.
10 Cancelled
11 B Let Fn be the nth Fibonacci number, tri[i][j] is actually equal to Fi-j+1 when j ≠ 0 and equal to 1 when j = 0.
So tri[7][1] = F7 = 13 12 B Similar to Q.11
tri[13][7] = F7 = 13
13 A When n = 31, i & (n – i) will always equal to zero for 0 <= i <= n. Actually, this is true for any n where n = 2k – 1 where k is any positive integer.
14 C 7 is a special number since 7 = 111(2), and therefore, 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7
= 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0
= (0 xor 7) + (1 xor 7) + (2 xor 7) + (3 xor 7) + (4 xor 7) + (5 xor 7) + (6 xor 7) + (7 xor 7).
Similarly, 8 + 9 +… + 15 = (8 xor 7) + (9 xor 7) + … + (15 xor 7).
And so (0 xor 7) + (1 xor 7) + … + (100 xor 7)
= 0 + 1 + … + 95 + (96 xor 7) + (97 xor 7) + (98 xor 7) + (99 xor 7) + (100 xor 7)
= 95 × 96 ÷ 2 + 103 + 102 + 101 + 100 + 99
16 C Due to the last in first out property of a stack, the array will be reversed if it is pushed into and then popped from a stack. And if the array is pushed into and popped from a queue, it will have no effects on the array. So, if only one of P and Q is a stack, then the array will be reversed.
17 B Let’s calculate the no. of stars printed for each procedure.
For a, the no. of stars printed = 2 + 3 + 4 +…+ 11 = 65.
For b, the while loop will only run once, and the no. of stars printed = (20 – 3) / 2 = 8.
For c, when i = 1, there are 4 × 4 = 16 stars. When i = 2, if (j, k) = (3, 4), (4, 3) or (4, 4) there will be no stars, so there are totally 4 × 4 – 3 = 13 stars. When i = 3, if (j, k) = (2, 4), (3, 3), (3, 4), (4, 2), (4, 3) or (4, 4), there will be no stats, so there are totally 4 × 4 – 6 = 10 stars. When i = 4, there will be star only if (j, k) = (1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (3, 1) or (4, 1), so there are 8 stars. Procedure c will print 16 + 13 + 10 + 8 = 47 stars.
For d, all pair of (j, k) will produce exactly 2 stars (i is from 1 to 6), so it will print 7 + 6 + 5 + 4 + 3 = 25 stars.
So procedure b will print the least no. of stars and procedure a will print the most no.
of stars 18 A See Q.17
19 B x[j] is the no. of prime factors of j, the prime factors of 30 is 2, 3 and 5, the prime factor of 37 is 37, the prime factors of 60 is 2, 3 and 5, the prime factor of 999 is 3 and 37.
Therefore x[30] + x[37] + x[60] + x[999] = 3 + 1 + 3 + 2 = 9
20 B After first two sorts, a[0] <= a[1] <= a[2] <= a[3] and a[6] <= a[7] <= a[8] <= a[9] <=
a[10]
After the third sort, a[0] <= a[1] <= a[8] <= a[9] <= a[10] and a[2] <= a[3] <= a[4] <=
a[5] <= a[6] <= a[7] <= a[8] <= a[9]
After the fourth sort, a[0] <= a[1] <= a[8] <= a[9] <= a[14], a[2] <= a[3] <= a[4] <=
a[5] <= a[6] <= a[7] <= a[8] <= a[9] <= a[14] and a[10] <= a[11] <= a[12] <= a[13] <=
a[14].
So a[9] >= a[0] and a[14] >= a[5].
21 D This programme’s output is the index of the first maximum element.
For A, the output is 4, for B, the output is 3, for C, the output is 2, for D, the output is 6.
22 D If (7, 7) is removed, one of the solutions looks like this:
AABCCDD ABBECFD GGHEEFF IGHHJJK IILLJKK MMNLOPP MNNOOP*
If (3, 6) is removed, one of the solutions looks like this:
AABCCDD ABBECFD GGHEEFF GHHIJJK LLIIJKK LM*NOPP MMNNOOP
If (2, 5) is removed, one of the solutions looks like this:
AABBCDD EABCCDF EEGGHFF IIGJHHK I*LJJKK MLLNOOP MMNNOPP
23 C By using some simple algebra, we have c + 2 <= b <= a + 1 <= c + 4.
When b = c + 2, we have c + 2 <= a + 1 <= c + 4, a can be c + 1, c + 2 or c + 3.
When b = c + 3, we have c + 3 <= a + 1 <= c + 4, a can be c + 2 or c + 3.
When b = c + 4, we have c + 4 <= a + 1 <= c + 4, a can only be c + 3.
So there are total 6 different pairs of (a, b) for every c.
Since 0 <= c <= 3, there are 4 possible value of c, the total no. of group = 6 × 4 = 24.
24 A Skylake is the codename used by Intel for a processor microarchitecture which was launched in August 2015.
25 B The best solution is to produce a number of villagers continuously until such amount and wait for 500 units of food. The best solution is explained below.
Time No. of villagers No. of food before No. of food after
0 3 + 4 = 7 200 200 – 200 = 0
0 + 8 = 8 7 + 1 = 8 0 + 7 × 8 = 56 56 – 50 = 6 8 + 6 = 14 8 + 1 = 9 6 + 8 × 6 = 54 54 – 50 = 4 14 + 6 = 20 9 + 1 = 10 4 + 9 × 6 = 58 58 – 50 = 8 20 + 5 = 25 10 + 1 = 11 8 + 10 × 5 = 58 58 – 50 = 8 25 + 45 = 70 11 8 + 11 × 45 = 503 503 – 0 = 503 70 seconds is the fastest time.
Section B
Answer and Explanation
A i := 5 downto 2 do i = 4; i >= 2; i--
We need to do the copying from right to left, or the data will be overwritten.
B s[i - 1] s[i - 1]
C 1, a-b 1, a-b
1 > (a – b)2 if and only if a = b
D 101, a-b-101 101, a-b-101
Let c = a – b - 101
If a > b, then 99 > c > -101 and so 1012 > c2, if a <= b, c <= -101 and so 1012 <= c2. There exist other solutions such as: a-b,a-b-1
E 7
The program’s output is the no. of 1s minus the no. of 0s of n in binary.
7 = 111(2) and it is the minimum possible number to give the output 3.
F
2008
The program’s output is the no. of 1s minus the no. of 0s of n in binary.
2016 is the maximum valid input and 2016 = 11111100000(2), which have 11 bits, and we may deduce the answer has 7 bits of 1 and 4 bits of 0, and the answer turns out to be
11111011000(2) = 2008
G 21 61
H if (a[l] = x) then if (a[l] == x)
After finishing the binary search, r will be the index of the last number less than x, and l is equal to r + 1, a[l] must not be less than x and all the element after a[l] will be greater than a[l], so a[l] = x if and only if x is an element of a.
I1 Primes and their powers (eg. 31, 32)
Suppose x = a * b (a <= b), the programme will output a then a * b which is incorrect. If x is a prime or a prime power, then
I2 All other numbers (eg. 33, 34)
See I1.
J abs(a-x)+abs(y-b)=3 abs(a-x)+abs(y-b)==3
abs(a-x)+abs(y-b) is the Manhattan distance of the center of two crosses. If two crosses are connected, the distance of the center is 3. Since the two crosses do not overlap, so the
difference in x and y coordinates of two crosses will not be 1, therefore if their distance is 3, their center will be in either the same row or the same column.
K (abs(a-x)+abs(y-b)=4)and(a<>x)and (y<>b)
abs(a-x)+abs(y-b)==4 && a!=x && y!=b
If two crosses touch each other, the distance of the center is 4. Since the two crosses do not
Answer and Explanation L
The function shape will make the turtle draw something and return back to the original position and facing the original direction. shape 90 will make the turtle draw a triangle like this: ◁