Kick Start 2019 Round G
This round was the second-to-last round of the year, and also a relatively easy one.
I solved the third problem and the small data sets for problems 1 and 2. My algorithm for the second problem was correct in itself, but I did not correctly estimate the maximum number of bits in k or prevent overflow, so I got WA on the large data set. The first problem was not hard either, but I was not sensitive enough to divisors, so I missed the better solution. Overall, this was the closest I got to AC in a round. My luck was relatively good, and I finished the contest one hour early. Later, because I really could not think of more solutions or details to watch out for, I gave up.
Book Reading
Brute force plus memoization can pass directly. But I wrote the memoization wrong and forgot to actually memoize. That caused TLE and lost quite a few points. What a pity.
Also, if you can use long long, do not use int; otherwise the final ans will overflow.
Time complexity: O(N log N).
Because memoization exists, the number of pages calculated is at most 1 + 1 / 2 + 1 / 3 + ... + 1 / N = log N, the harmonic series.
1 |
|
The Equation
There are quite a few details to pay attention to in this problem, though the algorithm itself is not difficult.
- The
long longconstant should be1L; otherwise1defaults toint, which creates overflow risk. - We cannot start searching from the highest bit in A, because
kmay be a larger number. Instead, we should consider the maximum value ofkfrom the perspective of M. Here M has 15 decimal digits, corresponding to around 50 binary bits. However, we also cannot start from too high a bit, otherwiseN * (1 << i)will overflow 64 bits. Since the maximum N is 1000, around 10 binary bits, we can start searching from bits 50 to 53.
Since we want to maximize k, we can use a greedy idea: from high bits to low bits, try to place 1 whenever possible; otherwise place 0 and backtrack when the requirement is exceeded.
Because there is a lot of backtracking and pruning, the time complexity is not easy to analyze. But it can pass the large data set.
1 |
|
Shifts
Same as the second problem, I used backtracking with a lot of pruning and memoization to enumerate all combinations. I did not expect it to pass.
The official solution uses divide and conquer: enumerate the combinations of two smaller sets separately, then iterate over all combinations and find the number of combinations that satisfy the requirements after merging. This is the meet-in-the-middle approach.
1 |
|