题意:

求从不超过 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. c.vim的安装和使用

    之前写了一些废话,今天写的稍微具体点. 在mac上给vim装个插件,,倒腾了半天 先要去找到c.vim.zip,自己百度下,官网就有的,我mac下下来,是直接解压的格式,而不是*.zip,也就是说不需 ...

  2. TYVJ 1427 线段树的基本操作

    题意: 单点修改,区间最值 思路: 线段树 原题请戳这里 //By SiriusRen #include <cstdio> #include <cstring> #includ ...

  3. LeetCode Weekly Contest 27

    1. 557. Reverse Words in a String III 分割字符串,翻转. class Solution { public: string reverseWords(string ...

  4. 使用Micrisoft.net设计方案 第二章组织模式

    第二章组织模式 模式不仅依赖于它所包含的更小模式,同时也依赖包含它的更大的模式.它是描述复杂软件的系统方法. 本章的目标是让我们了解以下问题: 1.如何标识模式与模式的关系 2.如何把模式组织成模式集 ...

  5. Android横屏时软键盘全屏问题

    1.使用 SearchView xml加入 android:imeOptions="actionDone|flagNoExtractUi" 可以限制软键盘禁止全屏 <andr ...

  6. 【Oracle】ORA-38171: Insufficient privileges for SQL management object operation

    问题: 使用SQL PLAN MANAGEMENT的时候运行下面的存储过程报错. SYS@GOOD> conn scott/tiger Connected. SCOTT@GOOD> DEC ...

  7. Python框架、库和软件资源大全(整理篇)

    有少量修改,请访问原始链接.PythonWIn的exe安装包;http://www.lfd.uci.edu/~gohlke/pythonlibs/ 原文链接:codecloud.net/python- ...

  8. MongoDB经典入门(1)--安装

    一.前言 最近开始学习非关系型数据库MongoDB,却在博客园上找不到比较系统的教程,很多资料都要去查阅英文网站,效率比较低下.本人不才,借着自 学的机会把心得体会都记录下来,方便感兴趣的童鞋分享讨论 ...

  9. 路飞学城Python-Day77

    11-DIY一个web框架3 web框架 yuan功能总结 main.py: 启动文件,封装了socket 1 urls.py: 路径与视图函数映射关系 ---- url控制器 2 views.py ...

  10. leetCode 929 独特的电子邮件地址

    题目: 每封电子邮件都由一个本地名称和一个域名组成,以 @ 符号分隔. 例如,在 alice@leetcode.com中, alice 是本地名称,而 leetcode.com 是域名. 除了小写字母 ...