<p>You have <code>n</code> dice, and each dice has <code>k</code> faces numbered from <code>1</code> to <code>k</code>.</p>

<p>Given three integers <code>n</code>, <code>k</code>, and <code>target</code>, return <em>the number of possible ways (out of the </em><code>k<sup>n</sup></code><em> total ways) </em><em>to roll the dice, so the sum of the face-up numbers equals </em><code>target</code>. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

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

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 2, k = 6, target = 7
<strong>Output:</strong> 6
<strong>Explanation:</strong> 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.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> n = 30, k = 30, target = 500
<strong>Output:</strong> 222616187
<strong>Explanation:</strong> The answer must be returned modulo 10<sup>9</sup> + 7.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n, k &lt;= 30</code></li>
	<li><code>1 &lt;= target &lt;= 1000</code></li>
</ul>

class Solution {
public:
long long int MOD= 1000000007;
    int usingrec(int &n,int &k, int &target, int sum,int i)
    {
       
           if(i>n )
           { 
            if(sum==target)
            {
            return 1;
            }
            return 0;
           }
           if(i<=n && sum==target)
           {
            return 0;
           }
           if(sum>target)
           {
            return 0;
           }
           int ans=0;
           for(int face=1;face<=k;face++)
           {
            sum+=face;
            ans+=usingrec(n,k,target,sum,i+1);
           }
           return ans;
           
    }

    int usingmem(int &n, int &k, int &target, int sum, int i, vector<vector<int>>& dp) {
    // Base case: if we've used all dice
    if (i > n) {
        return sum == target ? 1 : 0;
    }

    // If sum exceeds target, no need to proceed
    if (sum > target) {
        return 0;
    }

    // Check if the result is already memoized
    if (dp[i][sum] != -1) {
        return dp[i][sum];
    }

    int ans = 0;

    // Try each face of the dice
    for (int face = 1; face <= k; face++) {
        if (sum + face <= target) {
            ans = (ans + usingmem(n, k, target, sum + face, i + 1, dp)) % MOD;
        }
    }

    // Store result in dp table
    dp[i][sum] = ans;
    return dp[i][sum];
}
int usingtab(int n, int k, int target)
{
      vector<vector<int>>dp(n+3,vector<int>(target+2,0));

      dp[n+1][target]=1;

      for(int i=n;i>=1;i--)
      {
        for(int sum=target;sum>=0;sum--)
        {
           

             int ans = 0;

    // Try each face of the dice
    for (int face = 1; face <= k; face++) {
        if (sum + face <= target) {
            ans = (ans + dp[i+1][sum+face]) % MOD;
        }
    }

    // Store result in dp table
    dp[i][sum] = ans;
        }
      }
      return dp[1][0];

}
int usingtabso(int n, int k, int target)
{
      //vector<vector<int>>dp(n+3,vector<int>(target+2,0));
      vector<int>next(target+1,0);
      vector<int>curr(target+1,0);

      next[target]=1;

      for(int i=n;i>=1;i--)
      {
        for(int sum=target;sum>=0;sum--)
        {
           

             int ans = 0;

    // Try each face of the dice
    for (int face = 1; face <= k; face++) {
        if (sum + face <= target) {
            ans = (ans + next[sum+face]) % MOD;
        }
    }

    // Store result in dp table
    curr[sum] = ans;
        }
        next=curr;
      }
      return next[0];

}
    int numRollsToTarget(int n, int k, int target) {
        
        int i=1;
        int sum=0;
        vector<vector<int>>dp(n+2,vector<int>(target+2,-1));
        int ans=usingtabso(n,k,target);
        return ans;
    }
};
10320