Problem. A worker is paid one link of a 7-link gold chain per day for 7 days, at the end of each day. Cutting a link is expensive. What is the minimum number of cuts? (Change may be given back: on day the worker must hold pieces totalling exactly links.)
Answer. One cut, at the third link, leaving pieces : day 1 give the single link; day 2 give 2 and take 1 back; day 3 give 1; day 4 give 4 and take back 1 and 2; and so on, binary counting.
| Variant | Best |
|---|---|
| gold bar of units, cut into pieces (each cut makes one more piece) | cuts give pieces : pays up to days |
| chain of links, a cut opens one link (it becomes a single piece and splits the chain) | cuts pay up to days: |
| pieces for cuts | single links and segments |
| general principle | a multiset of piece sizes pays every amount iff, sorted, each piece is at most the sum of the smaller ones |
int goldChainCuts(ll n) { // fewest cut links so that a chain of n links pays 1..n
int k = 0;
while (((ll)(k + 1) << (k + 1)) - 1 < n) k++;
return k;
}
vector<ll> goldChainCutPositions(ll n) { // 1-based positions of the links to cut
int k = goldChainCuts(n); vector<ll> seg, cuts;
ll rem = n - k; // links left after the k cut links
for (int i = 0; i <= k; i++) { seg.push_back(min((ll)(k + 1) << i, rem)); rem -= seg.back(); } // capped doubling
ll pos = 0;
for (int i = 0; i < k; i++) { pos += seg[i] + 1; cuts.push_back(pos); }
return cuts;
}
bool paysAll(vector<ll> pieces) { // every amount 1..sum is a sub-multiset sum
sort(all(pieces)); ll reach = 0;
for (ll p : pieces) { if (p > reach + 1) return false; reach += p; }
return true;
}Proof: the completeness criterion and the optimal number of cuts
Criterion. Sort the pieces and let . If , the amount cannot be paid: pieces are each too large and the smaller ones total only . Conversely, if and every amount in is payable from the first pieces, then every amount in is: amounts up to directly, larger ones as plus an amount in . Day by day the worker can return pieces, so paying “a total of on day ” for all is exactly this condition.
Upper bound. With cut links the pieces ( times), satisfy the criterion: after the singles , and each segment equals . Their total is ; for a shorter chain cap the segments in order at the links that remain (a capped segment is still at most , and later ones become empty), which keeps the criterion. The positions follow by laying the segments left to right with a cut link between consecutive ones.
Lower bound. cuts produce single links and at most segments. Sorted, the first pieces are ones (), and the criterion gives , i.e. at most doubles with each further piece. After at most more pieces, . So a chain longer than needs more than cuts. For a bar the same doubling with no singles gives . Checked against brute force over all sets of cut positions for every .
Related notes
- Bit Tricks and XOR (binary representations)
- Bachet’s Weights (the same idea with a balance: powers of 3)
- Greedy and Exchange Arguments (the “smallest unreachable sum” greedy)