C++20 sections · 1024 units
Open in Course

Stones - Algorithm

Traversal approach

You have stones to distribute between two piles with different weights. Each stone adds aa to pile 1 or bb to pile 2.

1.1. Try all possible distributions: give ii stones to pile 1 (0 to n).

2.2. The remaining nin - i go to pile 2.

3.3. Calculate pile 1 weight: i×ai \times a. Calculate pile 2 weight: (ni)×b(n-i) \times b.

4.4. Count this as a valid outcome. Store all unique (weight1, weight2) pairs.

5.5. The answer is how many unique pairs exist. Use a set to avoid counting duplicates.