Candice enjoys candy like any other girl her age. One day, she asks her mother to lend her money to buy candy.
Her mother agrees and lends her $ 4 0 but specifies a few rules first.
She agrees and heads over to the candy shop.Over there she notices that the shop offers candies of 4 different types, which are priced at $ 1 , $ 2 , $ 5 and $ 1 0 respectively.
If Candice picks each candy one at a time, In how many ways can Candice make her selection?
Details and Assumptions
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Here is an efficient Dynamic Programming solution. Let N ( c , d ) denote the number of ways of buying exactly c candies with exactly d dollars. Also denote the prices of the candies by p i , i = 1 , 2 , 3 , 4 . Then we have the following recursion N ( c , d ) = i = 1 ∑ 4 N ( c − 1 , d − p i ) with the convention that N ( a , b ) = 0 if b ≤ 0 . We have the initial condition that N ( 1 , p i ) = 1 , ∀ i and N ( 1 , k ) = 0 otherwise. This fully specifies the recursion. We are interested in the quantity ∑ i = 1 1 0 N ( i , 4 0 ) .
Complexity for c candies and a total of d dollars is Θ ( c d ) .
2-D DP code in C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
**The whole issue here was solving the equation a+2b+5c+10d=40, where constraints were all of them have to be integers and 0<a+b+c+d<11. Now to account for the different arrangements , one solution will give (a+b+c+d)!/(a!b!c!d!) different arrangements. Due to being a layabout, I did it in R instead of C++.)
Python 3.4:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Challenge:
What will be the solution if Candide received $400 and could buy up to 100 candies?
Log in to reply
My Dynamic Programming code terminates in less than a second. The answer is ≈ 9 . 5 4 9 9 × 1 0 5 7
One way would be to calculate the sum of coefficients of x 4 0 in the expansion of ( x + x 2 + x 5 + x 1 0 ) n where 1 ≤ n ≤ 1 0 . We can further simplify calculations by noticing that there are no possible combinations below a combination of 4 candies since the costliest is worth $ 1 0 .The only one of 4 in total is 4 candies of $ 1 0 each.
So essentially, you would need to calculate only for 5 ≤ n ≤ 1 0 and add 1 to the final answer, giving 4 4 6 6 2 .
Alternatively, you can calculate the sets of A n where A = { 1 , 2 , 5 , 1 0 } and examine exactly how many of these add up to a total of 4 0 . Here as well, you can limit your calculations to 5 ≤ n ≤ 1 0 and add 1 to the total. You would arrive at the same answer.
Other approaches are more than welcome, and you are encouraged to post your code even if you used this very same method.
I seem to find only 20 combinations which have less than equal to 10 candies and total exactly to $ 4 0 . Listed below are the combinations of number of candies of each denomination that satisfy the given conditions.
$ 1 | $ 2 | $ 5 | $ 1 0 |
1 | 2 | 7 | 0 |
0 | 0 | 8 | 0 |
0 | 5 | 4 | 1 |
3 | 1 | 5 | 1 |
1 | 2 | 5 | 1 |
0 | 0 | 6 | 1 |
2 | 4 | 2 | 2 |
0 | 5 | 2 | 2 |
5 | 0 | 3 | 2 |
3 | 1 | 3 | 2 |
1 | 2 | 3 | 2 |
0 | 0 | 4 | 2 |
4 | 3 | 0 | 3 |
2 | 4 | 0 | 3 |
0 | 5 | 0 | 3 |
5 | 0 | 1 | 3 |
3 | 1 | 1 | 3 |
1 | 2 | 1 | 3 |
0 | 0 | 2 | 3 |
0 | 0 | 0 | 4 |
Log in to reply
Hi, thanks for the reply. I've mentioned the change in the report filed! Sorry, I forgot to mention earlier that the order was unique.
Coefficient[Expand[Sum[(x + x^2 + x^5 + x^10)^t, {t, 1, 10}]], x^40]
Log in to reply
Yes, Mathematica one-liner, this is! Do you know how mathematica evaluates the Coefficient[] and Expand[] funtion ? If so, would Sum[Coefficient[]] be more efficient or Coefficient[Sum[]] ?
Log in to reply
I do not know that but you can experiment with the Timing command.
The question is, does 0.1 and 0.001 seconds have a difference to you ?
This solution maintains a table c[n][d], indicating in how many ways n candies can be bought for $d.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
I also took the challenge listed below: What will be the solution if Candide received $400 and could buy up to 100 candies? My code gives 9.549933817559471E57 within a fraction of a second.
Problem Loading...
Note Loading...
Set Loading...
In Python 3.4: