hihoCoder 1239 Fibonacci

#1239 : Fibonacci

时间限制: 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}


样例输入
6
2 1 1 2 2 3
样例输出
7


线性DP,0<=ai<=100000,所以fib的项数不会超过30.使用DP的时候,开数组最好开第二维30,我第一次开了35,结果超内存。

dp数组要用Long Long 中间过程会超int.应及时取模。


#include <stdio.h>
#include <string.h>
#include <algorithm>
#define N 1000005
#define MOD 1000000007
using namespace std;
int a[N];
long long dp[N][30];
int fib[30];
void getfibtable()
{
        fib[0]=1,fib[1]=1;
                for(int i=2;i<30;i++)
                        fib[i]=fib[i-1]+fib[i-2];
}
int main()
{
        int n;
        getfibtable();
        while(scanf("%d",&n)>0)
        {
                for(int i=0;i<30;i++)
                        dp[0][i]=0;
                for(int i=1;i<=n;i++)
                        scanf("%d",&a[i]);
                for(int i=1;i<=n;i++)
                {
                        for(int j=0;j<30;j++)
                                dp[i][j]=dp[i-1][j];
                        int k=lower_bound(fib,fib+30,a[i])-fib;
                        if(fib[k]==a[i])
                        {
                                if(a[i]==1)
                                {
                                        dp[i][k+1]=(dp[i][k+1]+dp[i-1][0])%MOD;
                                        dp[i][k]=(dp[i][k]+1)%MOD;
                                }
                                else
                                        dp[i][k]=(dp[i][k]+dp[i-1][k-1])%MOD;
                        }
                }
                long long sum=0;
                for(int i=0;i<30;i++)
                        sum+=dp[n][i]%MOD;
                printf("%lld\n",sum%MOD);
        }
        return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值