当前位置:网站首页>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];
}
}边栏推荐
- Laravel service provider instance tutorial - create a service provider test instance
- [C language] question set of X
- Detailed explanation of several ideas for implementing timed tasks in PHP
- Statistical learning method -- perceptron
- Binary search tree (basic operation)
- 谎牛计数(春季每日一题 53)
- 华东师大团队提出,具有DNA调控电路的卷积神经网络的系统分子实现
- Balanced binary tree (AVL)
- laravel post提交数据时显示异常
- Temperature sensor chip used in temperature detector
猜你喜欢

Personal notes of graphics (3)

Horizontal and vertical centering method and compatibility

AutoLISP series (3): function function 3

spark调优(三):持久化减少二次查询

AutoLISP series (2): function function 2

Introduction and use of gateway
![[medical segmentation] attention Unet](/img/f4/cf5b8fe543a19a5554897a09b26e68.png)
[medical segmentation] attention Unet

低代码(lowcode)帮助运输公司增强供应链管理的4种方式
![[vulnhub range] thales:1](/img/fb/721d08697afe9b26c94fede628c4d1.png)
[vulnhub range] thales:1

Sort out several important Android knowledge and advanced Android development interview questions
随机推荐
Read PG in data warehouse in one article_ stat
数据中台落地实施之法
typescript ts 基础知识之类型声明
低代码(lowcode)帮助运输公司增强供应链管理的4种方式
Master this promotion path and share interview materials
Find tags in prefab in unity editing mode
Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions
无法将“pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
最新阿里P7技术体系,妈妈再也不用担心我找工作了
Set the route and optimize the URL in thinkphp3.2.3
最新2022年Android大厂面试经验,安卓View+Handler+Binder
掌握这套精编Android高级面试题解析,oppoAndroid面试题
laravel构造函数和中间件执行顺序问题
Cesium (4): the reason why gltf model is very dark after loading
Arduino 控制的双足机器人
正在准备面试,分享面经
spark调优(三):持久化减少二次查询
01tire+链式前向星+dfs+贪心练习题.1
Laravel5.1 Routing - routing packets
掌握这个提升路径,面试资料分享