PROBLEM LINK:
Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4
Author: raysh07
Tester: iceknight1093
Editorialist: iceknight1093
DIFFICULTY:
Easy
PREREQUISITES:
None
PROBLEM:
Construct an array A of size 20 satisfying the following condition:
- For each integer i from 1 to 5\cdot 10^4, there exists a subset of A having size at most 10, and whose sum equals i.
EXPLANATION:
We’re only allowed 20 numbers, but we need to be able to obtain every sum upto 5\cdot 10^4.
This suggests that the numbers we choose must be rather widely spaced out.
A natural first choice would be to try powers of 2 - after all, every number can be written as the sum of distinct powers of 2 (i.e. in binary), and there aren’t really that many powers of 2 till 5\cdot 10^4.
In fact, there are only 16 powers of 2 available to us; since 2^{16} = 65536 \gt 5\cdot 10^4.
However, we run into a problem here with the second restriction of needing to take a subset having size at most 10.
With the first 16 powers of 2, there are certainly numbers that will need more than 10 of them to write - in fact, we could just take any subset of the powers of 2 with size \gt 10, and run into trouble there - and there are actually quite a few such numbers.
We do have four values left to fix this, but it’s not really clear how we can do that.
Instead, we try to take our solution in a different direction - rather than binary, we’ll use ternary; that is, we’ll work with powers of 3 instead of 2.
Recall that in the ternary system, any number X can be written uniquely as
where 0 \le c_k \lt 3.
In particular, this means any number is written by adding up only integers of the form 3^k or 2\cdot 3^k.
This means, if we were to take all the numbers of the form 3^k and 2\cdot 3^k for 0 \le k \lt 10, we would be able to write every integer from 0 to 3^{10}-1 as a sum of at most 10 of them; because for each power of 3 from 0 to 9, we’d have to only decide whether we want to take 0, 3^k, or 2\cdot 3^k.
Now, observe that 3^{10} = 59049 \gt 5\cdot 10^4, so we can indeed write every value till 5\cdot 10^4 like this.
And we’re done!
Quite simply, the solution is to take the following set:
TIME COMPLEXITY:
\mathcal{O}(1) per testcase.
CODE:
Editorialist's code (PyPy3)
n = int(input())
for i in range(10):
print(3**i, 2*3**i)