描述
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
描述
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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<string>
#include<limits.h>
#include<unordered_map>
#include<stack>
using namespace std;
long long dp[100001];
int main()
{
int n;
scanf("%d",&n);
unordered_map<int,int> Map;
Map[1]=1;Map[2]=2;
int k=3,last=1,pre=2;
while(true) {
int t=last+pre;
if(t>100000) break;
Map[t]=k++;
last=pre;
pre=t;
}
int num;
memset(dp,0,sizeof(dp));
long long res=0;
for(int i=0;i<n;i++) {
scanf("%d",&num);
if(num==1) {
dp[1]++;
res=(res+1)%1000000007;
}
else if(num==2){
if(dp[1]>=2) {
dp[2]=(dp[2]+(dp[1])*(dp[1]-1)/2)%1000000007;
}
}
else if(Map.find(num)!=Map.end()) {
int pos=Map[num];
if(dp[pos-1]>0) {
dp[pos]=(dp[pos]+dp[pos-1])%1000000007;
}
}
}
res=(res+(dp[1]*(dp[1]-1))/2)%1000000007;
for(int i=2;i<100000&&dp[i]!=0;i++) {
res=(res+dp[i])%1000000007;
}
printf("%lld\n",res);
return 0;
}