Dice Combinations#9036

Dice Combinations (Editorial)

We solve the problem using dynamic programming. Let be the number of ways to obtain the sum . Consider the last throw: suppose it shows the number (1 \le k \le 6). Then all the previous throws must give the sum n - k, which can be done in f(n - k) ways. Different values of produce different sequences of throws, so summing over all , we get:

f(n) = f(n - 1) + f(n - 2) + f(n - 3) + f(n - 4) + f(n - 5) + f(n - 6)

This formula applies for n > 6, when the arguments of all six terms are positive. The initial values f(1), \ldots, f(6) will be found separately.

Let us find the initial values. The sum n = 1 can be obtained in only one way — with a single throw showing one, so f(1) = 1.

The sum n = 2 can be obtained in two ways: 1 + 1 and , so f(2) = 2.

Consider n = 3. The sum can be obtained as follows:

Thus, f(3) = 1 + f(1) + f(2) = 1 + 1 + 2 = 4.

Similarly, for n \le 6 the sum can be obtained with a single throw, as well as with any last throw k < n preceded by the sum n - k. Therefore, for n \le 6:

f(n) = 1 + f(1) + f(2) + \ldots + f(n - 1)

The original recurrence requires adding six terms at each step. It can be simplified. Let us write it out once again:

f(n) = f(n - 1) + f(n - 2) + f(n - 3) + f(n - 4) + f(n - 5) + f(n - 6)

Write the same recurrence for f(n - 1):

f(n - 1) = f(n - 2) + f(n - 3) + f(n - 4) + f(n - 5) + f(n - 6) + f(n - 7)

From the last equality, express the sum of the five common terms

f(n - 2) + f(n - 3) + f(n - 4) + f(n - 5) + f(n - 6) = f(n - 1) - f(n - 7)

and substitute it into the original recurrence:

f(n) = f(n - 1) + f(n - 1) - f(n - 7) = 2 \cdot f(n - 1) - f(n - 7)

Visually, this means the following: the sums for and f(n - 1) consist of six consecutive values, and the window of the second sum is obtained from the window of the first by shifting it by one position. During the shift, f(n - 7) leaves the sum and f(n - 1) is added.

To make this formula work already for n = 7, set f(0) = 1: then f(7) = 2 \cdot f(6) - f(0) = 2 \cdot 32 - 1 = 63, which matches f(6) + f(5) + \ldots + f(1) = 32 + 16 + 8 + 4 + 2 + 1 = 63. The value f(0) = 1 is natural: the empty sum can be obtained in exactly one way — by not throwing the die at all.

It remains to compute the initial values of the function for n \le 6:

f(1) = 1, \quad f(2) = 2, \quad f(3) = 4,
f(4) = 1 + f(1) + f(2) + f(3) = 1 + 1 + 2 + 4 = 8,
f(5) = 1 + f(1) + f(2) + f(3) + f(4) = 1 + 1 + 2 + 4 + 8 = 16,
f(6) = 1 + f(1) + f(2) + f(3) + f(4) + f(5) = 1 + 1 + 2 + 4 + 8 + 16 = 32.

Thus, f(n) = 2^{n-1} for 1 \le n \le 6. This is also easy to explain directly: for n \le 6 any splitting of into ordered summands can be realized by die throws, and there are exactly 2^{n-1} such splittings — there are n - 1 gaps between ones, and in each gap we either put a separator or not.

As a result, we obtain an equivalent recurrence in which each value is computed in from two previous ones:

\begin{cases} f(n) = 2 \cdot f(n - 1) - f(n - 7), & n > 6 \\ f(n) = 2^{n-1}, & 1 \le n \le 6 \\ f(0) = 1 \end{cases}

Both recurrences allow us to compute in time. All computations are performed modulo 10^9 + 7.

Example

Let us compute the values of for n \le 9. For n \le 6 they are powers of two, and starting from n = 7 they are computed by the recurrence.

The value can be found by the original recurrence as the sum of the six previous values, and — by the simplified one:

f(8) = f(7) + f(6) + f(5) + f(4) + f(3) + f(2) = 63 + 32 + 16 + 8 + 4 + 2 = 125,
f(9) = 2 \cdot f(8) - f(2) = 2 \cdot 125 - 2 = 248.

For the input value n = 3 the answer is f(3) = 4, which matches the ways listed in the statement: 1 + 1 + 1, 1 + 2, 2 + 1 and .

Algorithm Implementation

Declare the constants: is the array size, \mathit{MOD} = 10^9 + 7 is the modulus. The value equals the number of ways to obtain the sum , that is, .

#define MAX 1000001
#define MOD 1000000007
long long dp[MAX];

Set the initial values \mathit{dp}[1], \ldots, \mathit{dp}[6] using the formula f(n) = 1 + f(1) + \ldots + f(n - 1): the one corresponds to obtaining the sum with a single throw. These values do not exceed , so there is no need to take them modulo.

dp[1] = 1;
dp[2] = dp[1] + 1;
dp[3] = dp[1] + dp[2] + 1;
dp[4] = dp[1] + dp[2] + dp[3] + 1;
dp[5] = dp[1] + dp[2] + dp[3] + dp[4] + 1;
dp[6] = dp[1] + dp[2] + dp[3] + dp[4] + dp[5] + 1;

Read the input value .

scanf("%d", &n);

Compute the values \mathit{dp}[7], \ldots, \mathit{dp}[n] by the original recurrence as the sum of the six previous values. Each term is less than , so the sum of six terms fits into the long long type, and it is enough to take the remainder once — after the addition.

for (i = 7; i <= n; i++)
  dp[i] = (dp[i - 1] + dp[i - 2] + dp[i - 3] + dp[i - 4] + dp[i - 5] + dp[i - 6]) % MOD;

Print the answer .

printf("%lld\n", dp[n]);

Algorithm Implementation — simplified recurrence

Declare the constants.

#define MAX 1000001
#define MOD 1000000007

We use the recurrence in which each next value is expressed through only two previous ones. Set \mathit{dp}[0] = 1 (this one accounts for the case "the sum is obtained with a single throw"), and for 1 \le i \le 6 directly set \mathit{dp}[i] = 2^{i-1}, computing the power of two with the shift 1 « (i - 1).

dp[0] = 1;
for (i = 1; i <= 6; i++)
  dp[i] = 1 << (i - 1);

Read and compute the values \mathit{dp}[7], \ldots, \mathit{dp}[n] by the simplified recurrence f(n) = 2 \cdot f(n - 1) - f(n - 7). The values of are stored modulo , so the difference 2 \cdot \mathit{dp}[i - 1] - \mathit{dp}[i - 7] may turn out negative. To keep the remainder non-negative, we add before taking the modulo.

scanf("%d", &n);
for (i = 7; i <= n; i++)
  dp[i] = (2 * dp[i - 1] - dp[i - 7] + MOD) % MOD;

Print the answer.

printf("%lld\n", dp[n]);

Java Implementation

import java.util.*;

class Main
{
  static long dp[] = new long[1000001];
  static long MOD = 1000000007;

  public static void main(String[] args)
  {
    Scanner con = new Scanner(System.in);
    dp[0] = 1;
    for (int i = 1; i <= 6; i++)
      dp[i] = 1 << (i - 1);

    int n = con.nextInt();
    for (int i = 7; i <= n; i++)
      dp[i] = (2 * dp[i - 1] - dp[i - 7] + MOD) % MOD;
    System.out.println(dp[n]);
    con.close();
  }
}