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

    1.MyBatis是持久层框架 什么是持久化: 狭义:把数据永久性的保存到数据当中 广义:针对于数据库的所有操作都称为持久化操作,CreateReadUpdateDelete操作 2.有哪些持久层框架 ...

  2. Oracle完整的压测记录

    问题描述:对oracle进行一次完整的数据压测,从制造数据到压测的过程,路上踩了一些坑,现在分享出来 1.下载swingbenh软件,一个比较好用的oracle压测软件 2.利用oewizard工具( ...

  3. 巩固javaweb第十天

    巩固内容: HTML <meta> 元素 meta标签描述了一些基本的元数据. <meta> 标签提供了元数据.元数据也不显示在页面上,但会被浏览器解析. META 元素通常用 ...

  4. Java项目发现==顺手改成equals之后,会发生什么?

    最近发生一件很尴尬的事情,在维护一个 Java 项目的时候,发现有使用 == 来比较两个对象的属性, 于是顺手就把 == 改成了 equals.悲剧发生...... == 和 equals 的区别 = ...

  5. day10设置文件权限

    day10设置文件权限 yum复习 1.修改IP [root@localhost ~]# sed -i 's#.200#.50#g' /etc/sysconfig/network-scripts/if ...

  6. @Data 注解引出的 lombok

    今天在看代码的时候, 看到了这个注解, 之前都没有见过, 所以就查了下, 发现还是个不错的注解, 可以让代码更加简洁. 这个注解来自于 lombok,lombok 能够减少大量的模板代码,减少了在使用 ...

  7. linux 定时导出sql查询结果文件

    如果想在服务器端生成sql查询结果的txt文件. 大体思路就是: 1.创建一个到处txt文件的sql脚本. set ARRAYSIZE 50 --从数据库往客户端一次发送记录数 set linesiz ...

  8. Android 实现微信QQ分享以及第三方登录

    集成准备 在微信开放平台创建移动应用,输入应用的信息,包括移动应用名称,移动应用简介,移动应用图片信息,点击下一步,选择Android 应用,填写信息提交审核. 获取Appkey 集成[友盟+]SDK ...

  9. Linux基础命令---slabtop

    slabtop slabtop实时显示详细的内核板条缓存信息.它显示按所列排序条件之一排序的顶级缓存的列表.它还会显示一个统计信息头,其中填充了板坯层信息. 此命令的适用范围:RedHat.RHEL. ...

  10. 3.1 go context代码示例

    context.WithCancel返回两个有关联的对象,ctx与cancel,调用cancel发送一个空struct给ctx,ctx一旦接收到该对象后,就终止goroutine的执行;ctx是线程安 ...