There are types of apples. For a fixed type of apple, , there is an unlimited number of apples of that type. The cost of buying an apple of type depends on how many apples of type you have already bought. In particular, the first apple of type costs yen. The second apple of type costs yen, the third costs yen, etc. In general, the apple of type costs yen.
You would like to purchase a total of apples. Among all the ways of purchasing apples, you choose the set of apples that costs the least, in total. For this set of apples, what is the most expensive apple you purchase? (The answer is guaranteed to be unique. Also, print your answer modulo )
Note that .
- Binary numbers, powers of 2
- Greedy
- Binary Search
- Sort all apples, take the smallest first
- "Search for a pattern"
- SQRT-Decomposition?
- Different algorithm depending on if K is large or small?
Review
Summary
This was a really hard problem for me. I was unable to solve it in the contest. Even after inspecting Petr's solution, it took me a while to understand and derive the actual answer for myself. Here is my summary of the approach to solve the problem -- hopefully we will be able to recreate the "correct train of thought" needed to solve this problem.
The first initial observation is that "the algorithm" for choosing apples is fixed. You will always buy the cheapest apple at any point in time, until you have total apples. The answer is the value of last apple you buy. Remember this, because we can use this fact throughout the analysis.
Now, we cannot simply "enumerate" or "list" all the apples explicitly, as and are too large to do this. But is there any efficient way to determine what this "last apple" would be?
At this point, we work backwards. What if we knew the final value of the apple? Or, what if we had a "guess" for what this value might be? Is there any way of checking whether is a good guess or a bad one? In theory, the answer is yes. Particularly, let's suppose we were able to count the total number of apples with value . If this number is strictly smaller than , then we can be sure that is too small of a guess. This is because of our "initial observation": we will always choose the cheapest apples until we get of them. And if there are less than apples with value , then we must pick at least one apple with a value in order to get apples in total. Conversely, if the answer we get is at least , then we know is a good guess, and we will stop at or earlier than .
This leads to a "binary search" idea. Fix a , and count the total number of apples with value . If this is less than , try a bigger . Otherwise, try a smaller .
There are two issues with this, though. First, might be extremely large! The numbers in the input can become as large as or bigger. The second issue is: "How do we count the total number of apples with value ?"
To answer both of these questions, and to (hopefully) lead to further insights, we both look at examples and draw a picture. Consider an example where and (you might also try smaller examples; we may use bigger examples later). To "illustrate", we draw an matrix, where cell has value (the cost of the copy of apple ). This matrix is drawn below.
Furthermore, suppose that we have a guess for . For example, let us guess that . Then we would choose all apples with value . In the matrix below, we have coloured/shaded the related cells.
1248163264128256512
2481632641282565121024
36122448961923847681536
4816326412825651210242048
51020408016032064012802560
61224489619238476815363072
714285611222444889617923584
8163264128256512102420484096
9183672144288576115223044608
10204080160320640128025605120
You can verify that there are apples with value . Since , this is large enough.
At this point, we can play around with different values of , and try to note down any observations we find. Here are a couple of these key observations (it's often helpful to write out our findings):
- Without loss of generality, our minimum will always be an element in the table, of the form for positive integers and (with ). If we have a guess for that is not an actual element of this form, we can simply decrease until we find a guess that is of this form. For example, .
- Given a fixed , consider the columns in the matrix from left-to-right, and we notice a pattern: there will be a few columns where all the cells are selected, followed by columns in which the number of selected cells decreases by (approximately) half from column-to-column.
To some readers, these may seem almost obvious. But focusing on these observations helps us to answer the questions we had earlier! In particular, we can use this "pattern" to quickly count the total number of apples with values . Firstly, we write with (which is possible from Observation 1). Then we consider the "pattern" of cells that would be selected when looking at values . In columns , all K cells must be chosen. Then in column , we get exactly cells. In column we get cells, and in column we get cells, and so on. This goes to 0 in a logarithmic number of steps.
We can formalize this argument. Let
**Then, for a number (with the largest possible such that ), there are exactly
apples with value .**
I omit the proof, but the idea can be seen from the picture. One important detail must be discussed however: ambiguity. Some numbers, like appear multiple times in the table. This corresponds to many different ways of writing as . For the proof to work (and this also turns out to be the most intuitive assumption as well), we assume that is as small as possible (equivalently, that is as large as possible) among all representations of the same number. This is the occurrence in the "left-most" occurrence of the number in the matrix. For example, we write as rather than or.
We notice that can be counted in logarithmic time (since the parameter is cut in half iteratively), and the part can be computed in time. So this answers one of our earlier questions about being able to efficiently count the number of apples for a given guess .
If the numbers were smaller, we could do a binary search, and we'd be done. However, we need a way to quickly make guesses for without explicitly having to represent it -- since the number can be really large. Also, in our counting formula above, we assumed had a certain form: .
To deal with these issues, we represent the number using the most natural idea based on the data we have. We always represent as a pair , meaning that , and is as large as possible without being bigger than .
At this point, I think a cleverly crafted search, similar to "Gallop search" or something like that, might be able to help us. We are simply looking for the parameters that yield a minimum so that
One problem, however, is that we don't have a nice bound on . How large of a would be necessary? How small of a would be sufficient?
Now that we have a nice formula, we can use it to find the bounds we are interested in. We know that . In fact, we know that , because we chose to be the largest among all representations. (If then we could let and to get a better representation of our number . This would usually be a contradiction.) Anyway, putting this together with our formula, gives us that:
Now, we want to find such that . What is the smallest we can have? Well, we can apply the second inequality above and assume (since this is the largest choice of for any fixed ). From this we would get:
or
And note that substituting was valid. If is smaller than , then we would need an even larger value of to compensate. Hence, we have proven the following theorem: **For any with , if there are at least apples with value , then:
j \geq j^* := \left\lceil\frac{N - S(K)}{K}\right\rceil + 1 $$** This gives us a *lower bound* on $$j$$ for valid $$(i,j)$$ pairs. But we are actually interested in an *upper bound* on $$j$$. If we draw more pictures, look at more examples, or go back to our formulas, we realize that this value $$j^*$$ actually tells us a lot more than we thought! We can actually prove another lemma: **if $$j > j^*$$ and $$i > \frac{K}{2}$$, then the value $$(j-1)K + S(i)$$ will be at least $$(j^*-1)K + S(K)$$**. So any minimal $$v$$ will have $$j \leq j^*$$. This is best shown with a picture. I omit such a picture, or a detailed explanation/proof, but it also follows from the inequalities above. I encourage the reader to prove why this is true. In any case, we have therefore discovered that, once we have found $$j^*$$, our "optimal" (minimal) $$v$$ will be of the form $$i \cdot 2^{j^*-1}$$ for some $$1 \leq i \leq K$$. With this information, we can use a simple *binary search* on $$i$$, checking whether $$(j^* - 1)K + S(i) \geq N$$ at each step or not. Once we've found our minimal $$i^*$$, we output the answer $$v = i^* \cdot 2^{j^*-1}$$ (modulo the number $$1,000,000,007$$, as asked in the problem statement.) Pseudo-code: <AlgorithmBox> ```text <code class="python"> S(n): answer := 0 while(n > 0): answer += n n = n / 2 return answer lastPrice(N,K): Let jstar = ceil((N - S(K)) / K) + 1 # The optimal j^* low = 1 high = K while low < high # binary search > i := (low + high) / 2 if (jstar - 1)*K + S(i) >= N: # how many apples no more than v = i*2^(j*-1)? high = a else low = a+1 istar = low # same as high return istar * power(2, jstar - 1) MODULO 1,000,000,007 </code> ``` </AlgorithmBox> 1. **When choosing apples, the order is fixed. We always choose the least costly apple first, and then repeat until we have $$N$$ apples.** This simple observation helps to point us in the right direction. We know that the algorithm for choosing apples is simple, so we can exploit its "greedy" properties later to achieve a good algorithm for counting the apples. 2. **Consider a minimum guess $$v$$. Then $$v$$ must be an element of the form $$i \cdot 2^{j-1}$$ for some $$1 \leq i \leq K$$. Moreover, without loss of generality, we can assume that $$i$$ is maximal among all such representations of $$v$$.** A rather obvious observation. But its helpful to write it down. This characterization actually allows us to focus our attention on "special" values of $$v$$. 3. **Consider a guess $$v$$ of the form $$i \cdot 2^{j-1}$$ with $$1\leq i \leq K$$ and $$i$$ maximal over all such representations. Then, the total number of apples with value $$\leq v$$ is exactly $$(j-1)K + S(i)$$, where $$S(i) := i + \left\lfloor i/2\right\rfloor + \left\lfloor i/4\right\rfloor + ... + \left\lfloor i/8\right\rfloor + ...$$** This was the key formula that enabled us to solve the counting problem! Once we have this formula, the problem becomes much easier to solve. 4. **Consider a value $$v = i \cdot 2^{j-1}$$ such that at least $$N$$ apples have value $$\leq v$$. Then it must be the case that $$j \geq j^*$$, where $$j^*:= \left\lceil\frac{N - S(K)}{K}\right\rceil + 1$$.** This can be proven from the formula above, and by using the fact that $$i \leq K$$. Basically, as long as $$j < j^*$$, then you can prove that $$(j-1)K + S(i) \lt N$$. The statement above is the *contrapositive* of this (and is therefore equivalent). 5. **Consider *the minimal* $$v = i \cdot 2^{j-1}$$ such that at least $$N$$ apples have value $$\leq v$$. Then $$j \leq j^*$$, as defined above.** This is the upper bound to match the lower bound. This tells us that for any optimal $$v$$, there is a single $$j^*$$ that we must use. It simply remains to find the $$i$$. Note that this statement can be proven using the observation that $$i \gt \frac{K}{2}$$ whenever $$j > j^* \geq 1$$. Why? Because if $$i \leq \frac{K}{2}$$ then $$v = (2i) \cdot 2^{j-2}$$. And this is a representation with a bigger value of "$$i$$", so this would contradict our choice of representation. Once you have this fact, you can subsitute this inequality into the formula (some how...) and eventually derive the result. Alternatively, you can draw some pictures, which make a lot more sense. 6. **Once $$j^*$$ is fixed, we can binary search on $$i$$ to find the final answer.** This is because the matrix is increasing in $$i$$ (obviously). So we can binary search, and use our formula to check whether $$i$$ is too small or not. 1. I was unable to solve this problem in contest. So it was beneficial to look at the solution and try to re-construct it for myself. Learning from other people is a good way to improve. Hopefully I have learned something! And same to you as well, dear reader! 2. I would say this is the most important problem-solving technique used here. Instead of asking "How do we find the value of the last apple?", we ask the question "Can we decide whether a particular value $$v$$ can possibly be the last apple?" This reversing of the question changes it from a *discovery* problem to a *decision* problem. And in this case, it leads to a *binary search* idea, because we suspect that we can quickly answer the related question: "Is my guess for $$v$$ too big or too small?" This immediately leads us onto the right track. 3. We followed the idea of guessing a particular $$v$$ and checking if it is too big or too small. This leads to the following method: "Count the total number of apples with value $$\leq v$$. If this is less than $$N$$, then $$v$$ is too small. Otherwise, $$v$$ is big enough." This turned our *decision* problem into a *counting problem*. Intuitively, because of the structure of the numbers, it *feels* like we may be able to answer this question efficiently. 4. After having the above problem transformation, its best to look at a few examples with different $$N$$,$$K$$ and guesses for $$v$$. I admit I did not do this much during the contest (time pressure!) but I followed this approach when I came through this problem a second time. It is sure to lead to some insights! 5. The description of the problem leads itself to a nice "matrix" picture. Using this pictorial representation makes a lot of "proofs" easier later on, and also makes it fairly easy to find the key observations we are looking for. 6. All numbers are given to us in the form $$i \cdot 2^{j-1}$$ (by definition of the problem). So, when looking for a nice way to represent our value $$v$$, it was convenient to always right it in this form. - **When you get stuck, spend some time looking at reasonable examples.** In the contest, I spent a lot of time trying to write down observations about the problem, but with no success. I think the best approach would have been to work out a reasonable example with particular guesses for $$v$$, to get an idea about how to actually solve the problem. The core ideas needed to solve it became fairly "obvious" once you look at enough examples, but can be mysterious if you are simply looking at the algebra. I'll try to internalize this for the future. - **Beware of going down the wrong path.** This might be impossible to detect without simply developing a better intuition. But I spent most of the contest thinking about a "SQRT Decomposition" approach. I was thinking, if $$K$$ is large, try one algorithm; if $$K$$ is small, try another. And the difference between "large" and "small" was likely going to be around $$sqrt(10^9)$$ or something similar. I was trying this approach because it seemed like a reasonable approach based on other problems I've solved. But it was completely wrong. Instead, I should have stopped and focused on generating correct insights about the specific problem, rather than trying to use "a specific trick" (such as SQRT Decomposition) to solve the problem. I need to get better at recognizing bad ideas. - Math / Formulas / Counting - Binary Search - Observation / Insight - Greedy