> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Number of Dice Rolls With Target Sum

> Tested Python solution for LeetCode 1155 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 1155, [Medium](/catalog/medium). Topics: [Dynamic Programming](/catalog/topics/dynamic-programming). [View on LeetCode](https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1155   # by problem number
lcpy gen -s number_of_dice_rolls_with_target_sum   # by problem name
```

## Problem

You have `n` dice, and each dice has `k` faces numbered from `1` to `k`.

Given three integers `n`, `k`, and `target`, return *the number of possible ways (out of the* `k^n` *total ways)* *to roll the dice, so the sum of the face-up numbers equals* `target`. Since the answer may be too large, return it **modulo** `10^9 + 7`.

### Examples

```
Input: n = 1, k = 6, target = 3
Output: 1
Explanation: You throw one die with 6 faces.
There is only one way to get a sum of 3.
```

```
Input: n = 2, k = 6, target = 7
Output: 6
Explanation: You throw two dice, each with 6 faces.
There are 6 ways to get a sum of 7: 1+6, 2+5, 3+4, 4+3, 5+2, 6+1.
```

```
Input: n = 30, k = 30, target = 500
Output: 222616187
Explanation: The answer must be returned modulo 10^9 + 7.
```

### Constraints

* `1 <= n, k <= 30`
* `1 <= target <= 1000`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_dice_rolls_with_target_sum/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_dice_rolls_with_target_sum/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n * k * target)
    # Space: O(target)
    def num_rolls_to_target(self, n: int, k: int, target: int) -> int:
        mod = 10**9 + 7
        dp = [1] + [0] * target
        for _ in range(n):
            ndp = [0] * (target + 1)
            for t in range(1, target + 1):
                for f in range(1, min(k, t) + 1):
                    ndp[t] = (ndp[t] + dp[t - f]) % mod
            dp = ndp
        return dp[target]
```

## Complexity

| Time | Space |
| - | - |
| O(n \* k \* target) | O(target) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.