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. meaven环境变量配置

    首先,先到官网去下载maven.这里是官网的地址:http://maven.apache.org/download.cgi  请选择最新的版本下载,这里咱们下载的是apache-maven-3.1.1 ...

  2. 我的Android进阶之旅------>Handlerr.removeCallbacksAndMessages(null)的作用

    今天都到一段代码,在onDestroy()方法中,使用了下面的代码: mHandler.removeCallbacksAndMessages(null); 一开始我完全看不懂,我为什么参数是null, ...

  3. 关联android-support-v4源码关联不上的解决办法

    在android项目中查看android-support-v4中的源码提示“Android Private Libraries which does not modified source attac ...

  4. c语言高速推断一个数是偶数还是奇数

    #include <stdio.h> int main() { int a; while(1) { printf("please input the number:\n" ...

  5. java基础语言 运算符

    /* ++,--运算符的使用: 单独使用: 放在操作数的前面和后面效果一样.(这种用法是我们比较常见的) 参与运算使用: 放在操作数的前面,先自增或者自减,然后再参与运算. 放在操作数的后面,先参与运 ...

  6. 《锋利的jQuery》打造个性网站整合

    搜索框文字效果 网页换肤 导航效果 广告效果 添加超链接提示 产品横向滚动效果 光标滑动列表效果 产品详细页面效果(放大镜,遮罩,选项卡,评分等) 1.搜索框文字效果 <!DOCTYPE htm ...

  7. <关于JSP技术>运行机制及语法概述(附对本次同济校内ACM选拔赛决赛的吐槽)

    (一)JSP运行的机制 JSP是一种建立在Servlet规范功能之上的动态网页技术,它们都是在通常的网页文件中嵌入脚本代码,用于产生动态内容,不过和ASP不同的是JSP文件中嵌入的是Java代码和JS ...

  8. 算法(Algorithms)第4版 练习 1.3.21

    方法实现: //1.3.21 /** * find if some node in the list has key as its item field * * @param list the lin ...

  9. ARP之windows下的ARP命令

    ARP之windows下的ARP命令 arp -a 查看当前电脑上的ARP映射表.可以看到当前的ARP的映射关系是动态的还是静态的. arp -s w.x.y.z aa-bb-cc-dd-ee-ff ...

  10. python读写txt大文件

    直接上代码: import easygui import os path = easygui.fileopenbox()#path是打开的文件的全路径 if path:#如果选择打开文件,没有选择取消 ...