Candice's trip to the candy shop

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 $ 40 \$40 but specifies a few rules first.

  • Candice needs to use all $ 40 \$40 and should not have any remaining money.
  • Her mother doesn't want her to get carried away so Candice can buy a minimum of 1 and a maximum of 10 candies in total.
  • Candice can buy as many of each type of candy she wants provided the above conditions are satisfied.

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 $ 10 \$1 , \$2, \$5 \text{ and } \$10 respectively.

If Candice picks each candy one at a time, In how many ways can Candice make her selection?

Details and Assumptions

  • As an explicit example, Candice can buy 4 candies of $ 10 \$10 each so that she would be left with no money extra.
  • Order of purchase matters. If Candice buys the candies as ( 5 , 5 , 10 , 10 , 10 ) (5,5,10,10,10) and ( 5 , 10 , 10 , 10 , 5 ) (5,10,10,10,5) both should be considered.
Image Courtsey : animekida
Check out Candice's Other Adventures!


The answer is 44662.

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.

7 solutions

Abdelhamid Saadi
Aug 27, 2015

In Python 3.4:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
Candide_ = {}

def Candide(n, depth):
    "Candice's trip to the candy shop"
    if depth == 0:
        res = 1 if n == 0 else 0
    elif n == 0:
        res  = 1
    elif (n, depth) in Candide_:
       res  =  Candide_[(n, depth)]
    else:
        res = sum(Candide(m, depth - 1) for m in [n-1, n-2, n-5, n-10] if m >= 0)
        Candide_[(n, depth)] = res
    return res

print(Candide(40, 10))

Abhishek Sinha
Aug 26, 2015

Here is an efficient Dynamic Programming solution. Let N ( c , d ) N(c,d) denote the number of ways of buying exactly c c candies with exactly d d dollars. Also denote the prices of the candies by p i , i = 1 , 2 , 3 , 4 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 ) N(c,d)=\sum_{i=1}^{4}N(c-1,d-p_i) with the convention that N ( a , b ) = 0 N(a,b)=0 if b 0 b\leq 0 . We have the initial condition that N ( 1 , p i ) = 1 , i N(1,p_i)=1, \forall i and N ( 1 , k ) = 0 N(1,k)=0 otherwise. This fully specifies the recursion. We are interested in the quantity i = 1 10 N ( i , 40 ) \sum_{i=1}^{10}N(i,40) .

Complexity for c c candies and a total of d d dollars is Θ ( c d ) \Theta(cd) .

Vishal Mittal
Jul 30, 2019

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
#include <bits/stdc++.h>
using namespace std;

int dp[100][20];

int f(int N, int choco)
{
    if(N < 0 || choco < 0)
        return 0;
    if(N == 0)
        return 1;
    if(dp[N][choco] != -1)
        return dp[N][choco];

    return dp[N][choco] = f(N-1, choco - 1) + f(N-2, choco - 1) + f(N-5, choco - 1) + f(N-10, choco - 1);
}

int main() 
{
    memset(dp, -1, sizeof(dp));
    cout << f(40, 10) << endl;
    return 0;
}

**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++.)

Brock Brown
Aug 26, 2015

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
candy_costs = (1, 2, 5, 10)
cost_limit = 40
def children(candy):
    cost = sum(candy)
    candies = len(candy)
    if candies < 10:
        for candy_cost in candy_costs:
            if cost + candy_cost <= cost_limit:
                yield candy + (candy_cost,)
ways = 0
start = ()
level = {0: [start]}
current_level = 0
while level[current_level]:
    current_level += 1
    level[current_level] = []
    for candy in level[current_level - 1]:
        for child in children(candy):
            if sum(child) == 40:
                ways += 1
            level[current_level].append(child)
print ("Answer:", ways)

Challenge:

What will be the solution if Candide received $400 and could buy up to 100 candies?

Abdelhamid Saadi - 5 years, 9 months ago

Log in to reply

My Dynamic Programming code terminates in less than a second. The answer is 9.5499 × 1 0 57 \approx 9.5499\times 10^{57}

Abhishek Sinha - 5 years, 9 months ago
Vishnu Bhagyanath
Aug 22, 2015

One way would be to calculate the sum of coefficients of x 40 x^{40} in the expansion of ( x + x 2 + x 5 + x 10 ) n (x+x^2+x^5+x^{10})^n where 1 n 10 1 \leq n \leq 10 . We can further simplify calculations by noticing that there are no possible combinations below a combination of 4 candies since the costliest is worth $ 10 \$10 .The only one of 4 in total is 4 4 candies of $ 10 \$10 each.

So essentially, you would need to calculate only for 5 n 10 5 \leq n \leq 10 and add 1 to the final answer, giving 44662 44662 .

Alternatively, you can calculate the sets of A n A^n where A = { 1 , 2 , 5 , 10 } A=\{1,2,5,10 \} and examine exactly how many of these add up to a total of 40 40 . Here as well, you can limit your calculations to 5 n 10 5 \leq n \leq 10 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 $ 40 \$40 . Listed below are the combinations of number of candies of each denomination that satisfy the given conditions.

$ 1 \$1 $ 2 \$2 $ 5 \$5 $ 10 \$10
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

Janardhanan Sivaramakrishnan - 5 years, 9 months ago

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.

Vishnu Bhagyanath - 5 years, 9 months ago

Coefficient[Expand[Sum[(x + x^2 + x^5 + x^10)^t, {t, 1, 10}]], x^40]

Agnishom Chattopadhyay - 5 years, 9 months ago

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[]] ?

Vishnu Bhagyanath - 5 years, 9 months ago

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 ?

Agnishom Chattopadhyay - 5 years, 9 months ago
Arjen Vreugdenhil
Sep 26, 2015

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
        int c[][] = new int[11][41];
        int candy[] = { 1, 2, 5, 10 };

        for (int i = 0; i <= 40; i++)
            for (int j = 0; j <= 10; j++) c[j][i] = 0;

        c[0][0] = 1;

        for (int j = 1; j <= 10; j++) {
            for (int i = 0; i <= 40; i++) if (c[j-1][i] > 0)
                for (int k = 0; k < 4; k++) {
                    int new_i = i + candy[k];
                    if (new_i <= 40) c[j][new_i] += c[j-1][i];
                }
            }            

        int sum = 0;
        for (int j = 1; j <= 10; j++) sum += c[j][40];
        out.println(sum);

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.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...