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记录这个数是否出现以及出现了几次,用过一次-1,dfs寻找最大长度,注意开始的两个都为0的情况,否则会超时,还有我把b开成全局变量一直wa,wa到哭啊啊啊,最后改成局部变量就过了;

AC代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. long long a[1005];
  4. int n,vis[1005];
  5. map<long long,int>mp;
  6. int ans=2;
  7. int dfs(long long x,long long y,int num)
  8. {
  9. long long b=x+y;
  10. if(mp[b]){
  11. mp[b]--;
  12. dfs(y,b,num+1);
  13. mp[b]++;
  14. return 0;
  15. }
  16. ans=max(ans,num);
  17. return 0;
  18. }
  19. int main()
  20. {
  21. scanf("%d",&n);
  22. for(int i=1;i<=n;i++)
  23. {
  24. scanf("%I64d",&a[i]);
  25. mp[a[i]]++;
  26. }
  27. for(int i=1;i<=n;i++)
  28. {
  29. for(int j=1;j<=n;j++)
  30. {
  31. if(i!=j)
  32. {
  33. if(a[i]==0&&a[j]==0)
  34. {
  35. ans=max(ans,mp[0]);
  36. }
  37. else {
  38. mp[a[i]]--;
  39. mp[a[j]]--;
  40. dfs(a[i],a[j],2);
  41. mp[a[j]]++;
  42. mp[a[i]]++;
  43. }
  44. }
  45. }
  46. }
  47. cout<<ans<<"\n";
  48. return 0;
  49. }

codeforces 633D D. Fibonacci-ish(dfs+暴力+map)的更多相关文章

  1. ACM: FZU 2107 Hua Rong Dao - DFS - 暴力

    FZU 2107 Hua Rong Dao Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  2. hdu 5612 Baby Ming and Matrix games(dfs暴力)

    Problem Description These few days, Baby Ming is addicted to playing a matrix game. Given a n∗m matr ...

  3. hdu 1010 Tempter of the Bone(dfs暴力)

    Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...

  4. ACM: Gym 100935G Board Game - DFS暴力搜索

    Board Game Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u  Gym 100 ...

  5. NOIP 2002提高组 选数 dfs/暴力

    1008 选数 2002年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 已知 n 个整数 x1,x2,…, ...

  6. codeforces 615 B. Longtail Hedgehog (DFS + 剪枝)

    题目链接: codeforces 615 B. Longtail Hedgehog (DFS + 剪枝) 题目描述: 给定n个点m条无向边的图,设一条节点递增的链末尾节点为u,链上点的个数为P,则该链 ...

  7. Codeforces 600 E. Lomsat gelral (dfs启发式合并map)

    题目链接:http://codeforces.com/contest/600/problem/E 给你一棵树,告诉你每个节点的颜色,问你以每个节点为根的子树中出现颜色次数最多的颜色编号和是多少. 最容 ...

  8. Codeforces Round #313 (Div. 1) B. Equivalent Strings DFS暴力

    B. Equivalent Strings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/559 ...

  9. Codeforces Round #286 (Div. 2)B. Mr. Kitayuta's Colorful Graph(dfs,暴力)

    数据规模小,所以就暴力枚举每一种颜色的边就行了. #include<iostream> #include<cstdio> #include<cstdlib> #in ...

随机推荐

  1. mysql 与mongodb的特点与优劣

    首先我们来分析下mysql 与mongodb的特点与优劣.下面是我以前做的ppt的部分截图. 再来分析下应用场景,a.如果需要将mongodb作为后端db来代替mysql使用,即这里mysql与mon ...

  2. 删除路径下的文件以及子路径。(范例“E:/www/”)

    /// <summary> /// 删除路径下的文件以及子路径. /// </summary> /// <param name="dir">目标 ...

  3. 卸载系统自带libevent

    rpm -qa|grep libevent yum remove libevent* 或 rpm -e --nodeps --allmatches libevent*

  4. mxnet编译问题手记

    MXNet在64位Win7下的编译安装:https://www.cnblogs.com/noahzn/p/5506086.html http://blog.csdn.net/Jarvis_wxy/ar ...

  5. vue2 本地安装

  6. 在Linux中显示日历(cal)

    cal 2013    显示2013年整年日历 cal 7 2013  显示2013年 7 月 日历

  7. MySQL 下 ROW_NUMBER / DENSE_RANK / RANK 的实现

    原文链接:http://hi.baidu.com/wangzhiqing999/item/7ca215d8ec9823ee785daa2b MySQL 下 ROW_NUMBER / DENSE_RAN ...

  8. Android代码绘制虚线、圆角、渐变效果图

    drawable文件夹放置动画/形状/选择器等属性文件,唯一的drawable文件名,不允许写错和拼错,否则运行报错.drawable文件夹底下的xml文件可以包括的标签共18个:animation- ...

  9. PHP网站在Linux服务器上面的安全配置

    本文详细总结了PHP网站在Linux服务器上面的安全配置,包含PHP安全.mysql数据库安全.web服务器安全.木马查杀和防范等,很好很强大很安全. PHP安全配置 1. 确保运行php的用户为一般 ...

  10. 设置某个字段为null

    update 表名 set 字段名=null WHERE 条件如下例子 update dwtz set ggid=null WHERE name_of_invested_company='浙江海翔药业 ...