Trung bình 0 đã giải Tỷ lệ chấp nhận 0%

Count Dice Sequences

Count ordered die-roll sequences with a given total.

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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.

Ràng buộc

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

Ví dụ

Ví dụ 1
Đầu vào:
target = 2

Kết quả mong đợi:
expected = 2
Đầu vào:
long long target = 2;

Kết quả mong đợi:
long long expected = 2;
Đầu vào:
long target = 2;

Kết quả mong đợi:
long expected = 2;

Giải thích

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

Ví dụ 2
Đầu vào:
target = 4

Kết quả mong đợi:
expected = 8
Đầu vào:
long long target = 4;

Kết quả mong đợi:
long long expected = 8;
Đầu vào:
long target = 4;

Kết quả mong đợi:
long expected = 8;

Giải thích

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

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

Think of smaller sums as building blocks for the target.

Gợi ý 2

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

Gợi ý 3

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

Độ phức tạp mong đợi

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.