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

// Java版本
import java.awt.im.InputContext;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner; public class Main {
/* 2
0 0
0 3 1.000 1.000 5.000 */
static HashSet<Integer> fibSet=new HashSet<Integer>();
static HashMap<Integer, Integer> fibMap=new HashMap<Integer, Integer>();
static HashMap<Integer, Integer> fibMap2=new HashMap<Integer, Integer>();
public static void fib(){
fibMap.put(1, (int) 1);
fibMap.put(2, (int) 1);
fibSet.add((int) 1);
fibMap2.put((int) 1, 1);
for(int i=3; i<50; i++){ fibMap.put(i, fibMap.get(i-1)+fibMap.get(i-2));
fibMap2.put(fibMap.get(i-1)+fibMap.get(i-2), i);
fibSet.add(fibMap.get(i-1)+fibMap.get(i-2));
}
//System.out.println("fibmap"); }
//如果有a之前的所有
public static boolean hasPre(int a, HashMap<Integer, Integer> nums){
int k=fibMap2.get(a); boolean result=true;
for(int i=k-1;i>2; i--){ if(nums.get(fibMap.get(i)) !=null &&nums.get(fibMap.get(i)) >0){
continue;
}else{
result=false;
break;
}
} if(result&& nums.get(1)!=null &&nums.get(1)>1 ){
result= true;
}else{
result= false;
} return result; } public static int precount(int a, HashMap<Integer, Integer> nums){
long count=1;
int k=fibMap2.get(a); for(int i=k-1;i>2; i--){
count=count*nums.get(fibMap.get(i)); }
count*=(nums.get(1)*(nums.get(1)-1)/2);
return (int) (count%1000000007); }
public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
int n=scanner.nextInt();
int a[] = new int[n];
for(int i=0; i<n; i++){
a[i]=scanner.nextInt(); }
fib();
//fib计数
HashMap<Integer, Integer> nums=new HashMap<Integer, Integer>();
boolean has1=false;
long count=0;
for(int i=0; i<n; ++i){
//如果等于1,那就好弄
//System.out.println(a[i]+""+fibSet.contains((long)a[i]) );
if(a[i]==1){
if(nums.containsKey(1)){
count=count+nums.get(1)+1;
nums.put(1, nums.get(1)+1); }else{
nums.put(1, 1);
count+=1;
}
has1=true; }else if(has1&& nums.get(1)>1&& fibSet.contains(a[i]) && hasPre(a[i],nums)){ count=(count+precount(a[i],nums))%1000000007;
if(nums.containsKey(a[i])){
nums.put(a[i], nums.get(a[i])+1);
}else{
nums.put(a[i], 1);
} }
//System.out.println(nums);
} System.out.println(count);
scanner.close();
} }

题目3 : Fibonacci的更多相关文章

  1. 山东省第七届ACM省赛------Fibonacci

    Fibonacci Time Limit: 2000MS Memory limit: 131072K 题目描述 Fibonacci numbers are well-known as follow: ...

  2. HPU--1221 Fibonacci数列

    题目描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时,Fn也非常大,现在我们想知道,Fn除以10007的余数是多少. 输入 输入包含一个整数n. ...

  3. 网易编程题——Fibonacci数列

    题目描述 Fibonacci数列是这样定义的: F[0] = 0 F[1] = 1 for each i ≥ 2: F[i] = F[i-1] + F[i-2] 因此,Fibonacci数列就形如:0 ...

  4. 1221: Fibonacci数列 [数学]

    1221: Fibonacci数列 [数学] 时间限制: 1 Sec 内存限制: 128 MB 提交: 116 解决: 36 统计 题目描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn- ...

  5. 2017网易---Fibonacci数列

    题目描述 Fibonacci数列是这样定义的:F[0] = 0F[1] = 1for each i ≥ 2: F[i] = F[i-1] + F[i-2]因此,Fibonacci数列就形如:0, 1, ...

  6. hdu4099 Revenge of Fibonacci

    题意:给定fibonacci数列,输入前缀,求出下标.题目中fibonacci数量达到100000,而题目输入的前缀顶多为40位数字,这说明我们只需要精确计算fibinacci数前40位即可.查询时使 ...

  7. Fibonacci数列(找规律)

    题目描述 Fibonacci数列是这样定义的:F[0] = 0F[1] = 1for each i ≥ 2: F[i] = F[i-1] + F[i-2]因此,Fibonacci数列就形如:0, 1, ...

  8. 算法设计与分析 1.2 不一样的fibonacci数列

    ★题目描述 fibonacci 数列的递推公式是F(n) = F(n-1) + F(n-2)(n >= 2 且 n 为整数). 将这个递推式改为F(n) = aF(n-1) + bF(n-2)( ...

  9. [HDU3117]Fibonacci Numbers

    题目:Fibonacci Numbers 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3117 分析: 1)后四位可以用矩阵快速幂解决.$T= \left ...

随机推荐

  1. Python爬取中国天气网

    Python爬取中国天气网 基于requests库制作的爬虫. 使用方法:打开终端输入 “python3 weather.py 北京(或你所在的城市)" 程序正常运行需要在同文件夹下加入一个 ...

  2. SOAP、SOCKET协议

    一.SOAP( SOAP:Simple Object Access Protocol) 简单对象访问协议,简单对象访问协议(SOAP)是一种轻量的.简单的.基于 XML 的协议,它被设计成在 WEB ...

  3. 【poj1149】 pigs 网络流最大流问题

    描述 Description 尼克在一家养猪场工作,这家养猪场共有M间锁起来的猪舍,由于猪舍的钥匙都给了客户,所以尼克没有办法打开这些猪舍,客户们从早上开始一个接一个来购买生猪,他们到达后首先用手中的 ...

  4. RestAPI的实现

    转自:http://blog.csdn.net/yanical/article/details/7856670 Rest的作者认为计算机发展到现在,最大的成就不是企业应用,而是web,是漫漫无边的互联 ...

  5. kaptcha Java验证码

    原文:http://www.cnblogs.com/chizizhixin/p/5311619.html 在项目中经常会使用验证码,kaptcha 就一个非常不错的开源框架,分享下自己在项目中的使用: ...

  6. Multithreading and Grand Central Dispatch on iOS for Beginners Tutorial

    Have you ever written an app where you tried to do something, and there was a long pause while the U ...

  7. A folder failed to be renamed or moved--安装Android SDK的问题

    对于Android是一直想学却一直未学,行动跟不上想法.现在,终于付诸于行动了. 首先,我找的第一个Android的资料是大话企业级Android,前阵子刚看完大话设计模式,通俗易懂,还是比较喜欢这一 ...

  8. python 常用的模块(collections)转

    collections是Python内建的一个集合模块,提供了许多有用的集合类. namedtuple 我们知道tuple可以表示不变集合,例如,一个点的二维坐标就可以表示成: >>> ...

  9. 拉格朗日对偶与kkt条件

  10. 遍历Enumeration

    版权声明:http://blog.csdn.net/qq924862077/ Enumeration(枚举)接口的作用和Iterator类似,只提供了遍历Vector和HashTable类型集合元素的 ...