http://codeforces.com/problemset/problem/633/D

D. Fibonacci-ish
 

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.

题意:给出 n个数,求出他们任意排列之后最长的斐波那契数列是多长。

思路:这题也是听了师兄的讲解之后才会做的。因为斐波那契数的递增是巨大的,1e9里面的斐波那契数是比较少的,

因此暴力枚举,比较巧妙的是用map来存一个数有没有出现过,并且如果是很多个零的话要加一个特判,不然的话可能会超时。

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
#define N 1010 map <int, int> mp;
int a[N];
int temp[N]; int main()
{
int n ;
cin >> n;
mp.clear();
for(int i = ; i < n; i++){
scanf("%d", a + i);
mp[a[i]]++;
//该数出现的次数
}
int ans = , now = , cnt, x, y, z;
for(int i = ; i < n; i++){
for(int j = ; j < n; j++){
if(i == j) continue;
x = a[i], y = a[j];
if( x == && y == ){
//两个都是0的话,直接判断0的个数,因为0+0=0
if(mp[] > ans) ans = mp[];
continue;
}
cnt = ;
mp[x]--; mp[y]--;
temp[cnt++] = x;
temp[cnt++] = y;
//先把要加的数从map里面删除,避免重复,用temp存放,之后再放回去
while(mp[x+y] > ){
z = x + y;
mp[z]--;
temp[cnt++] = z;
x = y;
y = z;
}
for(int i = ; i < cnt; i++)
mp[temp[i]]++;
if(cnt > ans) ans = cnt;
}
}
printf("%d\n",ans);
return ;
}

Coderforces 633D:Fibonacci-ish(map+暴力枚举)的更多相关文章

  1. CodeForces 742B Arpa’s obvious problem and Mehrdad’s terrible solution (暴力枚举)

    题意:求定 n 个数,求有多少对数满足,ai^bi = x. 析:暴力枚举就行,n的复杂度. 代码如下: #pragma comment(linker, "/STACK:1024000000 ...

  2. HNU 12886 Cracking the Safe(暴力枚举)

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数 ...

  3. POJ-3187 Backward Digit Sums (暴力枚举)

    http://poj.org/problem?id=3187 给定一个个数n和sum,让你求原始序列,如果有多个输出字典序最小的. 暴力枚举题,枚举生成的每一个全排列,符合即退出. dfs版: #in ...

  4. CCF 201312-4 有趣的数 (数位DP, 状压DP, 组合数学+暴力枚举, 推公式, 矩阵快速幂)

    问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次. 2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前. 3. 最高 ...

  5. HDU - 1248 寒冰王座 数学or暴力枚举

    思路: 1.暴力枚举每种面值的张数,将可以花光的钱记录下来.每次判断n是否能够用光,能则输出0,不能则向更少金额寻找是否有能够花光的.时间复杂度O(n) 2.350 = 200 + 150,买350的 ...

  6. POJ 3080 Blue Jeans (字符串处理暴力枚举)

    Blue Jeans  Time Limit: 1000MS        Memory Limit: 65536K Total Submissions: 21078        Accepted: ...

  7. こだわり者いろはちゃん / Iroha's Obsession (暴力枚举)

    题目链接:http://abc042.contest.atcoder.jp/tasks/arc058_a Time limit : 2sec / Memory limit : 256MB Score ...

  8. Codeforces Round #298 (Div. 2) B. Covered Path 物理题/暴力枚举

    B. Covered Path Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/534/probl ...

  9. Codeforces Round #266 (Div. 2)B(暴力枚举)

    很简单的暴力枚举,却卡了我那么长时间,可见我的基本功不够扎实. 两个数相乘等于一个数6*n,那么我枚举其中一个乘数就行了,而且枚举到sqrt(6*n)就行了,这个是暴力法解题中很常用的性质. 这道题找 ...

随机推荐

  1. 关于fastjson用法

    fastjson 是一个性能很好的 Java 语言实现的 JSON 解析器和生成器,来自阿里巴巴的工程师开发. public static final String toJSONString(Obje ...

  2. asp .net 文件浏览功能

    达到的目的是,发布站点后,在站点某个目录下的图片文件可以通过url访问 步骤 1.新建一个网站 注意,不要建立在需要较高访问权限的地方,不要建立空网站 如果是需要较高访问权限的目录,而IIS本身账号的 ...

  3. 命名管道的C#实现

    1.    命名管道简介 "命名管道"或"命名管线"(Named Pipes)是一种简单的进程间通信(I P C)机制,Microsoft Windows NT ...

  4. Win8 Metro(C#)数字图像处理--3.4图像信息熵计算

    原文:Win8 Metro(C#)数字图像处理--3.4图像信息熵计算 [函数代码] /// <summary> /// Entropy of one image. /// </su ...

  5. LoadLibrary方法加载运行DLL库

    最近和另一家公司对接,要求用对方提供的测试程序测试我们做的DLL. 接到对方的测试程序,发现和我们以前调用DLL的方式不太一样.但我稍微看了一会代码也看懂其意思了,一天搞定了. 但其中也遇到些小困惑, ...

  6. oracle data guard配置dg_broker

    https://community.oracle.com/docs/DOC-1007327 本文主要包括以下内容: 1.  配置dg broker,需要完成以下几个工作: 在主备库配置静态监听注册,注 ...

  7. 安卓环境下,通过QT调用jar包

    在安卓上,许多第三方工具都提供jar包.qt可以通过jni来调用jar包.本文通过一个例子,说明安卓上QT调用jar的方式. 工具/原料   qt android jar包 jar包准备   1 ja ...

  8. 4月份本周超过 10 款最新免费 jQuery 插件

    分享 <关于我> 分享  [中文纪录片]互联网时代                 http://pan.baidu.com/s/1qWkJfcS 分享 <HTML开发MacOSAp ...

  9. Hibernate注解(一):基本注解

    在Hibernate中使用注解,主要是为了替代映射文件,完成“类到表,属性到字段”的映射.JPA提供了一套功能强大的注解.Hibernate直接使用了JPA的这套注解.当然,对于JPA中的一些不足,H ...

  10. Python连载9-setup环境变量&os模块

    一.timeit包(上接连载9) 1.我们对于timeit函数,可采取如下例子: h = ''' def doTt(num1): for i in range(num1): print(i) ''' ...