मध्यम 0 हल किए गए 0% स्वीकृति

Count Dice Sequences

Count ordered die-roll sequences with a given total.

आप क्या सीखेंगे

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

समस्या

अनुवाद दिखाएँ

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.

सीमाएँ

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

उदाहरण

उदाहरण 1
इनपुट:
target = 2

अपेक्षित आउटपुट:
expected = 2
इनपुट:
long long target = 2;

अपेक्षित आउटपुट:
long long expected = 2;
इनपुट:
long target = 2;

अपेक्षित आउटपुट:
long expected = 2;

व्याख्या

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

उदाहरण 2
इनपुट:
target = 4

अपेक्षित आउटपुट:
expected = 8
इनपुट:
long long target = 4;

अपेक्षित आउटपुट:
long long expected = 8;
इनपुट:
long target = 4;

अपेक्षित आउटपुट:
long expected = 8;

व्याख्या

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

कैसे हल करें

पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।

संकेत 1

Think of smaller sums as building blocks for the target.

संकेत 2

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

संकेत 3

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

अपेक्षित जटिलता

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

संदर्भ

यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।