原题

给出数n,求出1......n 一串数,其中每个数字分解的两个加数都在这个序列中(除了1,两个加数可以相同),要求这个序列最短。


++m,dfs得到即可。并且事实上不需要提前打好表,直接输出就可以。

  1. #include<cstdio>
  2. using namespace std;
  3. int dep=0,n;
  4. int a[102];
  5. bool dfs(int step)
  6. {
  7. if(step>dep) return a[dep]==n;
  8. for(int i=0;i<step;i++)
  9. {
  10. if(a[step-1]+a[i]>n) break;
  11. a[step]=a[step-1]+a[i];
  12. if(dfs(step+1)) return 1;
  13. }
  14. return 0;
  15. }
  16. int main()
  17. {
  18. a[0]=1;
  19. while(~scanf("%d",&n)&&n)
  20. {
  21. dep=0;
  22. while(!dfs(1)) ++dep;
  23. for(int i=0;i<=dep;i++) printf("%d%c",a[i]," \n"[i==dep]);
  24. }
  25. return 0;
  26. }

提前打表:

  1. #include<cstdio>
  2. using namespace std;
  3. int n,s[110]={0,1,2},cnt=3,l[110]={0,1,2},ans[110][20]={{0},{0,1},{0,1,2}};
  4. void dfs(int x)
  5. {
  6. if (x>cnt) return ;
  7. for (int i=1;i<x;i++)
  8. for (int j=i;j<x;j++)
  9. {
  10. s[x]=s[i]+s[j];
  11. if (s[x]>100 || s[x]<=s[x-1]) continue;
  12. if (!l[s[x]] || l[s[x]]>x)
  13. {
  14. l[s[x]]=x;
  15. for (int l=1;l<=x;l++)
  16. ans[s[x]][l]=s[l];
  17. }
  18. dfs(x+1);
  19. }
  20. }
  21. int main()
  22. {
  23. while (cnt<=10) dfs(3),++cnt;//因为a[1]和a[2]是固定的
  24. while(~scanf("%d",&n) && n)
  25. {
  26. for (int i=1;i<=l[n];i++)
  27. printf("%d%c",ans[n][i]," \n"[i==l[n]]);
  28. }
  29. return 0;
  30. }

[zoj] 1937 [poj] 2248 Addition Chains || ID-DFS的更多相关文章

  1. poj 2248 Addition Chains (迭代加深搜索)

    [题目描述] An addition chain for n is an integer sequence with the following four properties: a0 = 1 am ...

  2. POJ 2248 - Addition Chains - [迭代加深DFS]

    题目链接:http://bailian.openjudge.cn/practice/2248 题解: 迭代加深DFS. DFS思路:从目前 $x[1 \sim p]$ 中选取两个,作为一个新的值尝试放 ...

  3. [POJ 2248]Addition Chains

    Description An addition chain for n is an integer sequence with the following four properties: a0 = ...

  4. POJ 2245 Addition Chains(算竞进阶习题)

    迭代加深dfs 每次控制序列的长度,依次加深搜索 有几个剪枝: 优化搜索顺序,从大往下枚举i, j这样能够让序列中的数尽快逼近n 对于不同i,j和可能是相等的,在枚举的时候用过的数肯定不会再被填上所以 ...

  5. [POJ2248] Addition Chains 迭代加深搜索

    Addition Chains Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5454   Accepted: 2923   ...

  6. UVA 529 Addition Chains(迭代搜索)

      Addition Chains  An addition chain for n is an integer sequence  with the following four propertie ...

  7. 1443:【例题4】Addition Chains

    1443:[例题4]Addition Chains 题解 注释在代码里 注意优化搜索顺序以及最优化剪枝 代码 #include<iostream> #include<cstdio&g ...

  8. 「一本通 1.3 例 4」Addition Chains

    Addition Chains 题面 对于一个数列 \(a_1,a_2 \dots a_{m-1},a_m\) 且 \(a_1<a_2 \dots a_{m-1}<a_m\). 数列中的一 ...

  9. LOJ10021 Addition Chains

    题目描述 原题来自:ZOJ 1937 已知一个数列 A0,A1,A2,A3,...,Am(其中A0=1,Am=n,A0<A1<A2<A3<...<Am ).对于每个 k, ...

随机推荐

  1. LeetCode567. Permutation in String

    Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...

  2. Navicat Premium Mac 12 破解

    破解地址:https://blog.csdn.net/xhd731568849/article/details/79751188 亲测有效

  3. a链接打开另外的新页面

    在a标签添加target = "_blank" 属性即可

  4. linux学习笔记二:三种网络配置

    本文引用自:https://www.linuxidc.com/Linux/2017-05/144370.htm [linux公社] VMware为我们提供了三种网络工作模式,它们分别是:Bridged ...

  5. css3 媒体查询的学习。

    1.什么是媒体查询 媒体查询可以让我们根据设备显示器的特性(如视口宽度.屏幕比例.设备方向:横向或纵向)为其设定CSS样式,媒体查询由媒体类型和一个或多个检测媒体特性的条件表达式组成.媒体查询中可用于 ...

  6. tcl之正则表达式

  7. Pythond函数的参数使用操作注意事项

    定义函数的时候,我们把参数的名字和位置确定下来,函数的接口定义就完成了.对于函数的调用者来说,只需要知道如何传递正确的参数,以及函数将返回什么样的值就够了,函数内部的复杂逻辑被封装起来,调用者无需了解 ...

  8. [Codeforces976E]Well played!(贪心)

    [不稳定的传送门] Solution 首先可以证明,hp翻倍的操作一定是在同一个生物上最优 Code #include <cstdio> #include <algorithm> ...

  9. 17-比赛1 B - 子串计算 Chef and his string challenge (string的运用)

    Chef's best friend Jerry gives Chef a string A and wants to know the number of string A that can be ...

  10. JAVA运行机制

    这一篇我们来简单理解一下JAVA的运行机制 大概可以分为三大部分 1.编写程序 2.编译程序 3.运行程序 1.编写程序 编写程序就是我们前面说的源代码 这些源代码都有特殊的语法 例如main函数 他 ...