CodeForces - 633D Fibonacci-ish —— 斐波那契數列
阿新 • • 發佈:2018-11-12
題意:
有1000個數,問能形成的最長斐波那契數列的長度
思路:
只要確定了數列的前兩項,數列就已經確定了,所以可以列舉前兩項,來找剩下的最長
因為數的絕對值小於1e9,斐波那契數列的增長速度很快,項數很少
用map記錄了一下每個數出現的個數,方便搜尋
要特判0的情況,因為0很多,就退化為n^3的複雜度了
#include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <map> #include <set> #include <stack> #include <queue> #include <string> #include <cstring> #include <algorithm> using namespace std; #define ll long long #define max_ 100010 #define mod 1000000007 #define inf 0x3f3f3f3f int n; ll num[1010]; map<ll,int>mp; void dfs(ll x,ll y,int &ans) { ll k=x+y; if(mp[k]) { mp[k]--; ans++; dfs(y,k,ans); mp[k]++; } } int main(int argc, char const *argv[]) { scanf("%d",&n); int f=0; for(int i=1;i<=n;i++) { scanf("%lld",&num[i]); mp[num[i]]++; if(num[i]==0) f++; } if(f==n) { printf("%d\n",n ); return 0; } int maxx=f; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(i==j) continue; if(num[i]==0&&num[j]==0) continue; int ans=2; mp[num[i]]--; mp[num[j]]--; dfs(num[i],num[j],ans); mp[num[i]]++; mp[num[j]]++; if(ans>maxx) maxx=ans; } } printf("%d\n",maxx ); return 0; }