Equal Sum Sets

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 set elements and their sum to be k and s, respectively, sets satisfying the
conditions are limited. When n = 9, k = 3 and s = 23, {6, 8, 9} is the only such set. There may be
more than one such set, in general, however. When n = 9, k = 3 and s = 22, both {5, 8, 9} and {6, 7, 9}
are possible.
You have to write a program that calculates the number of the sets that satisfy the given conditions.
Input
The input consists of multiple datasets. The number of datasets does not exceed 100.
Each of the datasets has three integers n, k and s in one line, separated by a space. You may assume
1 ≤ n ≤ 20, 1 ≤ k ≤ 10 and 1 ≤ s ≤ 155.
The end of the input is indicated by a line containing three zeros.
Output
The output for each dataset should be a line containing a single integer that gives the number of the
sets that satisfy the conditions. No other characters should appear in the output.
You can assume that the number of sets does not exceed 231 − 1.

Sample Input
9 3 23
9 3 22
10 3 28
16 10 107
20 8 102
20 10 105
20 10 155
3 4 3
4 2 11
0 0 0
Sample Output
1
2
0
20
1542
5448
1
0
0

题意:

在1~n 中任意选 k 个数组成 s 。求共有多少种组法。

分析:

枚举。普通枚举会超时,N!次

因为n,k,s的范围很小,可以直接用dfs。

代码:

 #include<cstdio>
#include<iostream>
using namespace std; int n,k,s;
int ans; void dfs(int num,int kase,int sum)
{
if(kase==k&&sum==s)
{
ans++;
return;
}
if(kase>k||sum>s)
return;
else
for(int i=num+;i<=n;i++)
dfs(i,kase+,sum+i);
return;
} int main ()
{
while(scanf("%d%d%d",&n,&k,&s)!=EOF)
{
if(n==&&k==&&s==)
break;
ans=;
for(int i=;i<=n;i++)
dfs(i,,i);
printf("%d\n",ans);
}
return ;
}

这道题还可以用dp 方法来做,但是还没有学。学了之后再重新补上。

D.6661 - Equal Sum Sets的更多相关文章

  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. UVALive 6661 Equal Sum Sets

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

  3. [UVALive 6661 Equal Sum Sets] (dfs 或 dp)

    题意: 求从不超过 N 的正整数其中选取 K 个不同的数字,组成和为 S 的方法数. 1 <= N <= 20  1 <= K<= 10  1 <= S <= 15 ...

  4. UvaLive6661 Equal Sum Sets dfs或dp

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

  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. HDU 3280 Equal Sum Partitions(二分查找)

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

  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. [LeetCode] 548. Split Array with Equal Sum 分割数组成和相同的子数组

    Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies fol ...

随机推荐

  1. [Swust OJ 632]--集合运算(set容器)

    题目链接:http://acm.swust.edu.cn/problem/632/ Time limit(ms): 1000 Memory limit(kb): 65535   Description ...

  2. JBoss 系列六十九:CDI 基本概念

    概述 如果说EJB,JPA是之前JEE(JEE5及JEE5之前)中里程碑式的规范,那么在JEE6,JEE7中CDI可以与之媲美,CDI(Contexts and Dependency Injectio ...

  3. javaScript中有关正则表达式的用法总结

    js中处理正则表达式的类:RegExp 用法: var reg=new RegExp("express"); 或者: var reg=/express/; js中常用的6个方法: ...

  4. js 中的bind函数

    bind是Function.prototype中内置函数 作用是指定函数作用域 代码参考 http://blog.csdn.net/load_life/article/details/7200381 ...

  5. phome_ecms_news 数据表字段解释(新闻系统模型-主表)

    http://www.phome.net/doc/manual/extend/html/dbdoc/index.html 字段名 类型 解释 附加说明 id int(11) 信息ID   classi ...

  6. 转: bower 客户端库管理工具

    概述 常用操作 库的安装 库的搜索和查看 库的更新和卸载 列出所有库 配置文件.bowerrc 库信息文件bower.json 相关链接 概述 注:bower下载安装依赖库实际上是使用git进行下载. ...

  7. Noip2007提高组总结

    两道基础题,后两题比较麻烦,算法想出来后,还是一些细枝末节的问题,需要特别注意,感觉Noip的题目质量还是挺高的,每做一套,都感觉会有大大小小不同的收获,就要月考了,最后把07年的题目总结一下,算是这 ...

  8. [置顶] 使用mongofiles操作GridFS

    使用mongofiles操作GridFS GridFS描述: GridFS,看起来像一种文件系统,其实是一种数据库用法.主要用来在数据库中存储二进制大文件.可以统一用数据库处理数据,而无需借助外部的文 ...

  9. Software Version --hdu1976

    #include using namespace std; int main() { int T; cin>>T; int a1,b1,c1; int a2,b2,c2; while(T- ...

  10. 使用JS进行pc端、手机端判断

     <script type="text/javascript">            (function(){                var ua = nav ...