## A series of three rooms are connected to each other by automatic doors.

Each door is operated by a security card. Once you enter a room the door automatically closes and that security card cannot be used again. A machine at the start will dispense an unlimited number of cards, but each room (including the starting room) contains scanners and if they detect that you are holding more than three security cards or if they detect an unattended security card on the floor, then all the doors will become permanently locked. However, each room contains a box where you may safely store any number of security cards for use at a later stage.

If you simply tried to travel through the rooms one at a time then as you entered room 3 you would have used all three cards and would be trapped in that room forever!

However, if you make use of the storage boxes, then escape is possible. For example, you could enter room 1 using your first card, place one card in the storage box, and use your third card to exit the room back to the start. Then after collecting three more cards from the dispensing machine you could use one to enter room 1 and collect the card you placed in the box a moment ago. You now have three cards again and will be able to travel through the remaining three doors. This method allows you to travel through all three rooms using six security cards in total.

It is possible to travel through six rooms using a total of $123$ security cards while carrying a maximum of $3$ cards.

Let $C$ be the maximum number of cards which can be carried at any time.

Let $R$ be the number of rooms to travel through.

Let $M(C,R)$ be the minimum number of cards required from the dispensing machine to travel through $R$ rooms carrying up to a maximum of $C$ cards at any time.

For example, $M(3,6)=123$ and $M(4,6)=23$.And, $\sum M(C, 6) = 146$ for $3 \le C \le 4$.

You are given that $\sum M(C,10)=10382$ for $3 \le C \le 10$.

Find $\sum M(C,30)$ for $3 \le C \le 40$.

### This question is a problem from the Project Euler(Problem 321)., which has its roots in recursion. Here is a brief explanation about how to solve it:

1. Before entering room 1, grab 3 cards (`C+1` cards if generalizing) from the machine.

2. Use one card to enter room 1, then place two cards (`C` cards if generalizing) in the box in room 1 before going back to the starting room. For doing this whole operation, you used `C+2` cards. (2 cards used to reach room 1 and go back, `C+1` cards initially grabbed, 2 cards left in the box in room 1).

3. Repeat the above process `R` times. Until you have cards in boxes in all rooms.

4. Finally, grab 3 cards (`C+1` cards if generalizing) from the machine and pass through the rooms using the cards from the boxes.

So, `M(C, R) = R*(C+2) + (C+1)`.

Your question seeks the sum of M(C,30) for C from 3 to 40. By substituting `R = 30` in the equation `M(C, R) = R*(C+2) + (C+1)`, you get `M(C,30) = 32*C + 31`, for `C` ranging from 3 to 40.

You can use the formula for the sum of an arithmetic series to find the answer. The first term a1 = 3, and the last term aN = 40. So, the sum of the series is `N/2 * (a1 + aN)`.

Here, `N = 40 – 3 + 1 = 38`.

So, the sum of `C` from 3 to 40 is `38/2 * (3 + 40) = 819`.

So, the `sum(M(C, 30))` would be the sum of `32*C + 31` from `C=3 to 40`.

Thus, `sum(M(C, 30)) = 32*sum(C)` from `3 to 40` `+ 31*N` (where `N` is the number of terms, i.e. 38).

So, `sum(M(C, 30)) = 32*819 + 31*38 = 26970`.

##### More Answers:

Building a TowerStone Game II

Modulo Summations