跳转至

Restocking the Holy Palanquin Ship's Confectionery: Alternative Solution

Problem setter’s solution: enumerate plans by nondecreasing prices. Codex (GPT-6) organizes the recursion, modular arithmetic, and output. The preferred method is the pentagonal recurrence.

First add the leftover money of the empty plan. A search state specifies the lowest current price and remaining money. Either skip this price or buy one item of it. After a purchase, add the new plan's leftover money, then allow another item of the same price. Expand branches only when they can produce a positive remainder; zero-remainder plans contribute nothing and may be omitted.

Every nonempty plan has a unique nondecreasing price sequence. Skipping prices and buying the current price uniquely constructs that sequence, so no plan is repeated. Every positive-remainder plan reaches its corresponding node by this sequence. Counting the empty plan separately and omitting zero contributions therefore gives the exact sum. For zero budget, do not recurse.

Let \(K=\sum_{s=0}^{X}P_s\) be the number of partitions of costs up to the budget. Every purchase prefix is itself such a plan, and a chain of skipped prices has length at most \(X\) . A conservative time bound is \(O((X+1)K)\) , with \(O(X+1)\) recursion space. Runtime grows with the number of plans, making this useful for small-case understanding and independent checks. The formula is correct throughout the range; whether this implementation meets the reference time limit is recorded in the benchmark. It is not recommended in place of the low-polynomial methods.

Program: alternative-search.cpp.

On the reference machine, the largest five-run median is about 0.410 seconds against a 0.5-second limit. This is only about 1.22 times headroom, below the recommended factor of two. Keep this as an enumeration and checking alternative; full-range acceptance is not promised on deployment. Prefer the pentagonal or knapsack implementation.