#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  (微软2016年秋招第三题)

输入

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

分析:

题意就是找到给定序列中斐波那契子序列的个数。

1. 首先想到的就是动态规划,dp[i]表示以i结尾的斐波那契子序列,然后每次变量j (0...i-1)更新i。

但是这样时间复杂度是O(n^2),数据量10^6,肯定是超时的。

2. 考虑优化,每次更新只与斐波那契数列中的元素结尾的有关,没必要dp开那么大,并且从头遍历。

所以可以把dp存成vector<pair<int,int>>,一个表示值,一个表示以此结尾的fib序列个数。然后每次变量vector即可。

但是很遗憾,还是超时了。。。(当数组中斐波那契数列中的数存在很多时,依然是个O(n^2))。

3. 继续优化,其实每个遍历到每个数在斐波那契序列中,更新结果时,只与其在斐波那契序列中前一个数结尾fib序列个数有关。

所以可以把dp[i]考虑存储为以fib[i]结尾的斐波那契子序列个数,100000以内斐波那契数只有25个,所以时间复杂度O(25n) = O(n),就可以了。

注意: 用long long存result防止溢出;记得mod 1000000007

代码:

 #include<iostream>
#include<unordered_map>
using namespace std;
int fib[];
const int m = ;
void init() {
int a = , b = , c = ;
fib[] = ;
fib[] = ;
for (int i = ; i <= ; ++i) {
c = a + b;
fib[i] = c;
a = b;
b = c;
}
}
int findPos(int x) {
for (int i = ; i <= ; ++i) {
if (fib[i] == x) {
return i;
}
}
return -;
} int n;
int nums[];
long long dp[] = {};
int main() {
init();
cin >> n;
long long result = ;
for (int i = ; i < n; ++i) {
cin >> nums[i];
}
int first = -, second = -;
for (int i = ; i < n; ++i) {
if (nums[i] == ) {
first = i;
for (int j = i + ; j < n; ++j) {
if (nums[j] == ) {
second = j;
break;
}
}
break;
}
}
if (first != -) {
dp[] = ;
result += ;
}
if (second != -) {
dp[] = ;
dp[] ++;
result += ;
}
if (second == -) {
cout << result << endl;
return ;
} for (int i = second + ; i < n; ++i) {
if (findPos(nums[i]) == - ) {
continue;
}
if (nums[i] == ) { //1单独处理
dp[] += dp[];
dp[]++;
result += dp[];
dp[] %= m;
result %= m;
continue;
}
dp[findPos(nums[i])] += dp[findPos(nums[i]) - ];
result += dp[findPos(nums[i]) - ];
dp[findPos(nums[i])] %= m;
result %= m;
}
cout << result << endl;
}

hihoCoder#1239 Fibonacci的更多相关文章

  1. 骨牌覆盖问题总结!hihoCoder/ NYOJ-1273宣传墙1151

    本想着做一下第九届河南省省赛题,结果被这个类似骨牌覆盖的题卡住了,队友然我去hihoCoder上老老实实把骨牌覆盖一.二.三做完,这题就没什么问题了.虽然很不情愿,但还是去见识了一下.  骨牌覆盖问题 ...

  2. hihocoder 1873 ACM-ICPC北京赛区2018重现赛 D Frog and Portal

    http://hihocoder.com/problemset/problem/1873 时间限制:1000ms 单点时限:1000ms 内存限制:512MB 描述 A small frog want ...

  3. 算法与数据结构(九) 查找表的顺序查找、折半查找、插值查找以及Fibonacci查找

    今天这篇博客就聊聊几种常见的查找算法,当然本篇博客只是涉及了部分查找算法,接下来的几篇博客中都将会介绍关于查找的相关内容.本篇博客主要介绍查找表的顺序查找.折半查找.插值查找以及Fibonacci查找 ...

  4. #26 fibonacci seqs

    Difficulty: Easy Topic: Fibonacci seqs Write a function which returns the first X fibonacci numbers. ...

  5. 关于java的递归写法,经典的Fibonacci数的问题

    经典的Fibonacci数的问题 主要想展示一下迭代与递归,以及尾递归的三种写法,以及他们各自的时间性能. public class Fibonacci { /*迭代*/ public static ...

  6. 斐波拉契数列(Fibonacci) 的python实现方式

    第一种:利用for循环 利用for循环时,不涉及到函数,但是这种方法对我种小小白来说比较好理解,一涉及到函数就比较抽象了... >>> fibs = [0,1] >>&g ...

  7. hihocoder -1121-二分图的判定

    hihocoder -1121-二分图的判定 1121 : 二分图一•二分图判定 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 大家好,我是小Hi和小Ho的小伙伴Net ...

  8. Hihocoder 太阁最新面经算法竞赛18

    Hihocoder 太阁最新面经算法竞赛18 source: https://hihocoder.com/contest/hihointerview27/problems 题目1 : Big Plus ...

  9. hihoCoder太阁最新面经算法竞赛15

    hihoCoder太阁最新面经算法竞赛15 Link: http://hihocoder.com/contest/hihointerview24 题目1 : Boarding Passes 时间限制: ...

随机推荐

  1. 磁力搜索网站 BT torrent search engine 推荐 2019/12/25日更新

    btkitty 知名的BT磁力搜索,资源很多,中文友好 btdb 知名的BT磁力搜索,资源很多,中文友好 838888 不错的 BT 磁力搜索引擎,资源很多,中文友好 idope.se 资源丰富的BT ...

  2. Excel函数学习:HLOOKUP函数

    Excel函数学习:HLOOKUP函数 HLOOKUP函数查找表的第一行中的值,返回该表中与找到的值在同一列的另一个值. 什么情况下使用HLOOKUP? HLOOKUP函数可以在查找行中找到精确匹配值 ...

  3. cookie-在关闭浏览器之前弹框只弹一次

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  4. mysql 常用命令语法

    登录到mysql client 以windows下为例,打开cmd命令窗口,进入到mysql安装目录bin目录下,首先要启动mysql服务,执行命令: net start mysql,这里不需要分号. ...

  5. linux追加中文字库,解决imagemagick 中文乱码的问题。

    Windows下的字体丰富多样,而且显示的工整.漂亮. 所以自己想把windows上的字体移到Ubuntu下来.Windows下字体库的位置为C:\Windows\fonts,这里面包含所有windo ...

  6. python基础--反射、元类、单例设计模式

    反射:reflect,反射指的是一个对象应该具备可以检测.修改.增加自身属性的能力,反射就是通过字符串操作属性 hasattr(对象,带查询的属性名称) 判断某个对象中是否存在某个属性 getattr ...

  7. 洛谷P1855 榨取kkksc03 [2017年4月计划 动态规划 09]

    P1855 榨取kkksc03 题目描述 洛谷的运营组决定,如果一名oier向他的教练推荐洛谷,并能够成功的使用(成功使用的定义是:该团队有20个或以上的成员,上传10道以上的私有题目,布置过一次作业 ...

  8. UE4物理模块(三)---碰撞查询(下)SAP/MBP/BVH算法简介

    在上一文中介绍了碰撞查询的配置方法: Jerry:UE4物理模块(三)---碰撞查询(上)​zhuanlan.zhihu.com 本篇介绍下UE4的各种零大小的射线检测,以及非零大小(带体积)的射线检 ...

  9. ubuntu设置终端命令历史记录

    ----------------------------------------------- HISTTIMEFORMAT='%F %T ' # 使用HISTTIMEFORMAT在历史中显示TIME ...

  10. angular.module()创建、获取、注册angular中的模块

    // 传递参数不止一个,代表新建模块;空数组代表该模块不依赖其他模块 var createModule = angular.module("myModule", []); // 只 ...