Lintcode20 Dices Sum solution 题解

【题目描述】

Throw n dices, the sum of the dices‘ faces is S. Given n, find the all possible value of S along with its probability.

Notice:You do not care about the accuracy of the result, we will help you to output results.

扔 n 个骰子,向上面的数字之和为 S。给定 Given n,请列出所有可能的 S 值及其相应的概率。

注意:你不用关注答案的准确性,我们会帮你输出答案

【题目链接】

http://www.lintcode.com/en/problem/dices-sum/

【题目解析】

这题用dfs做感觉更加直观,但是过不了time cost。换成dp的方法我是这么想的:

用dp[i][j]表示有i + 1个骰子的情况下,掷到的和为j的次数。那么intialize这个dp[0][j], j = 1...6的值都为1,然后从i = 1开始做循环。i个骰子和i + 1个骰子的差别就是1个骰子(废话),所以再用一个k = 1...6进行遍历,那么i + 1个骰子掷到j + k的次数就是原来dp[i][j + k]的次数加上dp[i - 1][j]。

这样我们就求得了n个骰子的情况下,每个S出现的次数dp[n - 1][j], j = n...6 * n。那么概率就是每个dp[n - 1][j]除以出现的总次数sum(dp[n - 1][j]).

这里要注意dp的值可能很大,所以要用到long long,否则在有些test case(e.g., n = 15)的情况下,会出现负数答案。

【答案链接】

http://www.jiuzhang.com/solution/dices-sum/

时间: 2024-10-16 02:18:14

Lintcode20 Dices Sum solution 题解的相关文章

Lintcode206 Interval Sum solution 题解

[题目描述] Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. Each query has two integers[start, end]. For each query, calculate the sum number between index start and end in the given array, return the re

Lintcode207 Interval Sum II solution 题解

[题目描述] Given an integer array in the construct method, implement two methods query(start, end) and modify(index, value): For query(start,end), return the sum from index start to index end in the given array. For modify(index,value), modify the number

Lintcode17 Subsets solution 题解

[题目描述] Given a set of distinct integers, return all possible subsets. Notice:Elements in a subset must be in non-descending order;The solution set must not contain duplicate subsets. 给定一个含不同整数的集合,返回其所有的子集 注意:子集中的元素排列必须是非降序的,解集必须不包含重复的子集 [题目链接] http:/

Lintcode42 Maximum Subarray II solution 题解

[题目描述] Given an array of integers, find two non-overlapping subarrays which have the largest sum.The number in each subarray should be contiguous.Return the largest sum. Notice:The subarray should contain at least one number 给定一个整数数组,找出两个 不重叠 子数组使得它们

Lintcode18 Subsets II solution 题解

[题目描述] Given a list of numbers that may has duplicate numbers, return all possible subsets Notice:Each element in a subset must be in non-descending order.The ordering between two subsets is free.The solution set must not contain duplicate subsets. 给

Lintcode1 A+B Problem solution 题解

[题目描述] Write a function that add two numbers A and B. You should not use + or any arithmetic operators. Notice:There is no need to read data from standard input stream. Both parameters are given in function aplusb, you job is to calculate the sum and

Lintcode202 Segment Tree Query solution 题解

[题目描述] For an integer array (index from 0 to n-1, where n is the size of this array), in the corresponding Segment Tree, each node stores an extra attribute max to denote the maximum number in the interval of the array (index from start to end). 对于一个

Solution: 题解 CF896C Willem, Chtholly and Seniorious(线段树解珂朵莉树)

Intro: 珂朵莉树模板题 怎么所有题解都是珂朵莉树啊啊啊啊 于是本蒟蒻决定来一发中(feng)规(kuang)中(luan)矩(gao)的线段树 首先这棵线段树只维护懒标记 来一发定义 线段树节点\(u\)维护区间\([l_u,r_u]\)的内容 懒标记\(t_u\):当\(t_u\not=0\)时表示区间\([l_u,r_u]\)全是\(t_u\),\(t_u=0\)就是没有懒标记 建立线段树 在建立时顺便处理\(l_u,r_u\),只要当\(l_u=r_u\)时就打上标记 P.s \(L

Lintcode53 Reverse Words in a String solution 题解

[题目描述] Given an input string, reverse the string word by word. 给定一个字符串,逐个翻转字符串中的每个单词. [题目链接] http://www.lintcode.com/en/problem/reverse-words-in-a-string/ [题目解析] 这道题让我们翻转字符串中的单词,题目中给了我们写特别说明,如果单词之间遇到多个空格,只能返回一个,而且首尾不能有单词,并且对C语言程序员要求空间复杂度为O(1),所以我们只能对