[UVALive 6661 Equal Sum Sets] (dfs 或 dp)
题意:
求从不超过 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<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<fstream>
#include<cstring>
#include<ctype.h>
#include<iostream>
#include<algorithm>
#define INF (1<<30)
#define PI acos(-1.0)
#define mem(a, b) memset(a, b, sizeof(a))
#define For(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;
int n, k, s;
int cnt = 0;
void dfs(int sum, int x, int depth) {
if (sum > s) return ;
if (k - 1 == depth) {
if (sum == s) cnt++;
return ;
}
for (int i = x + 1; i <= min(n, s - sum); i++) dfs(sum + i, i, depth + 1);
}
int main () {
while(scanf("%d%d%d", &n, &k, &s)) {
if (n + k + s == 0) break;
cnt = 0;
for (int i = 1; i <= n; i++) {
dfs(i, i, 0);
}
printf("%d\n", cnt);
}
}
DP:
dp[i][j][k] = dp[i - 1][j][k] + dp[i - 1][j - i][k - 1] //dp[i][j][k]表示从不超过i的数字中选取k个数和为j的方法数。
/*
ID: wuqi9395@126.com
PROG:
LANG: C++
*/
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<fstream>
#include<cstring>
#include<ctype.h>
#include<iostream>
#include<algorithm>
#define INF (1<<30)
#define PI acos(-1.0)
#define mem(a, b) memset(a, b, sizeof(a))
#define For(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;
int dp[22][160][11];
int n, k, s;
int main () {
dp[0][0][0] = 1;
for (int i = 1; i <= 20; i++) {
for (int j = 0; j <= 155; j++) {
for (int k = 0; k <= 10; k++) {
dp[i][j][k] = dp[i - 1][j][k];
if (k > 0 && j >= i) dp[i][j][k] += dp[i - 1][j - i][k - 1];
}
}
}
while(scanf("%d%d%d", &n, &k, &s), n || k || s) printf("%d\n", dp[n][s][k]);
}
[UVALive 6661 Equal Sum Sets] (dfs 或 dp)的更多相关文章
- UvaLive 6661 Equal Sum Sets (DFS)
Let us consider sets of positive integers less than or equal to n. Note that all elements of a set a ...
- UvaLive6661 Equal Sum Sets dfs或dp
UvaLive6661 PDF题目 题意:让你用1~n中k个不同的数组成s,求有多少种组法. 题解: DFS或者DP或打表. 1.DFS 由于数据范围很小,直接dfs每种组法统计个数即可. //#pr ...
- UVALive 6661 Equal Sum Sets
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- D.6661 - Equal Sum Sets
Equal Sum Sets Let us consider sets of positive integers less than or equal to n. Note that all elem ...
- Equal Sum Sets
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=49406 题意: 输入n,k,s,求在不小于n的数中找出k个不同的数 ...
- HDU-3280 Equal Sum Partitions
http://acm.hdu.edu.cn/showproblem.php?pid=3280 用了简单的枚举. Equal Sum Partitions Time Limit: 2000/1000 M ...
- POJ 1849 - Two - [DFS][树形DP]
Time Limit: 1000MS Memory Limit: 30000K Description The city consists of intersections and streets t ...
- 698. Partition to K Equal Sum Subsets
Given an array of integers nums and a positive integer k, find whether it's possible to divide this ...
- HDU 3280 Equal Sum Partitions(二分查找)
Equal Sum Partitions Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
随机推荐
- [源码管理] ubuntu中svn简明用法:服务器搭建+客户端使用
本文是对网络上前人的优秀文章加以实践验证后所整理(修正或补充) 第一部分:svn服务器搭建(主要是四步走) 参考:http://www.son1c.cn/show/920.html 一,安装Subve ...
- [牛客挑战赛 30D] 小A的昆特牌 解题报告 (组合数学)
interlinkage: https://ac.nowcoder.com/acm/contest/375/D description: solution: 我们枚举步兵的数量$x$,还剩下$S-x$ ...
- 46.Qt 使用OpenGL绘制立方体
main.cpp #include <QApplication> #include <iostream> #include "vowelcube.h" in ...
- java中的数组的Arrays工具类的使用
package day04.d1.shuzu; import java.util.Arrays; /** * Arrays 工具类 * @author Administrator * */public ...
- Elasticsearch之curl创建索引库和索引时注意事项
前提, Elasticsearch之curl创建索引库 Elasticsearch之curl创建索引 注意事项 1.索引库名称必须要全部小写,不能以下划线开头,也不能包含逗号 2.如果没有明确指定索引 ...
- html中map标签和area标签的应用
map标签的用途:是与img标签绑定使用的,常被用来赋予给客户端图像某处区域特殊的含义,点击该区域可跳转到新的文档. 因为map标签是与img标签绑定使用的,所以我们需要给map标签添加ID和name ...
- (转)redux
随着 JavaScript 单页应用开发日趋复杂,越来越多的 state (状态)需要在前端进行管理. 这些 state 可能包括服务器响应.缓存数据.本地生成尚未持久化到服务器的数据,也包括 UI ...
- Findbugs分析工具介绍
Findbugs是一个静态分析工具,它检查类或者JAR 文件,将字节码与一组缺陷模式进行对比以发现可能的问题.Findbugs自带检测器,其中有60余种Bad practice,80余种Correct ...
- SqlServer与MySql语法比较
1.复制表(包括表结构.表数据) SqlServer: Select * into user_copy from user MySql: CREATE TABLE user_copy LIKE use ...
- VFS(Virtual File System)
一.VFS作为内核子系统,为用户空间程序提供了文件系统相关的接口.所有实际文件系统依赖VFS共存,依靠VFS系统工作. 1.VFS提供通用文件系统接口:用户空间程序可以利用标准的UNIX文件系统调用, ...