D. Fibonacci-ish
time limit per test

3 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

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一一对应下数值,开个数组记录各个值的个数,然后根据斐波那契数列依次推下个数字

在数组中查找,其中开始为0,0需要特判否则复杂度n3

 1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<math.h>
6 #include<queue>
7 #include<map>
8 using namespace std;
9 const long long N=1e9+7;
10 typedef long long LL;
11 LL vv[25];
12 LL MM[1005];
13 int NN[1005];
14 int KK[1005];
15 map<LL,LL>my;
16 LL quickmi(long long a,long long b);
17 int main(void)
18 {
19 int i,j,k;
20 while(scanf("%d",&k)!=EOF)
21 {memset(NN,0,sizeof(NN));
22 for(i=0; i<k; i++)
23 {
24 scanf("%I64d",&MM[i]);
25 }
26 int cnt=1;sort(MM,MM+k);my[MM[0]]=1;
27 NN[1]++;
28 for(i=1; i<k; i++)
29 {
30 if(MM[i]!=MM[i-1])
31 cnt++;
32 NN[cnt]++;
33 my[MM[i]]=cnt;
34 }int maxx=2;
35 for(i=0; i<k; i++)
36 {
37 for(j=0; j<k; j++)
38 {int sum=2;
39 if(i!=j)
40 {int KK[1005]={0};LL p=MM[i];LL q=MM[j];
41 if(p==0&&q==0)
42 {
43 sum=NN[my[0]];
44 }
45 else {KK[my[p]]++;KK[my[q]]++;
46 while(true)
47 {
48 LL ans=p+q;
49 KK[my[ans]]++;
50 if(KK[my[ans]]<=NN[my[ans]])
51 sum++;
52 else break;
53 p=q;q=ans;
54 }}
55 if(sum>maxx)
56 maxx=sum;}
57 }
58 }printf("%d\n",maxx);
59 }
60 return 0;
61 }
62
63 LL quickmi(long long a,long long b)
64 {
65 LL sum=1;
66 while(b)
67 {
68 if(b&1)
69 sum=(sum*a)%(N);
70 a=(a*a)%N;
71 b/=2;
72 }
73 return sum;
74 }

最新文章

  1. C#详解format函数,各种格式化
  2. mysql memory表性能测试以及使用场景
  3. Dynamic CRM 2013学习笔记(四十一)流程4 - 异步工作流(Workflow)用法图解
  4. Qt Charts
  5. if条件语句
  6. [视频]物联网应用-ARM mbed-来至MultiTech Systems的解决方案
  7. JPEG最优压缩参数试验【光影魔术手VS Image Optimizer】
  8. 解决Oracle clob字段数据过大问题
  9. 《Velocity 模板使用指南》中文版[转]
  10. qsort的另类玩法,无聊写着耍耍
  11. css中设置div水平居中,margin:0px auto无用的情况
  12. BZOJ3537 : [Usaco2014 Open]Code Breaking
  13. Python模块——HashLib与base64
  14. android ------ Emulator: error: x86 emulation currently requires hardware acceleration
  15. sqlserver中的CHARINDEX用法
  16. ActiveMQ P2P版的HelloWorld
  17. JavaScript高级编程———JSON
  18. PWN入门
  19. Tomcat 部署一工程时Deploy Location 为什么 是 INVALID
  20. Python下尝试实现图片的高斯模糊化

热门文章

  1. Apache RocketMQ分布式消息传递和流数据平台及大厂面试宝典v4.9.2
  2. TOMCAT 搭建
  3. Java的那些小事
  4. day07 ORM中常用字段和参数
  5. nexus 私服 拉不了 jar 包,报 Not authorized
  6. my42_Mysql基于ROW格式的主从同步
  7. 关于python中的随机种子——random_state
  8. HDC2021技术分论坛:进程崩溃/应用卡死,故障频频怎么办?
  9. Linux下安装数据库sqlite3
  10. 前端浅谈-协议相关(DNS协议)