题意: 求从不超过 N 的正整数其中选取 K 个不同的数字,组成和为 S 的方法数. 1 <= N <= 20  1 <= K<= 10  1 <= S <= 155 解题思路: DFS: 因为N,K.S的范围非常小.直接DFS就可以. /* ID: wuqi9395@126.com PROG: LANG: C++ */ #include<map> #include<set> #include<queue> #include<s…
Let us consider sets of positive integers less than or equal to n. Note that all elements of a set are different. Also note that the order of elements doesnt matter, that is, both {3, 5, 9} and {5, 9, 3} mean the same set. Specifying the number of se…
UvaLive6661 PDF题目 题意:让你用1~n中k个不同的数组成s,求有多少种组法. 题解: DFS或者DP或打表. 1.DFS 由于数据范围很小,直接dfs每种组法统计个数即可. //#pragma comment(linker, "/STACK:102400000,102400000") #include<cstdio> #include<cmath> #include<iostream> #include<cstring>…
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <string> #include <vector> #include <map> #include <set> #include <time.h> #include <que…
Equal Sum Sets Let us consider sets of positive integers less than or equal to n. Note that all elements of a set aredifferent. Also note that the order of elements doesnt matter, that is, both {3, 5, 9} and {5, 9, 3} meanthe same set.Specifying the…
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=49406 题意: 输入n,k,s,求在不小于n的数中找出k个不同的数的和等于s的可能性有多少种. 样例: Sample Input9 3 239 3 2210 3 2816 10 10720 8 10220 10 10520 10 1553 4 34 2 110 0 0Sample Output1202015425448100 分析: 用递推的方法把所有的值先求出来…
http://acm.hdu.edu.cn/showproblem.php?pid=3280 用了简单的枚举. Equal Sum Partitions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 453    Accepted Submission(s): 337 Problem Description An equal sum…
Time Limit: 1000MS Memory Limit: 30000K Description The city consists of intersections and streets that connect them. Heavy snow covered the city so the mayor Milan gave to the winter-service a list of streets that have to be cleaned of snow. These s…
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal. Example 1: Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4 Output: True Explanation: It's possible…
Equal Sum Partitions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 551    Accepted Submission(s): 409 Problem Description An equal sum partition of a sequence of numbers is a grouping of the…
216. Combination Sum III 保证subset.size( ) == k && target == 0的时候结束DFS subset.size( ) > k || target < 0返回. class Solution { public List<List<Integer>> combinationSum3(int k, int n) { List<List<Integer>> res = new Array…
P1021 邮票面值设计 题目传送门 题意: 给定一个信封,最多只允许粘贴N张邮票,计算在给定K(N+K≤15N+K≤15)种邮票的情况下 (假定所有的邮票数量都足够),如何设计邮票的面值,能得到最大值MAX, 使在1至MAX之间的每一个邮资值都能得到. 思路: dfs+背包dp 暴搜k种邮票,下一种邮票的取值就是上一张邮票值+1,到上一次选的最大值+1:    比如这上一次你选了1,n=3时,下一次你能选的就是2,3,4: 然后怎么确定当前选择i之后的最大值呢,就是用到背包dp,dp[i]表示…
目录 声明 dfs与dp的关系 经典例题-数字三角形 - POJ 1163 题目 dfs思路 解题思路 具体代码 dp思路 解题思路 具体代码 声明 本文不介绍dfs.dp算法的基础思路,有想了解的可以自己找资源学习. 本文适合于刚刚接触dfs和dp算法的人,发现两种算法间的内在联系. 本人算法之路走之甚短,如果理解出现问题欢迎大家的指正,我会分享基于我目前理解到的算法思想. dfs与dp的关系 很多情况下,dfs和dp两种解题方法的思路都是很相似的,这两种算法在一定程度上是可以互相转化的. 想…
名词解释: DFS(Dynamic Plan):动态规划 DFS(Depth First Search):深度优先搜索 DFS与DP的关系 很多情况下,dfs和dp两种解题方法的思路都是很相似的,这两种算法在一定程度上是可以互相转化的. 想到dfs也就常常会想到dp,当然在一些特定的适用场合下除外. dp主要运用了预处理的思想,而dfs则是类似于白手起家,一步步探索.一般来讲,能够预处理要好些,好比战争前做好准备. dfs和dp都是十分重要的基础算法,在各个竞赛中都有涉及,务必精通. 题目: T…
Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies following conditions: 0 < i, i + 1 < j, j + 1 < k < n - 1 Sum of subarrays (0, i - 1), (i + 1, j - 1), (j + 1, k - 1) and (k + 1, n - 1) should be…
Codeforce 685 B. Kay and Snowflake 解析(思維.DFS.DP.重心) 今天我們來看看CF685B 題目連結 題目 給你一棵樹,要求你求出每棵子樹的重心. 前言 完全不知道我怎麼想到的 @copyright petjelinux 版權所有 觀看更多正版原始文章請至petjelinux的blog 想法 首先會感覺是樹狀DP,並且狀態是每個子樹的重心. 在建重心剖分樹的時候,我們找根為\(v\)的樹的重心的方法是看看目前節點有沒有大小大於目前樹的一半(\(size[v…
题意:给一个元素周期表的元素符号(114种),再给一个串,问这个串能否有这些元素符号组成(全为小写). 解法1:动态规划 定义:dp[i]表示到 i 这个字符为止,能否有元素周期表里的符号构成. 则有转移方程:dp[i] = (dp[i-1]&&f(i-1,1)) || (dp[i-2]&&f(i-2,2))     f(i,k):表示从i开始填入k个字符,这k个字符在不在元素周期表中.  dp[0] = 1 代码: //109ms 0KB #include <ios…
Let us consider sets of positive integers less than or equal to n. Note that all elements of a set are different. Also note that the order of elements doesnt matter, that is, both {3, 5, 9} and {5, 9, 3} mean the same set.        Specifying the numbe…
Let us consider sets of positive integers less than or equal to n. Note that all elements of a set are different. Also note that the order of elements doesnt matter, that is, both {3, 5, 9} and {5, 9, 3} mean the same set.        Specifying the numbe…
题目链接: POJ:id=3132">http://poj.org/problem?id=3132 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemCode=2822 Description A positive integer may be expressed as a sum of different prime numbers (primes), in one way or another. Given two…
Starship Troopers Problem Description You, the leader of Starship Troopers, are sent to destroy a base of the bugs. The base is built underground. It is actually a huge cavern, which consists of many rooms connected with tunnels. Each room is occupie…
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into knon-empty subsets whose sums are all equal. Example 1: Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4 Output: True Explanation: It's possible…
Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies following conditions: 0 < i, i + 1 < j, j + 1 < k < n - 1 Sum of subarrays (0, i - 1), (i + 1, j - 1), (j + 1, k - 1) and (k + 1, n - 1) should be…
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal. Example 1: Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4 Output: True Explanation: It's possible…
[抄题]: Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into knon-empty subsets whose sums are all equal. Example 1: Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4 Output: True Explanation: It's pos…
I Want That Cake 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4925 Description There was an interesting game played on a popular Korean reality TV Show. 12 players in 3 teams - 4…
A. Alyona and Numbers time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output After finishing eating her bun, Alyona came up with two integers n and m. She decided to write down two columns of inte…
原题链接在这里:https://leetcode.com/problems/partition-to-k-equal-sum-subsets/description/ 题目: Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal. Examp…
Problem Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal. Example 1:Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4Output: TrueExplanation: It's pos…
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal. Example 1: Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4 Output: True Explanation: It's possible…