Coderforces 633d:fibonacci-ish (map+ Violence enumeration)

Source: Internet
Author: User

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

D. Fibonacci-ish

Yash had recently learnt about the Fibonacci sequence and was very excited about it. He calls a sequence fibonacci-ish if

    1. The sequence consists of at least and 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 the sequence in such a-a-on-its-longest-possible is prefix fibonacci-ish.

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 is fibonacci-ish.

In the second sample, the optimal-rearrange elements is,,,, 28.

Test instructions: Give the number of N and find out how long the longest Fibonacci sequence is after they are arbitrarily arranged.

Thought: This question also listens to the elder's explanation only then will do. Because the Fibonacci numbers are huge, the Fibonacci numbers in 1e9 are relatively small,

So brute force enumeration, the more ingenious is to use a map to save a number has not appeared, and if it is a lot of 0 to add a special sentence, otherwise it may be timed out.

1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <iostream>5#include <map>6 using namespacestd;7 #defineN 10108 9Map <int,int>MP;Ten intA[n]; One intTemp[n]; A  - intMain () - { the     intN; -CIN >>N; - mp.clear (); -      for(inti =0; I < n; i++){ +scanf"%d", A +i); -mp[a[i]]++; +         //the number of occurrences A     } at     intAns =2, now =0, CNT, x, y, Z; -      for(inti =0; I < n; i++){ -          for(intj =0; J < N; J + +){ -             if(i = = j)Continue; -x = A[i], y =A[j]; -             if(x = =0&& y = =0 ){ in                 //two are 0, directly judge the number of 0, because 0+0=0 -                 if(mp[0] > ans) ans = mp[0]; to                 Continue; +             } -CNT =0; themp[x]--; mp[y]--; *temp[cnt++] =x; $temp[cnt++] =y;Panax Notoginseng             //first remove the number you want to add from the map, avoid duplication, store it with temp, and then put it back . -              while(Mp[x+y] >0){ thez = x +y; +mp[z]--; Atemp[cnt++] =Z; thex =y; +y =Z; -             } $              for(inti =0; I < CNT; i++) $mp[temp[i]]++; -             if(cnt > ans) ans =CNT; -         } the     } -printf"%d\n", ans);Wuyi     return 0; the}

Coderforces 633d:fibonacci-ish (map+ Violence enumeration)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.