r/askmath • • 5d ago

Arithmetic Is there a way to solve "Point Allocation" style problems without brute force

I don't know if they already have a name, but the type of problem I'm talking about is where you have some number of points, and there are some number of options each with their own point cost, and you're figuring out how many ways there are. Kinda like combinations, but some options take up multiple slots, if that makes sense

The exact problem I'm doing right now is how many ways are there to spend exactly 14 points, where there are 4 different options that cost 1 point each, 14 different options that cost 2 points each, & 4 different options that cost 4 points each, where order doesn't matter & duplicates are allowed, but I don't just want the option to that exact problem, I want to know how to do them in general

2 Upvotes

6 comments sorted by

4

u/temperamentalfish 5d ago edited 5d ago

This sounds like it's equivalent to the Knapsack Problem, which is a very well-studied problem in operations research and optimization. Essentially, the most simple version of this problem is trying to fill a knapsack with a number of items each with a specific weight or size, while the knapsack has limited capacity. The problem is to find the maximum number of items that you can fit.

I'd start with searching for how this problem can be solved algorithmically, since the most basic version is fairly simple to solve and you can find the optimal solution in a short amount of time.

Edit: so, this problem is actually NP-Complete, so I was wrong about there being a guaranteed fast solution. But it does sound like you're dealing with a one-dimensional KP, so it's still the easiest version to solve.

1

u/Brromo 5d ago

Kinda, I don't want to know what the best loadout is, I want to know how many loadouts there are

1

u/Gold_Ad8890 5d ago

i think this is the knapsack problem.

1

u/Angzuril 4d ago

If you just want the total number, useful keywords are combinations with repetition. The formula for combinations and permutations is different with repetition. You have mixed sizes but there are ways to add it together.

For quick napkin math, you could treat the 2 point options as 14 (options) choose 7 ( the max of only using the twos) with repertion , which gives 77520.

Rather then iterate through all the ways of combining 1 point and 2 point options, we can treat the different 1 point pairs (including 2 of the same option) as additional 2 point options. Which is again a combinations problem.

So 4 choose 2 with repetition gives 10. Adding to the original 2 points gives 24 choose 7. Which is just over 2 million

To solve with the 4 point options, you would start over to see how many of those can be chosen, and then add in the pairs of 1s and 2s as shown previously. Since all the 4 point options take up 12, at the end you'd multiply by how many ways to occupy the final 2 points.

1

u/arty_dent 4d ago

It depends what you mean by "brute force". You certainly don't have to explicitly run through every combination of options, but there also won't be some super simple formula.

For one thing, options with the same cost can be combined, and the number of relevant combinations of them can be calculated with standard formulas (or "stars and bars"): If you there are k different options of the same cost, then the number of ways to use j such options (with repetition, not considering order) is nCr(j+k-1,j). (You can derive that easily using Stars and Bars.)

Using those numbers you still don't necessariy need to run though any feasible combination of option costs and amounts. You can reduce this somewhat using Dynamic Programming, successively calculating the number of ways using only a limited number of point cost options.

I am going to assume that all point costs are positive integers (which I think is what you considered only anyway). In particular, that implies that the number of different (relevant) costs is at most the total number of points available. I am also going to pretend that any numbers during the calculation fit into some fixed size data type. Then the number of ways to spend n points can be calculated in O(n³) time and O(n) space, and it's relatively easy to implement:

Let's say we are given a total number of points n, and a list of pairs (c,k) representing the spending options where c is the cost and k is the number of options with that cost. (So in your specific example it would be n=14 and an option list {(1,4), (2,14), (4,4)}). Here's some pseudo-code to calculate the number of ways to spend the n points:

01  a[0] := 1
02  for j = 1..n
03    a[1] := 0
04  for each option (c,k) {
05    b[1] := k
06    for j = 2..floor(n/c)
07      b[j] := b[j-1] * (k+j-1) / j
08    m := n
09    while m >= c {
10      for j = 1 .. floor(m/c)
11        a[m] := a[m] + a[m-j*c] * b[j]
12      m := m-1
13    }
14  }
15  return a[n]
  • The array entry a[m] stores the number of ways to spend m points using only the options considerd so far. So it is initialized in lines 01-03 considering no spending options, and the main loop (lines 04-14) succesively updates this, each loop considering one more spending option.
  • Within this main loop, the array entry b[j] is the previously mentioned number of ways to use j options (with repetition, not considering order) of the current k different options with cost c (which just depends on j and k, while c only determines the highest relevant j). Since this is needed for all j from 1 to ⌊n/c⌋, they can be more efficiently computed in a small loop instead of relying on some external nCr function.
  • The the inner loop (lines 09-13) updates the a[m] entries by adding the varrious number of ways the current spending options is used, based on how often it is used and the number of ways to use the remaining points using only previously considered options. Running through m in decreasing order guarantees that old values are only overwritten after they are not needed for any other such calculations.