You have array a1, a2, ..., an. Segment [l, r] (1 ≤ lrn) is good if ai = ai - 1 + ai - 2, for all i (l + 2 ≤ ir).

Let's define len([l, r]) = r - l + 1, len([l, r]) is the length of the segment [l, r]. Segment [l1, r1], is longer than segment [l2, r2], if len([l1, r1]) > len([l2, r2]).

Your task is to find a good segment of the maximum length in array a. Note that a segment of length 1 or 2 is always good.

Input

The first line contains a single integer n (1 ≤ n ≤ 105) — the number of elements in the array. The second line contains integers: a1, a2, ..., an (0 ≤ ai ≤ 109).

Output

Print the length of the longest good segment in array a.

Sample test(s)
Input
10
1 2 3 5 8 13 21 34 55 89
Output
10
Input
5
1 1 1 1 1
Output
2

找出符合a[i] = a[i-1]+a[i-2]的最长长度

没有考虑0的状况。。。。。。蛋疼,长久没做CF了,坑成球了。。。

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int a[100005],b[100005],len; int main()
{
int maxn,sum;
int i,n;
while(~scanf("%d",&n))
{
sum = 0;
for(i = 0; i<n; i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
if(sum == 0)
{
printf("%d\n",n);
continue;
}
if(n == 1)
{
printf("1\n");
continue;
}
if(n == 2)
{
printf("2\n");
continue;
}
len = 0;
int l,r,flag = 1;
for(i = 2; i<n; i++)
{
if(a[i-1]+a[i-2] == a[i])
b[len++] = 1;
else
b[len++] = 0;
}
maxn = 0;
l = 0;
r = 0;
for(i = 0; i<len; i++)
{
if(b[i] == 1)
{
if(flag)
{
l = i;
flag = 0;
}
else
r = i;
}
else
{
if(r-l+1>maxn && r!=l)
maxn = r-l+1;
flag = 1;
l = 0;
r = 0;
}
}
if(r-l+1>maxn && r!=l)
maxn = r-l+1;
printf("%d\n",maxn+2);
} return 0;
}

CF#213DIV2:B The Fibonacci Segment的更多相关文章

  1. cf B. The Fibonacci Segment

    http://codeforces.com/contest/365/problem/B #include <cstdio> #include <cstring> #includ ...

  2. CF 914 G Sum the Fibonacci —— 子集卷积,FWT

    题目:http://codeforces.com/contest/914/problem/G 其实就是把各种都用子集卷积和FWT卷起来算即可: 注意乘 Fibonacci 数组的位置: 子集卷积时不能 ...

  3. 笔试算法题(11):Josephus环 & Fibonacci序列

    出题:Josephus Cycle,约瑟夫环问题.k个数字连成一个环,第一个数字为1.首先从1开始计数删除第m个数字:然后从上次被删除的数字的下一个数字开始计数,删除第m个数字:重复进行第二步直到只剩 ...

  4. 题解报告:poj 3070 Fibonacci

    题目链接:http://poj.org/problem?id=3070 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, a ...

  5. 题解报告:hdu 1848 Fibonacci again and again(尼姆博弈)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1848 Problem Description 任何一个大学生对菲波那契数列(Fibonacci num ...

  6. codeforces B. The Fibonacci Segment 解题报告

    题目链接:http://codeforces.com/problemset/problem/365/B 题目意思:简单来说,就是要找出最长的斐波纳契长度. 解决的方法不难,但是要注意更新左区间和右区间 ...

  7. HDU1250:Hat's Fibonacci

    Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequen ...

  8. 2019牛客暑期多校训练营(第九场)A:Power of Fibonacci(斐波拉契幂次和)

    题意:求Σfi^m%p. zoj上p是1e9+7,牛客是1e9:  对于这两个,分别有不同的做法. 前者利用公式,公式里面有sqrt(5),我们只需要二次剩余求即可.     后者mod=1e9,5才 ...

  9. Codeforces Round #213 (Div. 2) B. The Fibonacci Segment

    #include <iostream> #include <algorithm> #include <vector> using namespace std; in ...

随机推荐

  1. aliyun硬盘挂载

    实在难以忍受公司服务器的网络问题,停用了半年的aliyun服务器今天终于决定启用了. 购买的时候是40G的硬盘空间,首先查了一硬盘情况结果发现有一个分区居然没有挂载.  第一步是创建一个分区 输入命令 ...

  2. 【转】Ubuntu 上编译Android出现cannot find -lstdc++解决办法

    [转]Ubuntu 上编译Android出现cannot find -lstdc++解决办法 在Ubuntu 12.04 x86_64机器上编译Android出现下面错误,是因为找不到32bit的li ...

  3. 通过SqlHelper实现登录

    1.建立解决方案:. 首先创建windows窗体应用程序,然后拖出登录界面 2. 写配置文件App.config:     <configuration>         <conn ...

  4. ecplise启动tomcat异常

    由于myeclipse为正常关闭,导致下次启动时抛出异常:类似于无法加载异常缓存的内容之类... 处理办法: 1.关闭myeclipse 2.找到工作空间/.metadata/.plugins/ 3. ...

  5. Arduino101 中使用 Mirf 库的问题(2016-04-04)

    Mirf 库在使用 NRF24L01 的时候接触到,感觉很好用.但在用 Arduino101 的时候遇到一些问题,记录一下,对于底层不了解,希望有熟悉的同学能给点指点: 编译会提示 MirfHardw ...

  6. IOS 隐藏键盘。

    在View的UITextField中经常需要输入完文字后隐藏软键盘,要实现着一点要让View的Controller实现UITextFieldDelegate代理,然后编写相应的代码. #import ...

  7. sorl6.0+jetty+mysql

    sorl6.0+jetty+mysql搭建solr服务 1.下载solr 官网:http://lucene.apache.org/solr/ v2.目录结构如下 v3.启动solr(默认使用jetty ...

  8. 谈谈Facebook的聊天系统架构

    今天看到一份 Facebook 公司 2009 年的 Slideshow, 介绍它的聊天系统架构, 其中的一张图结构非常清晰, 所以我对这张图谈谈我的看法. Web Tier: 用 PHP 开发, 聊 ...

  9. 基于visual Studio2013解决C语言竞赛题之0304整除数

      题目 解决代码及点评 按照题目要求,判断数值对3和5取模的结果,是否为0 #include <stdio.h> #include <stdlib.h> void m ...

  10. 浅谈独立使用NDK编译库文件(Android)

    阅读前准备 这是一篇相对入门的文章.文中会涉及到少许NDK的知识,但个人认为对初学者来说都相对比较实用,因为都是在平时项目中遇到的(目前自己也是初学者).一些其他高深的技术不再本文探讨范围之内(因为我 ...