题意:

求从不超过 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)的更多相关文章

  1. 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 ...

  2. UvaLive6661 Equal Sum Sets dfs或dp

    UvaLive6661 PDF题目 题意:让你用1~n中k个不同的数组成s,求有多少种组法. 题解: DFS或者DP或打表. 1.DFS 由于数据范围很小,直接dfs每种组法统计个数即可. //#pr ...

  3. UVALive 6661 Equal Sum Sets

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  4. 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 ...

  5. Equal Sum Sets

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=49406 题意: 输入n,k,s,求在不小于n的数中找出k个不同的数 ...

  6. HDU-3280 Equal Sum Partitions

    http://acm.hdu.edu.cn/showproblem.php?pid=3280 用了简单的枚举. Equal Sum Partitions Time Limit: 2000/1000 M ...

  7. POJ 1849 - Two - [DFS][树形DP]

    Time Limit: 1000MS Memory Limit: 30000K Description The city consists of intersections and streets t ...

  8. 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 ...

  9. HDU 3280 Equal Sum Partitions(二分查找)

    Equal Sum Partitions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

随机推荐

  1. 大数据攻城狮之Linux基础------rpm软件管理

    rpm的英文名称为: Redhat package manager 常用的命令加组合: i 安装 rpm -ivh 软件包名 当然我们的rpm也可以支持多包同时操作 rpm -ivh 软件包1 软件包 ...

  2. BPM不同表单之间子表的赋值

    上次写的是同一个表单的子表之间赋值,这次是不同表单之间子表的赋值 首先,我们给需要赋值的表单添加一个复制按钮 $.MvcSheet.AddAction({            Action: &qu ...

  3. 图片加载AsyncTask并发问题

    在列表控件中使用AsycnTask加载图片时,会带来并发问题. 如果每个子视图都触发一个AsyncTask,因为AsyncTask内部是一个线程池,并发触发时,不能确保每个子视图的AsyncTask都 ...

  4. Nginx代码风格图示

    Nginx代码风格图示 (100%) 一.基本原则 K&R编码风格(偏BSD子类). 每行不能超过80列. 不用TAB对齐,用空格. 默认对齐单元是4个空格. 除宏定义外,字母均为小写,单词间 ...

  5. PythonOpencv-分类器—SVM,KNearest,RTrees,Boost,MLP

    原文链接:http://blog.csdn.net/gjy095/article/details/9243153 上一篇文章,不是很详细,这一篇解释的清晰些,请访问原始链接. Rtrees介绍!参考链 ...

  6. 查看linux机器cpu、内存环境信息

    2C2G,4C4G,8C16G,16C32G 这里C指cpu物理核数,G指总内存大小 # 查看物理CPU个数 cat  /proc/cpuinfo| grep "physical id&qu ...

  7. MongoDB_基础知识

    mongoDB术语:database-数据库,collection-数据库表/集合,document-数据记录行/文档,field-数据字段/域,index-索引,primary key-主键(Mon ...

  8. IDEA使用GsonFormat完成JSON和JavaBean之间的转换

    原文地址:https://www.leshalv.net/posts/12625/ 前言: 之前处理接口传来的一堆数据,用jsonObject很难受,后面就用gosn来弄,配合这个工具体验很好. 转: ...

  9. WTM

    WTM的由来 WalkingTec.Mvvm框架(简称WTM)最早开发与2013年,基于Asp.net MVC3 和 最早的Entity Framework, 当初主要是为了解决公司内部开发效率低,代 ...

  10. 商品的CRUD操作

    (1)新增商品:新增文档,建立索引 PUT /index/type/id{ "json数据"} PUT /ecommerce/product/1{ "name" ...