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
程序分析:这种题目,一看就知道思路,就是枚举法来做,可是有那么多种情况,N!种,怎么都超时了,可是我们也要注意到一种题目的标志,就是状态压缩,对于这类题目,n一般都是20,而这个题就是20,而且完全符合状态压缩,虽然这个题有dp 的解法,可是状态压缩已经足以解决了。
程序代码:
  1. #include<iostream>
  2. using namespace std;
  3. int a[];
  4. int A[];
  5. int ans=;
  6. void dfs(int n,int cur){
  7. if(cur==a[]){
  8. int sum=;
  9. for(int i=;i<a[];i++){
  10. sum+=A[i];
  11. }
  12. if(sum==a[])ans++;
  13. }
  14. int s=;
  15. if(cur!=)s=A[cur-]+;
  16. for(int i=s;i<=n;i++){
  17. A[cur]=i;
  18. dfs(n,cur+);
  19. }
  20. }
  21. int main(){
  22. while(cin>>a[]>>a[]>>a[]&&a[]+a[]+a[]){
  23. ans=;
  24. dfs(a[],);
  25. cout<<ans<<endl;
  26. }
  27. return ;
  28. }


您的朋友 陈春辉 为这封邮件插入了背景音乐 - 下载  播放
 
播放器加载中...
正在发送...
 
此邮件已成功发送。再回一封
 

Aizu 1335 Eequal sum sets的更多相关文章

  1. Eequal sum sets

    Let us consider sets of positive integers less than or equal to n. Note that all elements of a set a ...

  2. D.6661 - Equal Sum Sets

    Equal Sum Sets Let us consider sets of positive integers less than or equal to n. Note that all elem ...

  3. 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 ...

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

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

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

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

  8. Project Euler P105:Special subset sums: testing 特殊的子集和 检验

    Special subset sums: testing Let S(A) represent the sum of elements in set A of size n. We shall cal ...

  9. Project Euler 103:Special subset sums: optimum 特殊的子集和:最优解

    Special subset sums: optimum Let S(A) represent the sum of elements in set A of size n. We shall cal ...

随机推荐

  1. Android手机设置隐藏命令大全

    注意:因Android版本较多,固有部分隐藏命令或不能使用 *#*#4636#*#* 显示手机信息.电池信息.电池记录.使用统计数据.WiFi 信息 *#*#7780#*#* 重设为原厂设定,不会删除 ...

  2. 2014多校3 Wow! Such Sequence!段树

    主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=4893 这个问题还真是纠结啊--好久不写线段树的题了.由于这几天学伸展树.然后认为线段树小case了. ...

  3. c基础知识复习

    C的发展历程 C原本是为了开发UNIX操作系统而设计的语言:如此说,应该C比UNIX更早问世,而事实并非如此,最早的UNIX是由汇编写的: C语言本来是美国人开发的,解读C的声明,最好还是用英语来读: ...

  4. 碰撞缓冲效果的导航条 js

  5. Java多线程之synchronized(四)

    前面几章都是在说synchronized用于对象锁,无论是修饰方法也好修饰代码块也好,然而关键字synchronized还可以应用到static静态方法上,如果这样写,那就是对当前的*.java文件所 ...

  6. 解决:Visual Assist X 不支持HTML、Javascript等提示

    Visual Assist X 安装后,不能进行javascript hmtl提示,只有回到老版本才行.这个问题折腾了老久,才给解决了. 记录下来,以便于网友和自己使用. 问题原因: Visual A ...

  7. net core 静态文件

    asp.net core 之静态文件目录的操作   文章前言 之前写了一篇关于模拟登录的文章,自我感觉内容不太丰富,今天的这篇文章,希望在内容上能丰富些.本人缺少写文章的经验,技术上也是新手,但我会努 ...

  8. AutoCAD 2014简体中文版官方正式版x86 x64下载,带注册机,永久免费使用

    注册机使用说明:会有部分杀毒软件报病毒,请无视.操作步骤:1.安装Autodesk AutoCAD 20142.使用以下序列号666-69696969安装.3.产品密码为001F14.安装完成后,启动 ...

  9. 通过Qt样式表定制程序外观(比较通俗易懂)

    1. 何为Qt样式表[喝小酒的网摘]http://blog.hehehehehe.cn/a/10270.htm2. 样式表语法基础3. 方箱模型4. 前景与背景5. 创建可缩放样式6. 控制大小7. ...

  10. 解决sqlite删除数据后,文件大小不变问题(VACUUM)

    删除表格的全部数据: DELETE FROM [Name] 当在sqlite中删除了大量数据后,数据库文件的大小还是那样,没有变.原因是:从Sqlite删除数据后,未使用的磁盘空间被添加到一个内在的” ...