Yash has recently learnt about the Fibonacci sequence and is very excited about it. He calls a sequence Fibonacci-ish if
- the sequence consists of at least two elements
- f0 and f1 are arbitrary
- 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).
OutputPrint the length of the longest possible Fibonacci-ish prefix of the given sequence after rearrangement.
Examples3 1 2 -1
3
5 28 35 7 14 21
4
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.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
map<ll,int>num_cp;
map<pair<ll,ll>,int> is_in;
const int maxn=1010;
ll val[maxn],val_tmp[maxn];
/*
这道题告诉了我map赋值的复杂度,最起码也是n,这样就会
必定超时,所以我们把去掉的记录下来,然后再加回去就好了
还有一个优化的问题,我想的是只要是找过头就可以直接
ans-2+map[a,b],但是没有考虑后面可能和前面的重复,没有标记,
所以导致答案变大
***最重要的一点,看见1e9,1e18,这种数据不要慌,有可能这个
数据本身就是指数性的增长,所以复杂度会比我们预想的低好几个
数量级,
*/
int solve(int i,int j){
ll a=val[i],b=val[j],c=val[i]+val[j];
pair<ll,ll> tmp=make_pair(a,b);
if(is_in.count(tmp)) return 0;
is_in[tmp]=1;
int ans=2,cnt=0;
val_tmp[cnt++]=a,val_tmp[cnt++]=b;
num_cp[a]--,num_cp[b]--;
while(num_cp[c]>0){
ans++;
num_cp[c]--;
val_tmp[cnt++]=c;
a=b;
b=c;
tmp=make_pair(a,b);
is_in[tmp]=1;
c=a+b;
}
for(int k=0;k<cnt;k++){
num_cp[val_tmp[k]]++;
}
return ans;
}
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%lld",&val[i]);
num_cp[val[i]]++;
}
int ans=2;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i==j) continue;
ans=max(ans,solve(i,j));
}
}
printf("%d\n",ans);
return 0;
}