1. <span style="color:#330099;">/*
  2. I - 深搜 基础
  3. Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
  4. Submit
  5.  
  6. Status
  7. Description
  8. Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t = 4, n = 6, and the list is [4, 3, 2, 2, 1, 1], then there are four different sums that equal 4: 4, 3+1, 2+2, and 2+1+1. (A number can be used within a sum as many times as it appears in the list, and a single number counts as a sum.) Your job is to solve this problem in general.
  9. Input
  10. The input will contain one or more test cases, one per line. Each test case contains t, the total, followed by n, the number of integers in the list, followed by n integers x 1 , . . . , x n . If n = 0 it signals the end of the input; otherwise, t will be a positive integer less than 1000, n will be an integer between 1 and 12 (inclusive), and x 1 , . . . , x n will be positive integers less than 100. All numbers will be separated by exactly one space. The numbers in each list appear in nonincreasing order, and there may be repetitions.
  11. Output
  12. For each test case, first output a line containing `Sums of', the total, and a colon. Then output each sum, one per line; if there are no sums, output the line `NONE'. The numbers within each sum must appear in nonincreasing order. A number may be repeated in the sum as many times as it was repeated in the original list. The sums themselves must be sorted in decreasing order based on the numbers appearing in the sum. In other words, the sums must be sorted by their first number; sums with the same first number must be sorted by their second number; sums with the same first two numbers must be sorted by their third number; and so on. Within each test case, all sums must be distinct; the same sum cannot appear twice.
  13. Sample Input
  14. 4 6 4 3 2 2 1 1
  15. 5 3 2 1 1
  16. 400 12 50 50 50 50 50 50 25 25 25 25 25 25
  17. 0 0
  18. Sample Output
  19. Sums of 4:
  20. 4
  21. 3+1
  22. 2+2
  23. 2+1+1
  24. Sums of 5:
  25. NONE
  26. Sums of 400:
  27. 50+50+50+50+50+50+25+25+25+25
  28. 50+50+50+50+50+25+25+25+25+25+25
  29. By Grant Yuan
  30. 2014.7.14
  31. */
  32. #include<iostream>
  33. #include<cstdio>
  34. #include<cstring>
  35. #include<cstdlib>
  36. #include<cmath>
  37. #include<queue>
  38. #include<stack>
  39. using namespace std;
  40. int a[13];
  41. int fre[13];
  42. int ffre[100];
  43. int t,n;
  44. int s[100];
  45. bool mark;
  46. int num;
  47. int top;
  48. int top1;
  49. int sum;
  50. int first;
  51. void sort()
  52. {int t1;
  53. for(int i=0;i<top1;i++)
  54. for(int j=i;j<=top1;j++)
  55. {
  56. if(a[i]<a[j]){
  57. t1=a[i],a[i]=a[j],a[j]=t1;
  58. t1=fre[i],fre[i]=fre[j],fre[j]=t1;
  59. }
  60. }
  61. }
  62.  
  63. void pt()
  64. { int bear=0;
  65. for(int i=0;i<=top;i++)
  66.  
  67. if(ffre[i]){
  68. for(int j=1;j<=ffre[i];j++)
  69. { if(bear==0)
  70. {
  71. cout<<s[i];
  72. bear=1;}
  73. else
  74. printf("+%d",s[i]);}
  75.  
  76. }
  77. cout<<endl;
  78. }
  79.  
  80. void dps(int k)
  81. {
  82. if(k>top1){
  83. if(sum==t)
  84. {mark=1;
  85. if(first==0)
  86. printf("Sums of %d:\n",t);
  87. first=1;pt();num++;}
  88. return;
  89. }
  90. for(int i=fre[k];i>=0;i--)
  91. {if(sum+a[k]*i<=t){
  92. s[++top]=a[k];
  93. ffre[top]=i;
  94. sum+=a[k]*i;
  95. dps(k+1);
  96. top--;
  97. sum-=a[k]*i;
  98. }
  99.  
  100. }
  101. }
  102.  
  103. int main()
  104. {
  105. while(1){
  106. cin>>t>>n;
  107. top1=-1;
  108. top=-1;
  109. sum=0;
  110. first=0;
  111. mark=0;
  112. memset(fre,0,sizeof(fre));
  113. memset(ffre,0,sizeof(ffre));
  114. num=0;
  115. if(n==0)
  116. break;
  117. int m;
  118. bool flag1;
  119. for(int i=0;i<n;i++)
  120. {flag1=0;
  121. cin>>m;
  122. for(int j=0;j<=top1;j++)
  123. {
  124. if(m==a[j])
  125. flag1=1,fre[j]++;
  126. }
  127. if(flag1==0)
  128. {a[++top1]=m;
  129. fre[top1]=1;}}
  130. sort();
  131. dps(0);
  132. if(mark==0)
  133. {printf("Sums of %d:\n",t);
  134. printf("NONE\n");}
  135. }
  136. return 0;
  137. }
  138. </span>

I深搜的更多相关文章

  1. HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?

    这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others)  ...

  2. 利用深搜和宽搜两种算法解决TreeView控件加载文件的问题。

    利用TreeView控件加载文件,必须遍历处所有的文件和文件夹. 深搜算法用到了递归. using System; using System.Collections.Generic; using Sy ...

  3. 2016弱校联盟十一专场10.3---Similarity of Subtrees(深搜+hash、映射)

    题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52310 problem description Define the depth of a ...

  4. 2016弱校联盟十一专场10.2---Around the World(深搜+组合数、逆元)

    题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52305 problem  description In ICPCCamp, there ar ...

  5. 2015暑假多校联合---Cake(深搜)

    题目链接:HDU 5355 http://acm.split.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m s ...

  6. 深搜+回溯 POJ 2676 Sudoku

    POJ 2676 Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17627   Accepted: 8538 ...

  7. 深搜+DP剪枝 codevs 1047 邮票面值设计

    codevs 1047 邮票面值设计 1999年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description ...

  8. 【wikioi】1049 棋盘染色(迭代深搜)

    http://www.wikioi.com/problem/1049/ 这题我之前写没想到迭代加深,看了题解,然后学习了这种搜索(之前我写的某题也用过,,但是不懂专业名词 囧.) 迭代加深搜索就是限制 ...

  9. poj1190 生日蛋糕(深搜+剪枝)

    题目链接:poj1190 生日蛋糕 解题思路: 深搜,枚举:每一层可能的高度和半径 确定搜索范围:底层蛋糕的最大可能半径和最大可能高度 搜索顺序:从底层往上搭蛋糕,在同一层尝试时,半径和高度都是从大到 ...

  10. HDU 4597 Play Game(记忆化搜索,深搜)

    题目 //传说中的记忆化搜索,好吧,就是用深搜//多做题吧,,这个解法是搜来的,蛮好理解的 //题目大意:给出两堆牌,只能从最上和最下取,然后两个人轮流取,都按照自己最优的策略,//问说第一个人对多的 ...

随机推荐

  1. paip.odbc DSN的存储与读取

    paip.odbc DSN的存储与读取 作者Attilax ,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http://blog.csdn.net/atti ...

  2. 断剑重铸之日,席卷朗朗乾坤之时--PHP学习一月漫记

    传说中阿尔萨斯王子沉沦堕落之后,被巫妖王安置在冰冷的城堡中,静静地等待重出天日,它随身携带的宝剑也埋没与尘土之间,暗淡无光.他想起宝剑伴身,东征西战的峥嵘岁月,忆及如今身陷囹圄,一股怨念由心底升起,许 ...

  3. 在StatusBar中显示当前时间

    在StatusBar中显示当前时间,如下: 1.在String Table中插入一项 (注意:状态栏将根据字符串的长度来确定相应窗格的缺省宽度,所以指定为00:00:00就为时间的显示预留了空间)   ...

  4. php运行

    运行命令: $php 1.php php教程: http://www.w3school.com.cn/php/php_variables.asp

  5. Redisql: the lightning fast data polyglot【翻译】 - Linvo's blog - 博客频道 - CSDN.NET

    Redisql: the lightning fast data polyglot[翻译] - Linvo's blog - 博客频道 - CSDN.NET Redisql: the lightnin ...

  6. Android应用开发学习笔记之绘图

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 一.绘图常用类介绍 在Android中绘图时,常用到的几个类是Paint.Canvas.Bitmap和Bitmapt ...

  7. 访问祖先类的虚方法(直接访问祖先类的VMT,但是这种方法在新版本中未必可靠)

    访问祖先类的虚方法 问题提出 在子类覆盖的虚方法中,可以用inherited调用父类的实现,但有时候我们并不需要父类的实现,而是想跃过父类直接调用祖先类的方法. 举个例子,假设有三个类,实现如下: t ...

  8. [置顶] Bug 11775332 - cluvfy fails with PRVF-5636 with DNS response timeout error [ID 11775332.8]

    Bug 11775332  cluvfy fails with PRVF-5636 withDNS response timeout error but error text is not clear ...

  9. 简单的 "双缓冲" 绘图的例子(研究一下)

    所谓双缓冲就是先画到内存画布(如: TBitmap), 然后再转帖到目的地. 譬如下面小程序: procedure TForm1.FormCreate(Sender: TObject); begin ...

  10. LAN路由

    一.实验的目的:   实现不同子网之前的信息交流      二.如果 1.虚拟子网 VMnet8:192.168.233.0/24 VMnet1:172.16.1.0/24 2.虚拟机vm1 ip:1 ...