Editorial for WC '17 Contest 1 S4 - Change
Submitting an official solution before solving the problem yourself is a bannable offence.
Let's start by solving the problem for relatively small values of (up to a few thousand). This can be accomplished with dynamic programming. Let be the answer for . That is, the maximum number of denominations no larger than which we can use such that is unattainable.
For a given , assuming that we'll be able to use at least denomination, let's consider each possible value such that the largest denomination used will be . must not be one of the forbidden denominations, and must not be a divisor of .
For a given , the greedy algorithm will use coins of that denomination until it's away from its target amount of money, essentially reducing us to a subproblem with . Along the way, we can also freely use all non-forbidden denominations between and , inclusive.
Let be the number of non-forbidden denominations in the interval , which can be implemented in time using binary search once the values are sorted. This brings us to the recurrence over all valid values of .
The time complexity of the algorithm described above is , which is far too slow for full marks when is as large as . We need to next make the insight that larger values of (ones which are only slightly smaller than ) are generally more optimal than smaller values of . This is fairly intuitive as choosing a smaller value over a larger value means skipping or more valid denominations in the interval which we could otherwise have used. Of course, could still turn out to be larger than , so it's not always optimal to use the first valid denomination smaller than .
However, it's a fairly safe guess that the initial value of should be no smaller than, let's say, . This is convenient because, not only do we only need to try values of to compute , but we only need to have computed to complement this (as will be at most ). We can compute that many values efficiently enough.
To be more precise, we can prove that we only need to consider initial values of no smaller than (and so, only compute ). If we find a valid value such that is a non-forbidden denomination, then is as large as possible (as we'll be able to use all smaller non-forbidden denominations), meaning that it can't be more optimal to skip in favour of any smaller value . A value of meeting these criteria no smaller than must exist (it may be as small as exactly if all denominations in the interval are forbidden, for example). In the end, then, we can achieve a time complexity of .
Comments