DP30 求骰子能凑成给定和的组合数 Dice Throw @geeksforgeeks

用户和实体行为分析(UEBA)基础指南及解决方案 用户和实体行为分析(UEBA)或异常检测是一种网络安全技术,它使用机器学习(ML)算法来检测网络中用户、主机和其他实体的异常活动。 阅读详情

Given n dice each with m faces, numbered from 1 to m, find the number of ways to get sum X. X is the summation of values on each face when all the dice are thrown.

The Naive approach is to find all the possible combinations of values from n dice and keep on counting the results that sum to X.

This problem can be efficiently solved using Dynamic Programming (DP).

Let the function to find X from n dice is: Sum(m, n, X)
The function can be represented as:
Sum(m, n, X) = Finding Sum (X - 1) from (n - 1) dice plus 1 from nth dice
               + Finding Sum (X - 2) from (n - 1) dice plus 2 from nth dice
               + Finding Sum (X - 3) from (n - 1) dice plus 3 from nth dice
                  ...................................................
                  ...................................................
                  ...................................................
              + Finding Sum (X - m) from (n - 1) dice plus m from nth dice

So we can recursively write Sum(m, n, x) as following
Sum(m, n, X) = Sum(m, n - 1, X - 1) + 
               Sum(m, n - 1, X - 2) +
               .................... + 
               Sum(m, n - 1, X - m)

Why DP approach?
The above problem exhibits overlapping subproblems. See the below diagram. Also, see this recursive implementation. Let there be 3 dice, each with 6 faces and we need to find the number of ways to get sum 8:

diceThrow2

Sum(6, 3, 8) = Sum(6, 2, 7) + Sum(6, 2, 6) + Sum(6, 2, 5) + 
               Sum(6, 2, 4) + Sum(6, 2, 3) + Sum(6, 2, 2)

To evaluate Sum(6, 3, 8), we need to evaluate Sum(6, 2, 7) which can 
recursively written as following:
Sum(6, 2, 7) = Sum(6, 1, 6) + Sum(6, 1, 5) + Sum(6, 1, 4) + 
               Sum(6, 1, 3) + Sum(6, 1, 2) + Sum(6, 1, 1)

We also need to evaluate Sum(6, 2, 6) which can recursively written
as following:
Sum(6, 2, 6) = Sum(6, 1, 5) + Sum(6, 1, 4) + Sum(6, 1, 3) +
               Sum(6, 1, 2) + Sum(6, 1, 1)
..............................................
..............................................
Sum(6, 2, 2) = Sum(6, 1, 1)

Please take a closer look at the above recursion. The sub-problems in RED are solved first time and sub-problems in BLUE are solved again (exhibit overlapping sub-problems). Hence, storing the results of the solved sub-problems saves time.


For DP:

Time Complexity: O(m * n * x) where m is number of faces, n is number of dice and x is given sum.

We can add following two conditions at the beginning of findWays() to improve performance of program for extreme cases (x is too high or x is too low)

// When x is so high that sum can not go beyond x even when we
// get maximum value in every dice throw.
if (m*n <= x)
     return (m*n == x);
 
// When x is too low
if (n >= x)
     return (n == x);

With above conditions added, time complexity becomes O(1) when x >= m*n or when x <= n.

文中的骰子不一定就是6面的,可以是任意面。


package DP;

public class DiceThrow {

	public static void main(String[] args) {
		System.out.println(findWaysRec(4, 2, 1));
		System.out.println(findWaysRec(2, 2, 3));
		System.out.println(findWaysRec(6, 3, 8));
		System.out.println(findWaysRec(4, 2, 5));
		System.out.println(findWaysRec(4, 3, 5));
		System.out.println(findWaysDP(4, 2, 1));
		System.out.println(findWaysDP(2, 2, 3));
		System.out.println(findWaysDP(6, 3, 8));
		System.out.println(findWaysDP(4, 2, 5));
		System.out.println(findWaysDP(4, 3, 5));
	}

	// 返回能使组合之和为sum的方法数
	// faces:一个dice的最大面值,所有面值为:[1...faces]
	// dices:dices的个数,每个dice都可以选择一个face
	public static int findWaysRec(int faces, int dices, int sum){
		if(sum < 1){		// 总和过小
			return 0;
		}
		if(dices == 1){	// 只有一个dice,判断总和和最大面值哪个大
			return sum<=faces ? 1 : 0;
		}
		
		int ways = 0;
		for(int i=1; i<=faces; i++){
			ways += findWaysRec(faces, dices-1, sum-i);
		}
		return ways;
	}
	
	// The main function that returns number of ways to get sum 
	// Time Complexity: O(m * n * x) where m is number of faces, n is number of dice and x is given sum.
	public static int findWaysDP(int faces, int dices, int sum){
		int[][] dp = new int[dices+1][sum+1];	//dp[dices][sum]
		
		for(int sum_=1; sum_<=sum; sum_++){	// 只有1个dice且sum大于
			if(sum_ <= faces){
				dp[1][sum_] = 1;
			}
		}
		
		for(int dices_=2; dices_<=dices; dices_++){		// dices
			for(int sum_=1; sum_<=sum; sum_++){			// sum
				for(int faces_=1; faces_<=faces; faces_++){
					if(sum_-faces_ >= 0){		// 防越界
						dp[dices_][sum_] += dp[dices_-1][sum_-faces_];
					}
				}
			}
		}
		
		return dp[dices][sum];
	}
}


http://www.geeksforgeeks.org/dice-throw-problem/


Flutter 直接调用so动态库,或调用C/C++源文件内函数 我把 .so 文件尝试放过好几个目录/lib/,最终都会报这个错误,现在才知道库文件需要放在你根本想象不到的地方!!!尝试了一些方法还是不行后我放弃了 ffi,想着用 Flutter 的 MethodChannel 桥接 android/ios 原生,再让原生去调 native 层。一顿操作把Android端的搞定了,当然中间涉及到 Kotlin和C++ 层的数据类型映射的痛苦,而且业务函数也不像一个 helloworld 那么简单,别提有多痛苦了。 阅读详情

相关推荐

通达信源码-金牛暴起

趋势1:=3*SMA((CLOSE-LLV(LOW,27))/(HHV(HIGH,27)-LLV(LOW,27))*100,5,1)-2*SMA(SMA((CLOSE-LLV(LOW,27))/(HHV(HIGH,27)-LLV(LOW,27))*100,5,1),3,1) AND SJTJ,DOTLINE,COLORGRAY;谷:=(((DD-LLV(DD,21))/(HHV(DD,21)-LLV(DD,21)))*(4))*(25);{0,25指高度};

qq_57222058的博客 3521

Dices Sum(骰子求和)

完成题目和写完报告共花费2h12分钟。 题目链接:https://www.lintcode.com/problem/dices-sum/description 题目:扔 n 个骰子,向上面的数字之和为 S。给定 Given n,请列出所有可能的 S 值及其相应的概率。 给定 n = 1,返回 [ [1, 0.17], [2, 0.17], [3, 0.17], [4, 0.17], [5, ...

HELL的博客 934

STM32双ADC同步采样实战:FFT测量相位差的5个关键细节

本文深入探讨了利用STM32双ADC同步采样结合FFT算法精确测量信号相位差的实战技巧。文章聚焦五个关键细节:确保ADC时钟同源、优化DMA缓冲区设计、信号调理与窗函数选择、精确的相位提取与解缠绕,以及系统误差校准与验证。这些细节是提升相位差测量精度、确保系统稳定性的核心,尤其强调了STM32配置中易被忽视的时钟同步问题。

oauth7security的博客 594

C - Dice Sum

原题链接 Time Limit: 2 sec / Memory Limit: 1024 MB Score :300points Problem Statement How many integer sequences of lengthN,A=(A1​,…,AN​), satisfy all of the conditions below? 1≤Ai​≤M(1≤i≤N) ∑​Ai​≤K Since the count can get enormous, find it ...

听取WA声一片 592

自己的搭建的slam机器人建图导航测试

1、前言 这里是对自己搭建的slam机器人建图导航的功能测试,原本并不想写这一篇的,因为每个小伙伴的需求都不一样,但是发现还是有同学有需求,于是就有了这一篇。本篇属于说明分享类文章,不涉及细节解释。按照步骤操作即可。 本次测试需要建图算法my_slam_gmapping、导航框架navigation、以及explore_lite功能包、mbot_teleop键盘控制功能包等。 环境:ubuntu mate 16.04 + ROS Kinetic 功能包下载:在公众号:小白学移动机器人,发送:建图导航测试,即

「小白学移动机器人」一个专注分享移动机器人相关知识的公众号! 8700

星载SAR的各项指标解读(史上最全)

一、系统指标 系统指标主要指雷达系统的通用的指标,以及和卫星整星相关的指标,主要有一下几项: 工作频段: 雷达体制: 极化方式: 设计s

9271

编程算法 - n个骰子的点数(递归) 代码(C)

n个骰子的点数(递归) 代码(C)本文地址:http://blog.youkuaiyun.com/caroline_wendy题目: 把n个骰子仍在地上, 所有骰子朝上一面的点数之和为s. 输入n, 打印出s的所有可能的值出现的概率.采用递归的方法, 可以假设只有一个骰子, 然后骰子数递增相加.代码:/* * main.cpp * * Created on: 2014.7.12 * Au

AGI 3722

【YOLOv11工业级实战】15. YOLOv11海洋监测实战:珊瑚礁健康评估(水下图像增强|机器人搭载)

摘要:本文针对珊瑚礁健康评估的海洋监测需求,基于YOLOv11提出解决方案,聚焦水下色偏、悬浮物干扰、珊瑚形态复杂三大难题。引用全球珊瑚礁监测网络(GCRMN)标准,采用CoralNet数据集,通过Jaffe-McGlamery物理模型引导增强与悬浮物去除算法,在浑浊水域(能见度<5m)实现87.3%的健康珊瑚识别率,藻类误判率低至7.2%。介绍了以BlueROV2水下机器人和Jetson AGX Orin为核心的部署方案,结合水下SLAM定位技术,实现珊瑚健康的实时评估与三维覆盖率计算。

专注于人工智能、软件开发、工控自动化、工厂数字化及智能化等领域,希望和大家共同进步! 1644

AtCoder Beginner Contest 248 C - Dice Sum 「二维费用背包的计数问题」

C - Dice Sum 题目描述: 问存在多少种序列A,满足如下的条件: 长度为n A[i] <= m ∑i=1nA[i]<=K\sum_{i=1}^{n}{A[i]}<=K∑i=1n​A[i]<=K 思路: 我们可以把他看成一个二维费用的背包问题 每个物品的体积是1,重量是i,背包的总体积是n,承载重量是K 问的是方案数 dp[j][k]表示当前体积是j,用了k的重量的方案数 状态转移方程: dp[j][k]+=dp[j−1][k−i]dp[j][k] += dp[j

Suryxin's blogs 665

SQL的基本介绍

1.什么是数据库?2.什么是数据库管理系统?3.什么是 SQL?1.数据库是保存有组织的数据的容器,通常是一个文件或一组文件,可以将其看作电子化的文件柜。用户可以对文件中的数据执行新增、删除、更新、查询等操作。2.数据库管理系统是一种操纵和管理数据库的大型软件,用于建立、使用和维护数据库,简称DBMS。它对数据库进行统一的管理和控制,以保证数据库的安全性和完整性。用户可以通过 DBMS 访问数据库中的数据,数据库管理员也可以通过 DBMS 进行数据库的维护工作。

shiwei0813的博客 2879

基于YOLOv8的城市违建识别系统:从数据集构建到UI界面实现

YOLOv8是Ultralytics公司于2023年推出的最新一代YOLO(You Only Look Once)系列目标检测算法。更高效的骨干网络设计改进的锚框机制更精确的损失函数支持分类、检测、分割等多任务YOLOv8特别适合像违建识别这样的实时检测任务,能够在保持高精度的同时实现快速推理。

YOLO 674

hihoCoder太阁最新面经算法竞赛8 题目2 : Dice Possibility (概率DP)

题目2 : Dice Possibility 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 What is possibility of rolling N dice and the sum of the numbers equals to M? 输入 Two integers N and M. (1 ≤ N

LzyRapX 1303

FPGA 学习笔记:Vitis IDE launch failed 的解决方法

FPGA 学习笔记:Vitis IDE launch failed 的解决方法

坚韧与专注 1万+

AtCoder Beginner Contest 248C Dice Sum (生成函数)

AtCoder Beginner Contest 248C Dice Sum 题目链接 题意 给定三个整数N,M,KN,M,KN,M,K,求有多少种满足以下条件的序列AAA: AAA长度为NNN; 1≤Ai≤M(1≤i≤N)1\le A_i\le M(1\le i\le N)1≤Ai​≤M(1≤i≤N) ∑i=1NAi≤K\sum_{i=1}^N A_i\le K∑i=1N​Ai​≤K 最后的结果对998244353取模。 思路 对于每一个AiA_iAi​,可以用f(x)=0+x+x2+...+xM=x

Moyan的博客 528

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

HDU 4586 Play the Dice(概率+期望)

HDU 4586 Play the Dice(概率+期望)

1509

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

n 个骰子点数和及各自出现的概率

题目:把n个骰子扔在地上,所有骰子朝上一面的点数之和为S。输入n,打印出S的所有可能的值出现的概率。这道算法题可采取动态规划法来求解。鉴于《剑指Offer》中对该题的解法晦涩难懂,尤其是代码,也没有指明其解题的思路本质上就是动态规划,所以提出自己的理解和答案。动态规划法简介: 动态规划法求解的总体过程就是将问题分为多个不同的阶段的问题,根据最开始阶段已知的问题的解逐步推导出最终解。即动态规划算法通

Doublelv 的博客专栏。 1万+

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

hdu 4586 Play the Dice(概率dp)

ProblemDescription Thereisadicewithnsides,whicharenumberedfrom1,2,...,nandhavetheequalpossibilitytoshowupwhenonerollsadice.Eachsidehasanintegeraionit.Nowherei...

dielucuan8830的博客 163

Codeforces466C Number of Ways

Codeforces466C Number of Ways(二分+构造)

追梦赤子心 1036

LintCode : Dices Sum 骰子求和

试题: Throw n dices, the sum of the dices’ faces is S. Given n, find the all possible value of S along with its probability. Example Example 1: Input: n = 1 Output: [[1, 0.17], [2, 0.17], [3, 0.17], [4,...

心之所向 329

Play the Dice hdu 4586 数学期望的问题

Play the Dice Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 710    Accepted Submission(s): 253 Special Judge Problem Description T

风华绝代 1233

Dice系数公式与代码(keras)

Dice coefficient

猫头军师兵书 9788

判断是否字符串重组 Scramble String @LeetCode

思路: 1 递归: 简单的说,就是s1和s2是scramble的话,那么必然存在一个在s1上的长度l1,将s1分成s11和s12两段,同样有s21和s22。 那么要么s11和s21是scramble的并且s12和s22是scramble的; 要么s11和s22是scramble的并且s12和s21是scramble的。 如果要能通过OJ,还必须把字符串排序后进行剪枝 http://

1万+

深入理解递归

大家都知道,递归的本质和栈数据的存取很相似了,都是先进去,但是往往最后处理!再者对于递归函数的局部变量的存储是按照栈的方式去存的,对于每一层的递归函数在栈中都保存了本层函数的局部变量,一边该层递归函数结束时能够保存原来该层的数据!如图:               如上图递归式依次往下进行的,并且在该层递归函数还没结束即将进入下一层递归调用时,将会把该层函数中的局部变量保存起来,以供下次使用!

7758

Edit Distance 编辑距离算法 @LeetCode

又是一道典型DP! 我们将人生划为诡异的阶段 我们把这个世界表为丰富的状态 package DP; import java.util.Arrays; /** * Edit Distance * * Given two words word1 and word2, find the minimum number of steps required to conve

5661

总结帖:全排列Permutation,子集subset 递归模板

两个经典递归模板,以前写过,现在再过一遍! 基本思路: 如果题目给的输入时数组,首先先要把数组转为ArrayList,因为ArrayList可以很方便地插入,删除,添加! 其次,递归函数的形式都一样,一共有3个参数,分别叫ArrayList done, ArrayList rest, ArrayList> ret 。 done存放已经处理过的数据,rest存放还没处理的数据,ret存

3714

DP22 堆盒子问题 Box Stacking Problem @geeksforgeeks

You are given a set of n types of rectangular 3-D boxes, where the i^th box has height h(i), width w(i) and depth d(i) (all real numbers). You want to create a stack of boxes which is as tall as possi

3480
上一篇: DP29 最长相同子串 Longest Common Substring @geeksforgeeks
下一篇: DP31 取大小游戏中的最优策略(附:如何对DP[0][n]进行对角线递推) Optimal Strategy for a Game @geeksforgeeks
chiiis
博客等级 码龄14年 422粉丝 429原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值