[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 ...
随机推荐
- LINUX/UNIX找回删除的文件
当Linux计算机受到入侵时,常见的情况是日志文件被删除,以掩盖攻击者的踪迹.管理错误也可能导致意外删除重要的文件,比如在清理旧日志时,意外地删除了数据库的活动事务日志.有时可以通过lsof来恢复这些 ...
- thrift RPC 框架的自我搭建
安装thrift rpc 安装的系统是Centos 7 未成功的方法 :(原因没找到,但是还是要记录下) 安装依赖库 yum install automake libtool flex bison ...
- flex和layout移动端布局
1.九宫格 样式为: ul{ display: flex; flex-wrap: wrap;//超出换行 } li{ width: 33%; height: 60px; display: flex; ...
- ORACLE 11g 生产中高水位线(HWM)处理
数据库中表不断的insert,delete,update,导致表和索引出现碎片.这会导致HWM之前有很多的空闲空间,而oracle在做全表扫描的时候会读取HWM一下的所有块,这样会产生更多的IO,影响 ...
- css要点
1.对inline-block设置overflow: hidden会造成baseline移动,因此需要设置vertical-align才不会出现样式问题. 2.使用flex时,需要对设置flex: 1 ...
- 如何用PYTHON代码写出音乐
什么是MIDI 博主本人虽然五音不全,而且唱歌还很难听,但是还是非常喜欢听歌的.我一直在做这样的尝试,就是通过人工智能算法实现机器自动的作词和编曲(在这里预告下,通过深度学习写歌词已经实现了,之后会分 ...
- 记一个男默女泪的 BUG
姗姗来迟的词频统计代码 BUG 的发现 1. 此前提交的第一次代码作业总结博客 http://www.cnblogs.com/ustczwq/p/8680704.html 2. BUG 本天成,妙手偶 ...
- PLSQL 11注册码
plsql 11 注册码:Product Code:4t46t6vydkvsxekkvf3fjnpzy5wbuhphqzserial Number:601769 password:xs374ca
- 杭电 1021 Fibonacci Again
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1021 解题思路:根据之前发现斐波那契数列的规律,即为f(n)能被3整除当且仅当n能被4整除. 于是联想 ...
- C# indexof 注意