#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. mac mamp host 配置

    <VirtualHost *:80> DocumentRoot "/Users/xuxu/www" ServerName localhost <Directory ...

  2. Oracle中with as用法

    with as 相当于虚拟视图. 例子:需求描述 按照x列分组后统计y列的总值,最终目标是选出比y列总值的三分之一大的那些分组统计信息   使用子查询方式实现最容易想到的方法 SELECT x, SU ...

  3. webpack学习之—— Manifest

    Runtime runtime,以及伴随的 manifest 数据,主要是指:在浏览器运行时,webpack 用来连接模块化的应用程序的所有代码.runtime 包含:在模块交互时,连接模块所需的加载 ...

  4. jQuery中的工具和插件

    jQuery的工具属性 jQuery类数组操作 length属性 表示获取类数组中元素的个数 get()方法 表示获取类数组中单个元素"括号中填写该元素的索引值" index()方 ...

  5. iOS开发——你真的会用SDWebImage?

    http://www.cocoachina.com/ios/20160503/16064.html 本文授权转载,作者:hosea_zhou(简书) SDWebImage作为目前最受欢迎的图片下载第三 ...

  6. iOS音频篇:使用AVPlayer播放网络音乐

    http://www.cocoachina.com/ios/20160324/15767.html 引言 假如你现在打算做一个类似百度音乐.豆瓣电台的在线音乐类APP,你会怎样做? 首先了解一下音频播 ...

  7. Http响应response(文件下载、验证码)

    Http响应response response:响应 作用: 往浏览器写东西 组成部分: 响应行 响应头 响应体 操作响应行 格式: 协议/版本 状态码 状态码说明 状态码: 1xx:已发送请求 2x ...

  8. golang之常量

    1.  常量可以是全局常量,也可以是函数内部的局部常量.常量的值不可修改,常量表达式的值在编译期计算,而不是在运行期.存储在常量中的数据类型只可以是布尔型.数字型(整数型.浮点型和复数)和字符串型.当 ...

  9. jenkins执行selenium自动化测试浏览器不显示解决方法

    因为jenkins是用windows installer 安装成 windows的服务了,那么jenkins是一个后台服务,所以跑selium cases 的时候不显示浏览器 解决办法:Step 1. ...

  10. Tumblr,instapaper分享

    <div id="bshare-custom"> <a title="分享到Tumblr" id="bshare-tumblr&qu ...