hihocoder#1239 : Fibonacci(递推DP)

来源:互联网 发布:温莎公爵间谍 知乎 编辑:程序博客网 时间:2024/05/22 11:37

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

Given a sequence {an}, how many non-empty sub-sequence of it is a prefix of fibonacci sequence.

A sub-sequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.

The fibonacci sequence is defined as below:

F1 = 1, F2 = 1

Fn = Fn-1 + Fn-2, n>=3

输入

One line with an integer n.

Second line with n integers, indicating the sequence {an}.

For 30% of the data, n<=10.

For 60% of the data, n<=1000.

For 100% of the data, n<=1000000, 0<=ai<=100000.

输出

One line with an integer, indicating the answer modulo 1,000,000,007.

样例提示

The 7 sub-sequences are:

{a2}

{a3}

{a2, a3}

{a2, a3, a4}

{a2, a3, a5}

{a2, a3, a4, a6}

{a2, a3, a5, a6}


样例输入
62 1 1 2 2 3
样例输出
7
思路:因为a[i]<=1e5,所以a[i]可以用一个下标表示其在Fibonacci中的位置。用d[i][j]表示以a[i]结尾且a[i]下标为j的子序列个数。

#include<bits/stdc++.h>using namespace std;const int MAX=1e6+10;const int MOD=1e9+7;int A[MAX],v[MAX];      //v[i]表示Fibonacci数i的下标int f[30],n,d[MAX][30];int main(){    scanf("%d",&n);    for(int i=1;i<=n;i++)scanf("%d",&A[i]);    f[1]=f[2]=1;    for(int i=3;f[i-2]+f[i-1]<=1e5;i++)    {        f[i]=f[i-1]+f[i-2];        v[f[i]]=i;    }    memset(d,0,sizeof d);    for(int i=1;i<=n;i++)    {        for(int j=1;j<30;j++)d[i][j]=d[i-1][j];        if(A[i]==1)        {            d[i][1]=(d[i][1]+1)%MOD;            d[i][2]=(d[i][2]+d[i-1][1])%MOD;        }        else d[i][v[A[i]]]=(d[i][v[A[i]]]+d[i-1][v[A[i]]-1])%MOD;    }    int ans=0;    for(int i=1;i<30;i++)ans=(ans+d[n][i])%MOD;    cout<<ans<<endl;    return 0;}