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. La=LaULb (单链表)

    #include<stdio.h> typedef struct LNode { int data; struct LNode *next; }LNode,*LinkList; void ...

  2. 第二天(CSS 选择器)

    1.常用的CSS选择器         类型选择器: 例如: p { color : red ; }         后代选择器: 例如: h2 span { font-weight : bold ; ...

  3. ssma for oracle

    SQL Server Migration Assistant (SSMA) for Oracle lets you quickly convert Oracle database schemas to ...

  4. selenium 学习笔记 ---新手学习记录(2) 问题总结

    今天研究了下ie.chrome.firefox浏览器执行脚本 1.首先firefox下执行时,我是安装在d盘了,所以要更改路径 //如果火狐浏览器没有默认安装在C盘,需要制定其路径 System.se ...

  5. C-重定向

    说实话,第一次接触重定向这一个概念,感觉是那么的神奇简洁不可思议…………………… freopen() 本来应该是打开的是文件指针,但是分配了指针,使她(亲切)指向了标准输入.输出.错误流. 用 法: ...

  6. socket 通信 入门3 android 客户端 C# 服务端

    这是一个android端操控服务器的例子  就是发送简单指令到服务器  然后服务器响应什么的... 当然这里是未完成的  只是简单展示一下大致思路 首先连接建立起来后  服务端给客户端一条信息  告诉 ...

  7. 解决用户 'IIS APPPOOL\Classic .NET AppPool' 登录失败

    解决用户 'IIS APPPOOL\Classic .NET AppPool' 登录失败 windows 7进入iis管理器 本地应用程序池 选中classic. net appPool 选择右侧的 ...

  8. JS中的RegExp对象常用属性和方法

    JavaScript提供了一个RegExp对象来完成有关正则表达式的操作和功能,每一条正则表达式模式对应一个RegExp实例.有两种方式可以创建RegExp对象的实例. 使用RegExp的显式构造函数 ...

  9. 挺有意思的Blog

    http://blog.csdn.net/dawanganban/article/details/19925449 http://www.syslog.org/ https://baoz.net/li ...

  10. SQVI和SAP查询QUERY的区别和使用注意事项

    SQVI.SAP查询QUERY都适用于简单的表连接数据查询,但都不能打包传输到PRD,不同环境需要重复创建.可以生成报表程序供T-CODE调用,用se93指定事务码和程序名称. 区别1-权限: SQV ...