問題
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.
例
入力:
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].
入力:
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 つずつ開いてみましょう。
ヒント 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用に調整されています。上記の説明と制約に従ってください。