[zoj] 1937 [poj] 2248 Addition Chains || ID-DFS
原题
给出数n,求出1......n 一串数,其中每个数字分解的两个加数都在这个序列中(除了1,两个加数可以相同),要求这个序列最短。
++m,dfs得到即可。并且事实上不需要提前打好表,直接输出就可以。
#include<cstdio>
using namespace std;
int dep=0,n;
int a[102];
bool dfs(int step)
{
if(step>dep) return a[dep]==n;
for(int i=0;i<step;i++)
{
if(a[step-1]+a[i]>n) break;
a[step]=a[step-1]+a[i];
if(dfs(step+1)) return 1;
}
return 0;
}
int main()
{
a[0]=1;
while(~scanf("%d",&n)&&n)
{
dep=0;
while(!dfs(1)) ++dep;
for(int i=0;i<=dep;i++) printf("%d%c",a[i]," \n"[i==dep]);
}
return 0;
}
提前打表:
#include<cstdio>
using namespace std;
int n,s[110]={0,1,2},cnt=3,l[110]={0,1,2},ans[110][20]={{0},{0,1},{0,1,2}};
void dfs(int x)
{
if (x>cnt) return ;
for (int i=1;i<x;i++)
for (int j=i;j<x;j++)
{
s[x]=s[i]+s[j];
if (s[x]>100 || s[x]<=s[x-1]) continue;
if (!l[s[x]] || l[s[x]]>x)
{
l[s[x]]=x;
for (int l=1;l<=x;l++)
ans[s[x]][l]=s[l];
}
dfs(x+1);
}
}
int main()
{
while (cnt<=10) dfs(3),++cnt;//因为a[1]和a[2]是固定的
while(~scanf("%d",&n) && n)
{
for (int i=1;i<=l[n];i++)
printf("%d%c",ans[n][i]," \n"[i==l[n]]);
}
return 0;
}
[zoj] 1937 [poj] 2248 Addition Chains || ID-DFS的更多相关文章
- poj 2248 Addition Chains (迭代加深搜索)
[题目描述] An addition chain for n is an integer sequence with the following four properties: a0 = 1 am ...
- POJ 2248 - Addition Chains - [迭代加深DFS]
题目链接:http://bailian.openjudge.cn/practice/2248 题解: 迭代加深DFS. DFS思路:从目前 $x[1 \sim p]$ 中选取两个,作为一个新的值尝试放 ...
- [POJ 2248]Addition Chains
Description An addition chain for n is an integer sequence with the following four properties: a0 = ...
- POJ 2245 Addition Chains(算竞进阶习题)
迭代加深dfs 每次控制序列的长度,依次加深搜索 有几个剪枝: 优化搜索顺序,从大往下枚举i, j这样能够让序列中的数尽快逼近n 对于不同i,j和可能是相等的,在枚举的时候用过的数肯定不会再被填上所以 ...
- [POJ2248] Addition Chains 迭代加深搜索
Addition Chains Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5454 Accepted: 2923 ...
- UVA 529 Addition Chains(迭代搜索)
Addition Chains An addition chain for n is an integer sequence with the following four propertie ...
- 1443:【例题4】Addition Chains
1443:[例题4]Addition Chains 题解 注释在代码里 注意优化搜索顺序以及最优化剪枝 代码 #include<iostream> #include<cstdio&g ...
- 「一本通 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\). 数列中的一 ...
- LOJ10021 Addition Chains
题目描述 原题来自:ZOJ 1937 已知一个数列 A0,A1,A2,A3,...,Am(其中A0=1,Am=n,A0<A1<A2<A3<...<Am ).对于每个 k, ...
随机推荐
- LeetCode567. Permutation in String
Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...
- Navicat Premium Mac 12 破解
破解地址:https://blog.csdn.net/xhd731568849/article/details/79751188 亲测有效
- a链接打开另外的新页面
在a标签添加target = "_blank" 属性即可
- linux学习笔记二:三种网络配置
本文引用自:https://www.linuxidc.com/Linux/2017-05/144370.htm [linux公社] VMware为我们提供了三种网络工作模式,它们分别是:Bridged ...
- css3 媒体查询的学习。
1.什么是媒体查询 媒体查询可以让我们根据设备显示器的特性(如视口宽度.屏幕比例.设备方向:横向或纵向)为其设定CSS样式,媒体查询由媒体类型和一个或多个检测媒体特性的条件表达式组成.媒体查询中可用于 ...
- tcl之正则表达式
- Pythond函数的参数使用操作注意事项
定义函数的时候,我们把参数的名字和位置确定下来,函数的接口定义就完成了.对于函数的调用者来说,只需要知道如何传递正确的参数,以及函数将返回什么样的值就够了,函数内部的复杂逻辑被封装起来,调用者无需了解 ...
- [Codeforces976E]Well played!(贪心)
[不稳定的传送门] Solution 首先可以证明,hp翻倍的操作一定是在同一个生物上最优 Code #include <cstdio> #include <algorithm> ...
- 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 ...
- JAVA运行机制
这一篇我们来简单理解一下JAVA的运行机制 大概可以分为三大部分 1.编写程序 2.编译程序 3.运行程序 1.编写程序 编写程序就是我们前面说的源代码 这些源代码都有特殊的语法 例如main函数 他 ...