(DP) hihocoder-1239 Fibonacci

Source: Internet
Author: User

original title link:http://hihocoder.com/problemset/problem/1239

Test Instructions: for a sequence, there are several sub-sequences that form the Fibonacci sequence.

Analysis: We can dynamically add results in the process of statistics.

Since there are two 1, we need to make some distinctions, we first define m[0] to denote the number of Fibonacci numbers ending with the first 1, m[1] represents the number of Fibonacci sequences ending with the second 1, similarly, M[i] (I>1,i represents Fibonacci numbers) Represents the number of Fibonacci sequences ending with Fibonacci (i).

such as this example 1 1 2 1 2 3 4 5.

① assumes the beginning of the 1, when m[0]=1.

② then another 1, at this time because M[0]=1 already had a sequence ending with the first 1, so m[1]+=m[0], and because 1 alone can become a series, so m[0]+=1.

③ next 2, we need to see the Fibonacci number in front of him, which is the second 1, so m[2]+=m[1].

④ then is again 1, like ②, first add already have, then add own new. M[1]+=m[0],m[0]+=1.

⑤ is still 2, like ③, M[2]+=m[1].

⑥ is then 3, similar to ③⑤, M[3]+=m[2].

⑦ this time is 4, found not Fibonacci number, skip directly.

⑧ finally 5, still similar, m[4]+=m[3].

Code:

1#include <Set>2#include <map>3#include <list>4#include <cmath>5#include <queue>6#include <vector>7#include <bitset>8#include <string>9#include <cctype>Ten#include <cstdio> One#include <cstring> A#include <cstdlib> -#include <iostream> -#include <algorithm> the  - using namespacestd; -  -typedefLong Longll; +typedef unsignedLong Longull; - #defineINF (0X3F3F3F3F) + #defineLNF (0x3f3f3f3f3f3f3f3f) A #defineEPS (1E-8) at intSgnDoubleA) {returnA <-eps? -1: A < EPS?0:1;} -  - //-------------------------- -  -  - Constll mod =1000000007; in  - intfb[ -] = {1,1,2}; toll tb[ -]; +map<int,int>m; - intp; the intN; *  $ voidinit () {Panax Notoginsengm[1] =1; -m[2] =2; the      for(P =3;; p++) { +         if(Fb[p-1] + fb[p-2] >100000) Break; AFB[P] = fb[p-1] + fb[p-2]; theM[FB[P]] =p; +  -     } $ } $  -  -  the voidsolve () { - init ();Wuyi      while(~SCANF ("%d", &N)) { thememset (TB,0,sizeof(TB)); -ll ans =0; Wu         intx; -         intK =0; About          for(inti =0; I < n; i++) { $scanf"%d", &x); -  -             int*lo = Lower_bound (FB, FB +p, x); -             if(*lo! = x)Continue; A             if(x = =1) { +Ans = (ans% mod + tb[0]% MoD)%MoD; thetb[1] = (tb[1]% mod + tb[0]% MoD)%MoD; -tb[0] = (tb[0]% mod +1) %MoD; $Ans = (ans% mod +1) %MoD; the}Else { theTb[m[x]] = (tb[m[x]]% mod + tb[m[x]-1]% MoD)%MoD; theAns = (ans% mod + tb[m[x]-1]% MoD)%MoD; the             } -         } in  theprintf"%lld\n", ans); the     } About  the } the  the intMain () { +  - #ifndef Online_judge theFreopen ("1.in","R", stdin);Bayi     //freopen ("1.out", "w", stdout); the #endif the     //Iostream::sync_with_stdio (false); - solve (); -     return 0; the}

(DP) hihocoder-1239 Fibonacci

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.