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 ...
随机推荐
- BZOJ 1083: [SCOI2005]繁忙的都市(MST)
裸的最小生成树..直接跑就行了 ---------------------------------------------------------------------- #include<c ...
- cocos2d-x spine 加载粒子特效
spine骨骼加载粒子效果 将粒子效果加到骨骼上,update位置和角度.h#ifndef __HOMESCENE_H__#define __HOMESCENE_H__#include "c ...
- C——货物管理系统
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> ...
- Delphi中三种方法获取Windows任务栏的高度
第一种:需要引用Windows单元 ShowMessage(IntToStr(GetSystemMetrics(SM_CYSCREEN)-GetSystemMetrics(SM_CYFULLSCREE ...
- ajax 中文乱码
ajax 中文乱码 Firefox 正常,IE 有问题是 解决办法 data:{"name":name,"number":number,"card ...
- Java Date 和 Calendar
Java 语言的Date(日期),Calendar(日历),DateFormat(日期格式)组成了Java标准的一个基本但是非常重要的部分.日期是商业逻辑计算一个关键的部分,所有的开发者都应该能够计算 ...
- Mysql 创建联合主键
Mysql 创建联合主键2008年01月11日 星期五 下午 5:21使用primary key (fieldlist) 比如: create table mytable ( ...
- Oracle 集群心跳及其參数misscount/disktimeout/reboottime
在Oracle RAC中,能够从多个层次,多个不同的机制来检測RAC的健康状况,即能够通过心跳机制以及一定的投票算法来隔离故障.假设检測到某节点失败,则存在故障的节点将会被逐出集群以避免故障节点破坏数 ...
- 74HC595的中文资料
74HC595--具有三态输出锁存功能的8位串行输入.串行/并行输出移位寄存器 本文翻译自NXP的74HC595的datasheet 74HC595和74HCT595是带有存储寄存器和三态输出的8位串 ...
- JavaScript推断E-mail地址是否合法
编写自己定义的JavaScript函数checkEmail(),在该函数中首先推断E-mail文本框是否为空,然后在应用正則表達式推断E-mail地址是否合法,假设不合法提示用户 <script ...