D.6661 - Equal Sum Sets
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的更多相关文章
- 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 ...
- UVALive 6661 Equal Sum Sets
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- [UVALive 6661 Equal Sum Sets] (dfs 或 dp)
题意: 求从不超过 N 的正整数其中选取 K 个不同的数字,组成和为 S 的方法数. 1 <= N <= 20 1 <= K<= 10 1 <= S <= 15 ...
- UvaLive6661 Equal Sum Sets dfs或dp
UvaLive6661 PDF题目 题意:让你用1~n中k个不同的数组成s,求有多少种组法. 题解: DFS或者DP或打表. 1.DFS 由于数据范围很小,直接dfs每种组法统计个数即可. //#pr ...
- 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 ...
- HDU 3280 Equal Sum Partitions(二分查找)
Equal Sum Partitions Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 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 ...
- [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 ...
随机推荐
- 【Java】 实现一个简单文件浏览器(1)
学习Java的Swing的时候写的一个超简单文件浏览器 效果如图: 项目结构: 这里面主要用了两个控件,JTree和JTable 下面先说下左侧的文件树如何实现: 首先是FileTree类,继承于JT ...
- BZOJ 3156: 防御准备( dp + 斜率优化 )
dp(i)表示处理完[i,n]且i是放守卫塔的最小费用. dp(i) = min{dp(j) + (j-i)(j-i-1)/2}+costi(i<j≤N) 然后斜率优化 ------------ ...
- 一周学会Mootools 1.4中文教程:(2)函数
温故: 透过对上一节课的学习,相信大家对mt的选择器应该有了一定的认识了,我再放几个小示例让大家对选择器的复杂应用有所了解: <!DOCTYPE html PUBLIC "-//W3C ...
- ASP.NET MVC进阶之路:依赖注入(Di)和Ninject
0X1 什么是依赖注入 依赖注入(Dependency Injection),是这样一个过程:某客户类只依赖于服务类的一个接口,而不依赖于具体服务类,所以客户类只定义一个注入点.在程序运行过程中,客户 ...
- phome_ecms_news 数据表字段解释(新闻系统模型-主表)
http://www.phome.net/doc/manual/extend/html/dbdoc/index.html 字段名 类型 解释 附加说明 id int(11) 信息ID classi ...
- 进一步探索:Windows Azure 网站中解锁的配置选项
编辑人员注释: 本文章由 Windows Azure 网站团队的项目经理 Erez Benari 撰写. 在 Windows Azure 网站 (WAWS) 中管理网站时,许多选项可使用 Azu ...
- 【蓝桥杯】入门训练 Fibonacci数列
入门训练 Fibonacci数列 时间限制:1.0s 内存限制:256.0MB 问题描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. ...
- 概率图模型(PGM)学习笔记(三)模式判断与概率图流
我们依旧使用"学生网络"作为样例,如图1. 图1 首先给出因果判断(Causal Reasoning)的直觉解释. 能够算出来 即学生获得好的推荐信的概率大约是0.5. 但假设我们 ...
- hadoop高速扫盲帖,从零了解hadoop
1.MapReduce理论简单介绍 1.1 MapReduce编程模型 MapReduce採用"分而治之"的思想,把对大规模数据集的操作,分发给一个主节点管理下的各个分节点共同完毕 ...
- 关于js的一些总结
1.js中如果判断变量是否为空,要注意两种情况: 一种是变量为空:另一种是变量为0: 2.js如果要进行算术运算或者比较,最好把它强制类型为整型或者浮点型.防止字符串比较,如果本来是数字的字符串,如果 ...