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

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

  1. // Java版本
  2. import java.awt.im.InputContext;
  3. import java.util.HashMap;
  4. import java.util.HashSet;
  5. import java.util.Scanner;
  6.  
  7. public class Main {
  8. /*
  9.  
  10. 2
  11. 0 0
  12. 0 3
  13.  
  14. 1.000 1.000 5.000
  15.  
  16. */
  17. static HashSet<Integer> fibSet=new HashSet<Integer>();
  18. static HashMap<Integer, Integer> fibMap=new HashMap<Integer, Integer>();
  19. static HashMap<Integer, Integer> fibMap2=new HashMap<Integer, Integer>();
  20. public static void fib(){
  21. fibMap.put(1, (int) 1);
  22. fibMap.put(2, (int) 1);
  23. fibSet.add((int) 1);
  24. fibMap2.put((int) 1, 1);
  25. for(int i=3; i<50; i++){
  26.  
  27. fibMap.put(i, fibMap.get(i-1)+fibMap.get(i-2));
  28. fibMap2.put(fibMap.get(i-1)+fibMap.get(i-2), i);
  29. fibSet.add(fibMap.get(i-1)+fibMap.get(i-2));
  30. }
  31. //System.out.println("fibmap");
  32.  
  33. }
  34. //如果有a之前的所有
  35. public static boolean hasPre(int a, HashMap<Integer, Integer> nums){
  36. int k=fibMap2.get(a);
  37.  
  38. boolean result=true;
  39. for(int i=k-1;i>2; i--){
  40.  
  41. if(nums.get(fibMap.get(i)) !=null &&nums.get(fibMap.get(i)) >0){
  42. continue;
  43. }else{
  44. result=false;
  45. break;
  46. }
  47. }
  48.  
  49. if(result&& nums.get(1)!=null &&nums.get(1)>1 ){
  50. result= true;
  51. }else{
  52. result= false;
  53. }
  54.  
  55. return result;
  56.  
  57. }
  58.  
  59. public static int precount(int a, HashMap<Integer, Integer> nums){
  60. long count=1;
  61. int k=fibMap2.get(a);
  62.  
  63. for(int i=k-1;i>2; i--){
  64. count=count*nums.get(fibMap.get(i));
  65.  
  66. }
  67. count*=(nums.get(1)*(nums.get(1)-1)/2);
  68. return (int) (count%1000000007);
  69.  
  70. }
  71. public static void main(String[] args) {
  72.  
  73. Scanner scanner = new Scanner(System.in);
  74. int n=scanner.nextInt();
  75. int a[] = new int[n];
  76. for(int i=0; i<n; i++){
  77. a[i]=scanner.nextInt();
  78.  
  79. }
  80. fib();
  81. //fib计数
  82. HashMap<Integer, Integer> nums=new HashMap<Integer, Integer>();
  83. boolean has1=false;
  84. long count=0;
  85. for(int i=0; i<n; ++i){
  86. //如果等于1,那就好弄
  87. //System.out.println(a[i]+""+fibSet.contains((long)a[i]) );
  88. if(a[i]==1){
  89. if(nums.containsKey(1)){
  90. count=count+nums.get(1)+1;
  91. nums.put(1, nums.get(1)+1);
  92.  
  93. }else{
  94. nums.put(1, 1);
  95. count+=1;
  96. }
  97. has1=true;
  98.  
  99. }else if(has1&& nums.get(1)>1&& fibSet.contains(a[i]) && hasPre(a[i],nums)){
  100.  
  101. count=(count+precount(a[i],nums))%1000000007;
  102. if(nums.containsKey(a[i])){
  103. nums.put(a[i], nums.get(a[i])+1);
  104. }else{
  105. nums.put(a[i], 1);
  106. }
  107.  
  108. }
  109. //System.out.println(nums);
  110. }
  111.  
  112. System.out.println(count);
  113. scanner.close();
  114. }
  115.  
  116. }

题目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. hashmap hashtable

    作者:付佳豪链接:https://zhuanlan.zhihu.com/p/37607299来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 在面试的时候,java集合最 ...

  2. 解决android模拟器连接本机服务器”Connection refused”问题

      在本机用模拟器连接 localhost 的服务器不成功,经查询是我反了一个小错误. android 模拟器其本身的localhost就是它自己的ip,而如果我要连接本机的localhost则需要将 ...

  3. linux-启动脚本-souce与sh

    source:        在当前shell程序中执行,  因此当前shell程序中的变量和环境变量,均可见.   执行的脚本,能更新到当前shell程序. sh:            开启一个新 ...

  4. tq2440实验手册qt编译问题

    转载:http://blog.sina.com.cn/s/blog_6182b82201015ym1.html 编译qtopia最好使用的是低版本的gcc和g++. 举个简单的例子在qtopia的源代 ...

  5. openfire源码研究笔记:对设计模式及原则的学习

    原文:http://blog.csdn.net/jinzhencs/article/details/50522105 一.拦截器的实现 地点:   package org.jivesoftware.o ...

  6. 【hibernate/JPA】对实体类的的多个字段建立唯一索引,达到复合主键的效果【spring boot】注解创建唯一索引和普通索引

    对实体类的的多个字段建立唯一索引,达到复合主键的效果 package com.sxd.swapping.domain; import lombok.Getter; import lombok.Sett ...

  7. 改变PS1变量的颜色

    2016.1.11今天学了改变PS1的颜色,怎么增加PS1变量找到文件(.bash_profile),或者bashrc export PS1="\[\e[32;1m\]Test $PWD&g ...

  8. 10 种机器学习算法的要点(附 Python)(转载)

    一.前言 谷歌董事长施密特曾说过:虽然谷歌的无人驾驶汽车和机器人受到了许多媒体关注,但是这家公司真正的未来在于机器学习,一种让计算机更聪明.更个性化的技术 也许我们生活在人类历史上最关键的时期:从使用 ...

  9. Hadoop 变更磁盘的方法总结

    背景说明HDFS文件系统使用一段时间后,可能会出现磁盘空间不足或是磁盘损坏的现象,此时需要对DataNode节点的磁盘进行扩充或是更换,本文对操作流程做一个简单的总结 操作步骤 挂载硬盘 添加硬盘的操 ...

  10. JNI 函数注册与管理

    class<--> 一一对应so-->method     每个so对应于一个类对象 类中的每个native方法对应 于so中的一个native的function,对应关系涉及 {c ...