当前位置:网站首页>LeetCode 1155. 掷骰子的N种方法 每日一题
LeetCode 1155. 掷骰子的N种方法 每日一题
2022-07-07 15:32:00 【@小红花】
问题描述
这里有 n 个一样的骰子,每个骰子上都有 k 个面,分别标号为 1 到 k 。
给定三个整数 n , k 和 target ,返回可能的方式(从总共 kn 种方式中)滚动骰子的数量,使正面朝上的数字之和等于 target 。
答案可能很大,你需要对 109 + 7 取模 。
示例 1:
输入:n = 1, k = 6, target = 3
输出:1
解释:你扔一个有6张脸的骰子。
得到3的和只有一种方法。
示例 2:输入:n = 2, k = 6, target = 7
输出:6
解释:你扔两个骰子,每个骰子有6个面。
得到7的和有6种方法1+6 2+5 3+4 4+3 5+2 6+1。
示例 3:输入:n = 30, k = 30, target = 500
输出:222616187
解释:返回的结果必须是对 109 + 7 取模。
提示:
1 <= n, k <= 30
1 <= target <= 1000来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/number-of-dice-rolls-with-target-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
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];
}
}
边栏推荐
- null == undefined
- 如何快速检查钢网开口面积比是否符合 IPC7525
- ATM system
- PHP has its own filtering and escape functions
- DAPP defi NFT LP single and dual currency liquidity mining system development details and source code
- Three. JS series (1): API structure diagram-1
- 【PHP】PHP接口继承及接口多继承原理与实现方法
- "The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."
- Horizontal and vertical centering method and compatibility
- Interface oriented programming
猜你喜欢
【C 语言】 题集 of Ⅹ
3000 words speak through HTTP cache
[C language] question set of X
Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions
字节跳动Android金三银四解析,android面试题app
AutoLISP series (2): function function 2
值得一看,面试考点与面试技巧
DNS 系列(一):为什么更新了 DNS 记录不生效?
直接上干货,100%好评
Personal notes of graphics (1)
随机推荐
华东师大团队提出,具有DNA调控电路的卷积神经网络的系统分子实现
最新阿里P7技术体系,妈妈再也不用担心我找工作了
Three. JS series (2): API structure diagram-2
作为Android开发程序员,android高级面试
Lowcode: four ways to help transportation companies enhance supply chain management
JS中null NaN undefined这三个值有什么区别
[C language] question set of X
删除 console 语句引发的惨案
[vulnhub range] thales:1
php 自带过滤和转义函数
Talk about the realization of authority control and transaction record function of SAP system
HAVE FUN | “飞船计划”活动最新进展
SqlServer2014+: 创建表的同时创建索引
URL和URI的关系
IP地址和物理地址有什么区别
谈谈 SAP 系统的权限管控和事务记录功能的实现
Introduction to ThinkPHP URL routing
Balanced binary tree (AVL)
水平垂直居中 方法 和兼容
Statistical learning method -- perceptron