当前位置:网站首页>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];
}
}
边栏推荐
- LeetCode 1654. 到家的最少跳跃次数 每日一题
- [designmode] flyweight pattern
- 最新Android面试合集,android视频提取音频
- logback. XML configure logs of different levels and set color output
- LeetCode 1774. 最接近目标价格的甜点成本 每日一题
- [PHP] PHP interface inheritance and interface multi inheritance principle and implementation method
- 《产品经理必读:五种经典的创新思维模型》的读后感
- 【医学分割】attention-unet
- LeetCode 1186. 删除一次得到子数组最大和 每日一题
- QT中自定义控件的创建到封装到工具栏过程(二):自定义控件封装到工具栏
猜你喜欢
随机推荐
Personal notes of graphics (3)
Three. JS series (3): porting shaders in shadertoy
如何快速检查钢网开口面积比是否符合 IPC7525
【Seaborn】组合图表:FacetGrid、JointGrid、PairGrid
DNS 系列(一):为什么更新了 DNS 记录不生效?
[Android -- data storage] use SQLite to store data
AutoLISP series (1): function function 1
dapp丨defi丨nft丨lp单双币流动性挖矿系统开发详细说明及源码
字节跳动Android金三银四解析,android面试题app
【图像传感器】相关双采样CDS
LeetCode 1774. 最接近目标价格的甜点成本 每日一题
深度监听 数组深度监听 watch
1亿单身男女“在线相亲”,撑起130亿IPO
【MySql进阶】索引详解(一):索引数据页结构
LeetCode 1049. 最后一块石头的重量 II 每日一题
最新高频Android面试题目分享,带你一起探究Android事件分发机制
3000 words speak through HTTP cache
LeetCode 120. 三角形最小路径和 每日一题
skimage学习(3)——使灰度滤镜适应 RGB 图像、免疫组化染色分离颜色、过滤区域最大值
[vulnhub range] thales:1