当前位置:网站首页>LeetCode 1155. N ways to roll dice one question per day
LeetCode 1155. N ways to roll dice one question per day
2022-07-07 16:59:00 【@Little safflower】
Problem description
Here you are n The same dice , Every dice has k Face to face , They are labeled as 1 To k .
Given three integers n , k and target , Return to possible ways ( From a total of kn In different ways ) The number of rolling dice , Make the sum of the numbers facing up equal to target .
The answer may be very big , You need to 109 + 7 modulus .
Example 1:
Input :n = 1, k = 6, target = 3
Output :1
explain : You throw one with 6 Face dice .
obtain 3 And there is only one way .
Example 2:Input :n = 2, k = 6, target = 7
Output :6
explain : You throw two dice , Each die has 6 Face to face .
obtain 7 And there are 6 Methods 1+6 2+5 3+4 4+3 5+2 6+1.
Example 3:Input :n = 30, k = 30, target = 500
Output :222616187
explain : The returned result must be right 109 + 7 modulus .
Tips :
1 <= n, k <= 30
1 <= target <= 1000source : Power button (LeetCode)
link :https://leetcode.cn/problems/number-of-dice-rolls-with-target-sum
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Java
class Solution {
public int numRollsToTarget(int n, int k, int target) {
int[][] dp = new int[n + 1][target + 1];
int mod = (int)Math.pow(10,9) + 7;
dp[0][0] = 1;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= target;j++){
for(int q = 1;q <= k;q++){
if(j >= q)
dp[i][j] = (dp[i][j] + dp[i - 1][j - q]) % mod;
}
}
}
return dp[n][target];
}
}边栏推荐
猜你喜欢

谈谈 SAP 系统的权限管控和事务记录功能的实现

Introduction and use of gateway

低代码(lowcode)帮助运输公司增强供应链管理的4种方式

运算符

《产品经理必读:五种经典的创新思维模型》的读后感
3000 words speak through HTTP cache

全网“追杀”钟薛高

The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit

time标准库

Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions
随机推荐
Three. JS series (3): porting shaders in shadertoy
LeetCode 1986. 完成任务的最少工作时间段 每日一题
3000 words speak through HTTP cache
LeetCode 1477. 找两个和为目标值且不重叠的子数组 每日一题
LeetCode 120. 三角形最小路径和 每日一题
正在准备面试,分享面经
Imitate the choice of enterprise wechat conference room
C语言进阶——函数指针
一文读懂数仓中的pg_stat
射线与OBB相交检测
ORACLE进阶(六)ORACLE expdp/impdp详解
ByteDance Android gold, silver and four analysis, Android interview question app
dapp丨defi丨nft丨lp单双币流动性挖矿系统开发详细说明及源码
QT中自定义控件的创建到封装到工具栏过程(一):自定义控件的创建
The latest interview experience of Android manufacturers in 2022, Android view+handler+binder
Pycharm IDE下载
LeetCode 1774. 最接近目标价格的甜点成本 每日一题
AutoLISP series (1): function function 1
[vulnhub range] thales:1
Have fun | latest progress of "spacecraft program" activities