Manthan, Codefest 16-D. Fibonacci-ish

来源:互联网 发布:柯哀分析文 知乎 编辑:程序博客网 时间:2024/05/16 06:52

原题链接

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
31 2 -1
output
3
input
528 35 7 14 21
output
4

某大牛思路:

:由Fibonaccilog,f0,f1, 
,f0=f1=0,退n3 
,map,,退n3 
map,O(n2log2n)

#include <bits/stdc++.h>#define maxn 1005#define MOD 1000000007using namespace std;typedef long long ll;map<ll, int> m;int num[maxn];vector<int> v;int main(){//freopen("in.txt", "r", stdin);int n;scanf("%d", &n);for(int i = 0; i < n; i++){ scanf("%d", num+i); m[num[i]]++;   }   int ans = m[0];   for(int i = 0; i < n; i++)    for(int j = 0; j < n; j++){    if(i == j || (!num[i] && !num[j]))     continue;    v.clear();    v.push_back(num[i]);    v.push_back(num[j]);    m[num[i]]--;    m[num[j]]--;    int cnt = 2;    ll pre = num[j], k = (ll)num[i] + num[j];    while(m[k]){    cnt++;    m[k]--;    v.push_back(k);        ll d = k;        k += pre;        pre = d;    }    ans = max(ans, cnt);    for(int i = 0; i < v.size(); i++)     m[v[i]]++;    }    printf("%d\n", ans);return 0;} 

:
   





0 0
原创粉丝点击