Retour aux défis
Moyen 0 résolus 0 % de réussite

Count Dice Sequences

Count ordered die-roll sequences with a given total.

Ce que vous allez apprendre

  • Build answers from smaller totals
  • Write a recurrence over the last die roll
  • Take results modulo a large prime

Problème

Afficher la traduction

A standard die produces an integer from 1 through 6. Count all nonempty ordered sequences of outcomes whose sum is target. Different orders count separately. Return the count modulo 1,000,000,007.

Implement dice_combinations(target).

Input

target: The required sum of all rolls.

Output

Return the number of sequences modulo 1,000,000,007.

Contraintes

1 ≤ target ≤ 1,000,000. Taking a count modulo 1,000,000,007 means returning its remainder after division by that number.

Exemples

Exemple 1
Entrée :
target = 2

Sortie attendue :
expected = 2
Entrée :
long long target = 2;

Sortie attendue :
long long expected = 2;
Entrée :
long target = 2;

Sortie attendue :
long expected = 2;

Explication

The two sequences are [2] and [1, 1].

Exemple 2
Entrée :
target = 4

Sortie attendue :
expected = 8
Entrée :
long long target = 4;

Sortie attendue :
long long expected = 8;
Entrée :
long target = 4;

Sortie attendue :
long expected = 8;

Explication

The sequences are [4], [1,3], [3,1], [2,2], [1,1,2], [1,2,1], [2,1,1], and [1,1,1,1].

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

Think of smaller sums as building blocks for the target.

Indice 2

Use a running count for each sum based on the previous six sums.

Indice 3

Be careful to apply the modulo at every addition and handle sums below 1 separately.

Complexité attendue

O(target) time, O(target) extra space

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.