Fibonacci-ish

Yash has recently learnt about the Fibonacci sequence and is very excited about it. He calls a sequence Fibonacci-ish if

  1. the sequence consists of at least two elements
  2. f0 and f1 are arbitrary
  3. fn + 2 = fn + 1 + fn for all n ≥ 0.

You are given some sequence of integers a1, a2, ..., an. Your task is rearrange elements of this sequence in such a way that its longest possible prefix is Fibonacci-ish sequence.

Input

The first line of the input contains a single integer n (2 ≤ n ≤ 1000) — the length of the sequence ai.

The second line contains n integers a1, a2, ..., an (|ai| ≤ 109).

Output

Print the length of the longest possible Fibonacci-ish prefix of the given sequence after rearrangement.

Examples

Input
3
1 2 -1
Output
3
Input
5
28 35 7 14 21
Output
4

Note

In the first sample, if we rearrange elements of the sequence as  - 1, 2, 1, the whole sequence ai would be Fibonacci-ish.

In the second sample, the optimal way to rearrange elements is , 28.

从已知元素中寻找最长斐波那契长度。

个人非常喜欢的一道题,比较综合。用到了一些套路,比如用map标记大数,以及加上pair来标记一组数。

开始有往dp方向考虑,想先给数列排个序,然后依次往右找。但是两数相加不一定会变大,和可能会在左边,比如本题负数情况。

再一个每次寻找下一个值时,都要重新使用标记,为了防止后效性,使用递归来处理。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<set>
#include<map>
#include<algorithm>
#define MAX 1005
#define INF 0x3f3f3f3f
using namespace std; int a[MAX],c;
map<int,int> mp;
map<pair<int,int>,int> mmp;
void dfs(int x,int y){ //递归寻找 if(mp[x+y]>) {
c++;
mp[x+y]--;
dfs(y,x+y);
mp[x+y]++;
}
}
int main()
{
int n,i,j;
scanf("%d",&n);
for(i=;i<=n;i++){
scanf("%d",&a[i]);
mp[a[i]]++; //大数标记
}
int max=;
for(i=;i<=n;i++){
for(j=;j<=n;j++){ //考虑负数情况
if(i!=j){
if(mmp[make_pair(a[i],a[j])]) continue;
mmp[make_pair(a[i],a[j])]=; //标记一对数,优化防T
c=;
mp[a[i]]--;
mp[a[j]]--;
dfs(a[i],a[j]);
mp[a[i]]++;
mp[a[j]]++;
if(c>max) max=c;
}
}
}
printf("%d\n",max);
return ;
}

CodeForces - 633D Fibonacci-ish 大数标记map+pair的使用的更多相关文章

  1. codeforces 633D D. Fibonacci-ish(dfs+暴力+map)

    D. Fibonacci-ish time limit per test 3 seconds memory limit per test 512 megabytes input standard in ...

  2. Codeforces Round #378 (Div. 2) D. Kostya the Sculptor map+pair

    D. Kostya the Sculptor time limit per test 3 seconds memory limit per test 256 megabytes input stand ...

  3. 2018 ICPC 徐州网络预赛 Features Track (STL map pair)

    [传送门]https://nanti.jisuanke.com/t/31458 [题目大意]有N个帧,每帧有K个动作特征,每个特征用一个向量表示(x,y).两个特征相同当且仅当他们在不同的帧中出现且向 ...

  4. BZOJ 4236 "JOIOJI"(前缀和+map+pair)

    传送门: [1]:BZOJ [2]:洛谷 •题解 定义数组 a,b,c 分别表示 'J' , 'O' , 'I' 的前缀和: 要想使区间 (L,R] 满足条件当且仅当 a[R]-a[L] = b[R] ...

  5. Codeforces Round #486 (Div. 3) C "Equal Sums" (map+pair<>)

    传送门 •题意 给k个数列,从中k个数列中找出任意2个数列 i ,j 使得数列i删除第x个数,和数列j删除第y个数的和相等 若存在,输出 i ,x 和 j,y •思路 每个数列之间的联系为数列的和之间 ...

  6. Codeforces Round #188 (Div. 2) C. Perfect Pair 数学

    B. Strings of Power Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/318/p ...

  7. Military Problem CodeForces - 1006E(dfs搜一下 标记一下)

    题意: 就是有一颗树  然后每次询问 父结点 的 第k个结点是不是他的子嗣...是的话就输出这个子嗣..不是 就输出-1 解析: 突然想到后缀数组的sa 和 x的用法..就是我们可以用一个id标记当前 ...

  8. Codeforces Round #462 (Div. 2) A Compatible Pair

    A. A Compatible Pair time limit per test1 second memory limit per test256 megabytes Problem Descript ...

  9. SCAU1143 多少个Fibonacci数--大菲波数【杭电-HDOJ-1715】--高精度加法--Fibonacci数---大数比较

    /*******对读者说(哈哈如果有人看的话23333)哈哈大杰是华农的19级软件工程新手,才疏学浅但是秉着校科联的那句“主动才会有故事”还是大胆的做了一下建一个卑微博客的尝试,想法自己之后学到东西都 ...

随机推荐

  1. cmake policy

    1 cmake policy是什么? cmake policy可以理解为cmake的语法标准,也就是说,它规定了cmake在解析CMakeLists.txt文件时的行为. 2 cmake policy ...

  2. mac sublime text 3 add ctags plugin

    https://www.smslit.top/2015/11/14/macSTctags-Develop/ ctags插件for sublime text项目和ctags源码项目都在github上.

  3. cocos2d-js添加百度MSSP插屏(通过jsb反射机制)

    1.导入jar包.... 2.修改AndroidManifest.xml文件 添加: <meta-data android:name="BaiduMobAd_APP_ID" ...

  4. Hibernate连接池设置

    在公司第一次做项目放到服务器上测试,发现每隔一段时间不用数据库就连接不上了(以前学过连接池,很久没用就忘了),在myeclipse上的时候没发现,网上搜索才发现是hibernate连接池配置问题. 1 ...

  5. 流畅的python学习笔记第八章:深拷贝,浅拷贝,可变参数

    首先来看赋值,浅拷贝,深拷贝. 一赋值: a=['word',2,3] b=a print id(a),id(b) print [id(x) for x in a] print [id(x) for ...

  6. Eclipse javax.servlet.jsp.PageContext cannot be resolved to a type 错误解决办法

    不要 直接将jsp-api.jar拷贝到lib目录下,而是通过外部jar包引用.项目 右键->Properties->Libraries->Add External JARS-选择 ...

  7. 创建spring管理的自定义注解

    转自: http://blog.csdn.net/wuqiqing_1/article/details/52763372 Annotation其实是一种接口.通过Java的反射机制相关的API来访问A ...

  8. 添加@ControllerAdvice后报错 Failed to invoke @ExceptionHandler method

    首先.单独使用ControllerAdvice 无法正常工作.需要配合@EnableWebMvc 使用. @ControllerAdvice @EnableWebMvc pulbic class Ex ...

  9. graphics基础

  10. 如何用命令行删除EasyBCD开机选择项?

    用硬盘安装Ubuntu方法的windows双系统电脑上面,很多人都是用EasyBCD设置的开机启动选择.所以当我们不需要双系统的时候,或者已经删除双系统后,或者安装双系统失败的情况下,发现电脑的开机启 ...