LOJ10021 Addition Chains
原题来自:ZOJ 1937
已知一个数列 A0,A1,A2,A3,...,Am(其中A0=1,Am=n,A0<A1<A2<A3<...<Am )。对于每个 k,需要满足 Ak=Ai+Aj(0<=i,j<k这里 i 与 j 可以相等)。
现给定 n 的值,要求 m 的最小值(并不要求输出),及这个数列每一项的值(可能存在多个数列,只输出任一个满足条件的就可以了)。
多组数据,每行给定一个正整数 n 。
输入以 0 结束。
对于每组数据,输出满足条件的长度最小的数列。
_________________________

1 #include<bits/stdc++.h>
2 using namespace std;
3 int n,ans;
4 int sz[105],da[105];
5 void dfs(int p)
6 {
7 if(sz[p-1]>n)return;
8 if(p-1>=ans)return ;
9 if(sz[p-1]==n && p-1<ans)
10 {
11 ans=p-1;
12 for(int i=1;i<p;++i)da[i]=sz[i];
13 }
14 for(int i=p-1;i>=1;--i)
15 {
16 sz[p]=sz[p-1]+sz[i];
17 dfs(p+1);
18 }
19 }
20 int main()
21 {
22 sz[1]=1;
23 while(scanf("%d",&n)==1 && n)
24 {
25 ans=105;
26 dfs(2);
27 for(int i=1;i<=ans;++i)printf("%d ",da[i]);
28 putchar('\n');
29 }
30 return 0;
31 }
LOJ10021 Addition Chains的更多相关文章
- UVA 529 Addition Chains(迭代搜索)
Addition Chains An addition chain for n is an integer sequence with the following four propertie ...
- [POJ2248] Addition Chains 迭代加深搜索
Addition Chains Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5454 Accepted: 2923 ...
- 1443:【例题4】Addition Chains
1443:[例题4]Addition Chains 题解 注释在代码里 注意优化搜索顺序以及最优化剪枝 代码 #include<iostream> #include<cstdio&g ...
- poj 2248 Addition Chains (迭代加深搜索)
[题目描述] An addition chain for n is an integer sequence with the following four properties: a0 = 1 am ...
- 「一本通 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\). 数列中的一 ...
- UVA 529 - Addition Chains,迭代加深搜索+剪枝
Description An addition chain for n is an integer sequence with the following four properties: a0 = ...
- [POJ 2248]Addition Chains
Description An addition chain for n is an integer sequence with the following four properties: a0 = ...
- Addition Chains POJ - 2248 (bfs / dfs / 迭代加深)
An addition chain for n is an integer sequence <a0, a1,a2,...,am=""> with the follow ...
- C++解题报告 : 迭代加深搜索之 ZOJ 1937 Addition Chains
此题不难,主要思路便是IDDFS(迭代加深搜索),关键在于优化. 一个IDDFS的简单介绍,没有了解的同学可以看看: https://www.cnblogs.com/MisakaMKT/article ...
随机推荐
- 加班申请单flowable中
/* * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. * * Redistribution and use in source ...
- Vue - MVVM模式及优点
MVVM模式 视图层和数据层的双向绑定,让我们无需再去关心DOM操作的事情,更多的精力放在数据和业务逻辑上去 MVVM是Model-View-ViewModel的缩写.MVVM是一种设计思想. Mod ...
- Mapreduce实例--二次排序
前言部分: 在Map阶段,使用job.setInputFormatClass定义的InputFormat将输入的数据集分割成小数据块splites,同时InputFormat提供一个RecordRed ...
- java数组之system.arrayCopy
public class ArrayDemo { /* public static void main(String[] args) { int[] a=new int[4]; int[] b=new ...
- 日常ie兼容问题(持续整理)
1.关于new Date()格式为何要转成y/m/d格式 IE不会识别时间状态为"y-m-d"的形式,如果获取的new Date("2020-05-01") 那 ...
- 一些php文件函数
当读入一个巨大的字符串的时候不能使用file_get_contents('文件名') 应该 使用fopen('文件名','r') feof('文件名') //判断是否读到了文件结尾 ******** ...
- Python使用urllib,urllib3,requests库+beautifulsoup爬取网页
Python使用urllib/urllib3/requests库+beautifulsoup爬取网页 urllib urllib3 requests 笔者在爬取时遇到的问题 1.结果不全 2.'抓取失 ...
- 安装Android Studio遇到的问题
1. 学习视频 视频链接:https://www.bilibili.com/video/BV1jW411375J?p=2 2. Android Studio1.5.1的下载地址: http://www ...
- 十八般武艺玩转GaussDB(DWS)性能调优:路径干预
摘要:路径生成是表关联方式确定的主要阶段,本文介绍了几个影响路径生成的要素:cost_param, scan方式,join方式,stream方式,并从原理上分析如何干预路径的生成. 一.cost模型选 ...
- Es5数组新增的方法及用法
1.forEachforEach是Array新方法中最基本的一个,就是遍历,循环.例如下面这个例子: [1, 2 ,3, 4].forEach(alert);等同于下面这个传统的for循环: var ...