跳转至

Restocking the Holy Palanquin Ship's Confectionery: Alternative Solution

Contribution: Codex (GPT-6) adds the complete-knapsack model, proof, and program. The cost-weighted answer formula is in the preferred explanation.

Initially only the empty plan exists: \(P_0=1\) and all other entries are zero. Process prices \(i=1,\ldots,X\) in order. For costs \(s=i,\ldots,X\) in increasing order, apply \(P_s\leftarrow P_s+P_{s-i}\) modulo the required modulus.

Before processing a price, the array counts plans using lower prices only. A plan either uses no item of the current price, represented by the old value, or at least one. Removing one such item gives a plan counted at the already-updated smaller cost. Increasing-cost updates allow repeated items of the current price. Processing prices in order avoids counting purchase orders separately. Induction proves that the final array contains every cost count, after which the weighted sum yields the answer.

Time is \(O(X^2)\) and space is \(O(X)\) , with constant work for zero budget. This is a direct parallel method that needs no number-theoretic identity; the pentagonal recurrence has a lower time order. The budget itself enforces the stock bound, so no stock expansion is needed.

Program: alternative-knapsack.cpp.