Parcel-delivery app~30 min2s · 256 MBtarget O(parcels + vans) time, O(parcels + vans) spacenumber-theory
You work on a parcel-delivery app. A hub has parcels identical parcels to load into vans different vans. A van may stay empty, but no van can carry more than cap parcels.
Your team lead asks: "In how many different ways can the hub load every parcel?" Two plans differ when some van carries a different number of parcels. The count is huge, so report it modulo 1,000,000,007. A hub can have up to 300,000 parcels and 300,000 vans.
Write load_plans(parcels, vans, cap).
parcels is the number of identical parcels. All of them must be loaded.vans is the number of vans. They are different from each other.cap is the most parcels one van can carry. It can be 0.If the vans cannot hold all the parcels, the answer is 0. With no parcels, the only plan leaves every van empty.
Input: parcels = 5, vans = 3, cap = 2
Output: 3Write a plan as the loads of van 1, van 2 and van 3. Each load is at most 2 and the loads add up to 5.
| Plan | Loads | Allowed |
|---|---|---|
| A | 2, 2, 1 | yes |
| B | 2, 1, 2 | yes |
| C | 1, 2, 2 | yes |
| D | 3, 1, 1 | no: the first van carries 3 |
Only the three orders of the loads 2, 2, 1 work, so the answer is 3.
Input: parcels = 6, vans = 3, cap = 6
Output: 28A cap of 6 never binds when there are 6 parcels, so every way to share 6 parcels among 3 vans counts, which is 28.
Input: parcels = 10, vans = 4, cap = 3
Output: 10The loads are 3, 3, 3, 1 in any order (4 plans) or 3, 3, 2, 2 in any order (6 plans).
0 ≤ parcels ≤ 300,0001 ≤ vans ≤ 300,0000 ≤ cap ≤ 300,000Loading the vans one at a time with a table of the parcels loaded so far costs up to vans × parcels × (cap + 1) steps. For 100,000 vans, 100,000 parcels and a cap of 3 that is 40 billion. A formula over the number of vans that break the cap needs at most vans + 1 terms. Each term reads the factorial table, which holds up to 600,001 numbers.