Leetcode-1155 Number of Dice Rolls With Target Sum(掷骰子的N种方法)

dp[i][j]表示前i个骰子到达数字总和j的方案数

dp[i][j] = Σdp[i-1][j-k],其中k是一个骰子能掷出的范围

 1 #define _for(i,a,b) for(int i = (a);i < b;i ++)
 2
 3 class Solution
 4 {
 5     public:
 6         int dp[31][1003];
 7         int numRollsToTarget(int d, int f, int target)
 8         {
 9             _for(k,1,f+1)
10                 dp[0][k] = 1;
11             _for(i,1,d)
12                 _for(j,1,target+1)
13                     _for(k,1,f+1)
14                     {
15                         if(j-k>0)
16                             dp[i][j] += dp[i-1][j-k];
17                         dp[i][j] %= 1000000007;
18                     }
19             return dp[d-1][target];
20         }
21 };

原文地址:https://www.cnblogs.com/Asurudo/p/11334549.html

时间: 2024-08-06 01:21:55

Leetcode-1155 Number of Dice Rolls With Target Sum(掷骰子的N种方法)的相关文章

LeetCode 1155. Number of Dice Rolls With Target Sum

原题链接在这里:https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/ 题目: You have d dice, and each die has f faces numbered 1, 2, ..., f. Return the number of possible ways (out of fd total ways) modulo 10^9 + 7 to roll the dice so the sum of

[leetcode 周赛 149] 1155 掷骰子的N种方法

目录 1155 Number of Dice Rolls With Target Sum 掷骰子的N种方法 描述 思路 代码实现 1155 Number of Dice Rolls With Target Sum 掷骰子的N种方法 描述 这里有 d 个一样的骰子,每个骰子上都有 f 个面,分别标号为 1, 2, ..., f. 我们约定:掷骰子的得到总点数为各骰子面朝上的数字的总和. 如果需要掷出的总点数为 target,请你计算出有多少种不同的组合情况(所有的组合情况总共有f^d 种),模10

Number类为数字到字符串的类型转换场景定义三种方法

toFixed()根据小数点后的指定位数将数字转换为字符串,它从不使用指数计数法: toExponential()使用指数计数法将数字转换为指数形式的字符串,其中小数点前只有一位,小数点后的位数则由参数指定(也就是说有效数字位数比指定的位数要多一位): toPrecision()根据指定的有效数字位数将数字转换成字符串,如果有效数字的位数少于数字整数部分的位数,则转换成指数形式. 这三种方法都会适当的进行四舍五入或填充0. 例: var n = 123456.789; n.toFixed(0);

[Leetcode] DP -- Target Sum

You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol. Find out how many ways to assign symbols to make sum of integers

[leetcode]Valid Number @ Python

原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较优雅.本文参考了http://blog.csdn.net/kenden23/article/details/18696083里面的内容,在此致谢! 首先这个题有9种状态: 0初始无输入或者只有space的状态1输入了数字之后的状态2前面无数字,只输入了dot的状态3输入了符号状态4前面有数字和有do

leetCode: Single Number II [137]

[题目] Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? [题意] 给定一个整数以外,其中除了一个整数只出现一次以外,其他

[leetcode]_Palindrome Number

判断integer是否为回文串(负数全部不为回文串) 思路很直接,提取出integer中的每一位,一头一尾进行比较是否相同. 一次AC , 直接上代码: public boolean isPalindrome(int x) { if(x < 0) return false; else if(x >= 0 && x <= 9) return true; else{ int[] num = new int[20]; int i = 0 ; while(x > 0){ n

LeetCode: Single Number Ⅱ

1 /** 2 * 3 */ 4 package solution; 5 6 import java.util.Arrays; 7 8 /** 9 * @author whh 10 * 11 * Given an array of integers, every element appears three times except 12 * for one. Find that single one. 13 * 14 * Note: Your algorithm should have a li

[LeetCode] [Palindrome Number 2012-01-04]

Determine whether an integer is a palindrome. Do this without extra space. if use recursive, like check the first dig and last dig, then remove them, check the rest, it will fail when digint like "1021", when remove the first and last one, the r