Fibonacci-ish CodeForces - 633D

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.

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值