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
解题思路:先打表求fib数列前25项(恰好小于10^6),然后DP
dp[i][j]表示到第i位为止,fib数列第j项的表达方法有多少种
dp[i][1]=dp[i-1][1]
dp[i][j]=dp[i-1][j]+dp[i-1][j-1]//dp[i-1][j]表示用当前项替代i-1之前的第j项,dp[i-1][j-1]表示i-1之前的第j-1项加上当前项
一定要取模,否则溢出会WA
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <deque>
#include <vector>
#include <bitset>
#include <cmath>
#include <utility>
#define Maxn 100005
#define Maxm 1000005
#define MOD 1000000007
#define lowbit(x) x&(-x)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PI acos(-1.0)
#define make_pair MP
#define LL long long 
#define Inf (1LL<<62)
#define inf 0x3f3f3f3f
#define re freopen("in.txt","r",stdin)
#define wr freopen("out.txt","w",stdout)
using namespace std;
int fib[30];
LL doc[Maxm][26];
map<int,int> _fib;
void init()
{
	fib[1]=fib[2]=1;
	_fib[1]=1;
	for(int i=3;fib[i-1]+fib[i-2]<=100000;i++)
		fib[i]=fib[i-1]+fib[i-2],_fib[fib[i]]=i;
}
int main()
{
	init();
	int n,num,ans;
	//re;
	while(~scanf("%d",&n))
	{
		ans=0;
		memset(doc,0,sizeof(doc));
		scanf("%d",&num);
		if(num==1)
		{
			ans++;
			doc[0][1]=1;
		}
		for(int i=1;i<n;i++)
		{
			scanf("%d",&num);
			for(int j=1;j<=25;j++)
				doc[i][j]=doc[i-1][j];
			if(_fib[num])
			{
				if(num==1)
				{
					if(doc[i][1])
					//fib数列第二项
					{
						ans+=doc[i-1][1];
						ans++;
						ans%=MOD;
						doc[i][1]=(doc[i-1][1]+1)%MOD;
						doc[i][2]=(doc[i-1][1]+doc[i-1][2])%MOD;
					}
					else
					//fib数列第一项
					{
						ans++;
						doc[i][1]=(doc[i-1][1]+1)%MOD;
					}
				}
				else
				{
					ans+=doc[i-1][_fib[num]-1];//fib数列第三项之后
					ans%=MOD;
					if(doc[i][2])
						doc[i][_fib[num]]=(doc[i-1][_fib[num]]+doc[i-1][_fib[num]-1])%MOD;
				}
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值