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. gcc 的编译过程

    通常我们都是使用下面的命令来直接生成可执行文件 gcc demo.c -o demo 对于我们来说十分简单,但是对编译器来说却完成了一系列复杂的工作,概括起来有如下几步: 1. 预处理 gcc -E ...

  2. JQuery中文本框获取焦点

    今天遇见这么一个小小的问题,就是文本框中需要输入内容才可以提交,如果没有输入就提示并使该文本框获得焦点! 这么一个简单的事情如果没有使用jQuery的话 是不是对象.focus()就可以了, 可是当我 ...

  3. virtualbox 中安装win7虚拟机

    下载了win7镜像文件后,在virtualbox中装了几次都提示 windows faied to start,后来在网上找了些解决办法,在这记录下,免得下次又忘了 创建新的虚拟机: 1.安装virt ...

  4. 用PyRestful快速构建Tornado下REST APIs 的支持

    一.安装PyRestful库 $ pip install pyrestful 二.使用案例 (一)books_service.py # -*- coding: utf-8 -*- import tor ...

  5. windows文件快速搜索软件推荐

    everything文件搜索工具,可以快速搜索windows下的文件

  6. 设置MAVEN_OPTS的推荐方法

    运行mvn eclipse:eclipse时,经常会出现因为maven项目过大,导致内存不足java栈溢出的error,需要更改MAVEN启动内存 http://stackoverflow.com/q ...

  7. cpu卡,sam卡原理

    第一部分 CPU基础知识一.为什么用CPU卡IC卡从接口方式上分,可以分为接触式IC卡.非接触式IC卡及复合卡.从器件技术上分,可分为非加密存储卡.加密存储卡及CPU卡.非加密卡没有安全性,可以任意改 ...

  8. Spring Boot Logback应用日志

    e Spring Boot Logback应用日志 2015-09-08 19:57 7673人阅读 评论(0) 收藏 举报 . 分类: Spring Boot(51) . 目录(?)[+] 日志对于 ...

  9. 宣布发布 Windows Azure ExpressRoute,宣告与 Level 3 建立全新的合作伙伴关系并推出关于其他 Azure 服务令人振奋的更新

     在我们与世界各地的客户和合作伙伴交谈时,总会听到他们说,希望找到一个提供商帮助他们最大限度地发挥内部部署投资的作用并且能够利用云的灵活性.这是我们构建混合云策略和云操作系统愿景的基本原则.本着我 ...

  10. VS2010/MFC对话框:非模态对话框的创建及显示

    非模态对话框的创建及显示 上一节讲了模态对话框及其弹出过程,本节接着讲另一种对话框--非模态对话框的创建及显示. 已经说过,非模态对话框显示后,程序其他窗口仍能正常运行,可以响应用户输入,还可以相互切 ...