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 }

Manthan, Codefest 16 D. Fibonacci-ish的更多相关文章

  1. Manthan, Codefest 16 H. Fibonacci-ish II 大力出奇迹 莫队 线段树 矩阵

    H. Fibonacci-ish II 题目连接: http://codeforces.com/contest/633/problem/H Description Yash is finally ti ...

  2. Manthan, Codefest 16 D. Fibonacci-ish 暴力

    D. Fibonacci-ish 题目连接: http://www.codeforces.com/contest/633/problem/D Description Yash has recently ...

  3. Manthan, Codefest 16(B--A Trivial Problem)

    B. A Trivial Problem time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  4. Manthan, Codefest 16 -C. Spy Syndrome 2

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  5. Manthan, Codefest 16 -A Ebony and Ivory

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  6. Manthan, Codefest 16

    暴力 A - Ebony and Ivory import java.util.*; import java.io.*; public class Main { public static void ...

  7. CF Manthan, Codefest 16 G. Yash And Trees 线段树+bitset

    题目链接:http://codeforces.com/problemset/problem/633/G 大意是一棵树两种操作,第一种是某一节点子树所有值+v,第二种问子树中节点模m出现了多少种m以内的 ...

  8. CF #Manthan, Codefest 16 C. Spy Syndrome 2 Trie

    题目链接:http://codeforces.com/problemset/problem/633/C 大意就是给个字典和一个字符串,求一个用字典中的单词恰好构成字符串的匹配. 比赛的时候是用AC自动 ...

  9. CF Manthan, Codefest 16 B. A Trivial Problem

    数学技巧真有趣,看出规律就很简单了 wa 题意:给出数k  输出所有阶乘尾数有k个0的数 这题来来回回看了两三遍, 想的方法总觉得会T 后来想想  阶乘 emmm  1*2*3*4*5*6*7*8*9 ...

随机推荐

  1. 前端1 — HTML — 更新完毕

    1.首先来了解一个东西 -- W3C标准( 全称是:World Wide Web Consortium ) 万维网联盟(外语缩写:W3C)标准不是某一个标准,而是一系列标准的集合 -- 这个其实每天都 ...

  2. keras房价预测数据集boston_housing.npz文件下载

    github地址: https://github.com/yuxiwang66/boston_housing 码云地址: https://gitee.com/polarisslk/boston_hou ...

  3. HDFS05 NameNode和SecondaryNameNode

    NameNode和SecondaryNameNode(了解) 目录 NameNode和SecondaryNameNode(了解) NN 和 2NN 工作机制 NameNode工作机制 Secondar ...

  4. nuxt使用图片懒加载vue-lazyload

    对于nuxt使用第三方插件的方式大体都是都是一致的,就是在plugins文件夹中新增插件对应的js文件进行配置与操作,然后在nuxt.config.js文件的plugins配置项中引入新建的js文件就 ...

  5. 【Linux】【Shell】【Basic】流程控制

    1. 选择执行: 1.1. if 单分支的if语句: if 测试条件 then 代码分支 fi 双分支的if语句: if 测试条件; then 条件为真时执行的分支 else 条件为假时执行的分支 f ...

  6. 锁对象-条件对象-synchronized关键字

    1 import java.util.concurrent.locks.Condition; 2 import java.util.concurrent.locks.Lock; 3 import ja ...

  7. 【Java基础】Java中new对象的过程

    序言 联系我上次写的关于Java内存的文章,对象访问在 Java 语言中无处不在,是最普通的程序行为,但即使是最简单的访问,也会却涉及 Java 栈.Java 堆.方法区这三个最重要内存区域之间的关联 ...

  8. 使用IntelliJ IDEA创建简单的Spring Boot项目

    方法一: File - New -Project 创建结束后进行测试运行,修改代码如下: package com.springboot.testone; import org.springframew ...

  9. layui-入门

    1.下载layui 2.导入layui <title>开始使用layui</title> <!-- 导入js样式 --> <link rel="st ...

  10. 小飞机可以解决git clone没有返回的问题吗?

    [1]Linux如何使用小飞机? 以ss为例,先下载客户端: https://www.mediafire.com/folder/xag0zy318a5tt/Linux 下载客户端以后,右键把权限中&q ...