There's an equation: 1a + 2b + 3c + 4d ... + 9i = 9
Constraints: 1 <= a + b + c + ... + i <= 104
where a,b,..,i are non-negative integers and each of the integers have a particular range.
For example: 1 <= a <= 5, 2 <= b <= 3, and so on.
I need to find out the number of different sets of values of those variables, or simply, the number of ways of solving that equation.
There's a recursive method of solving this, but that's very slow. I'm unable to think of a way to solve this efficiently under the given constraint.
If you think about it, the solutions are actually very limited.
Since all numbers are non-negative, and you have:
This implies that:
That is, there are only
10*5*4*3*2*2*2*2*2 = 19200
cases to consider.You can iterate through these cases and find out which ones satisfy your other constraints and which ones have
Hint: start by giving values from
i
toa
. This way, a high value ofi
orh
for example, immediately reduces the possible range of values of the smaller numbers.Make sure you apply MSalters' method before computing. Even though in this case it is not necessary, since the problem is too easy, but in general it helps a lot.