Found this question while preparing for interviews.
Suppose that some caterpillars start from the bottom and jump to the next leaf. They eat the leaf before jumping to n
This is just an optimization over the approach suggested by @cimbali's. From the array containing strides for caterpillar. You can remove the multiples of a stride value from that array to reduce the number of combinations found.
For example 24 leaves, and [2,3,4] the caterpillar jump steps.
First step: Go through the strides array and remove multiples of 2. Since 4 is a multiple of 2. Remove 4 from the array
Second step : subsets of size 1. Caterpillar j=2 would, alone, eat 24/2 = 12 leaves Caterpillar j=3 would, alone, eat 24/3 = 8 leaves
Third step : subsets of size 2. Caterpillar j=2 and j=3 would both like to eat 24/6 = 4 leaves : {6, 12, 18, 24}
So 24 - 16 (12+8-4) = 8 leaves remain.