题目3 : Fibonacci
描述
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的更多相关文章
- 山东省第七届ACM省赛------Fibonacci
Fibonacci Time Limit: 2000MS Memory limit: 131072K 题目描述 Fibonacci numbers are well-known as follow: ...
- HPU--1221 Fibonacci数列
题目描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时,Fn也非常大,现在我们想知道,Fn除以10007的余数是多少. 输入 输入包含一个整数n. ...
- 网易编程题——Fibonacci数列
题目描述 Fibonacci数列是这样定义的: F[0] = 0 F[1] = 1 for each i ≥ 2: F[i] = F[i-1] + F[i-2] 因此,Fibonacci数列就形如:0 ...
- 1221: Fibonacci数列 [数学]
1221: Fibonacci数列 [数学] 时间限制: 1 Sec 内存限制: 128 MB 提交: 116 解决: 36 统计 题目描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn- ...
- 2017网易---Fibonacci数列
题目描述 Fibonacci数列是这样定义的:F[0] = 0F[1] = 1for each i ≥ 2: F[i] = F[i-1] + F[i-2]因此,Fibonacci数列就形如:0, 1, ...
- hdu4099 Revenge of Fibonacci
题意:给定fibonacci数列,输入前缀,求出下标.题目中fibonacci数量达到100000,而题目输入的前缀顶多为40位数字,这说明我们只需要精确计算fibinacci数前40位即可.查询时使 ...
- Fibonacci数列(找规律)
题目描述 Fibonacci数列是这样定义的:F[0] = 0F[1] = 1for each i ≥ 2: F[i] = F[i-1] + F[i-2]因此,Fibonacci数列就形如:0, 1, ...
- 算法设计与分析 1.2 不一样的fibonacci数列
★题目描述 fibonacci 数列的递推公式是F(n) = F(n-1) + F(n-2)(n >= 2 且 n 为整数). 将这个递推式改为F(n) = aF(n-1) + bF(n-2)( ...
- [HDU3117]Fibonacci Numbers
题目:Fibonacci Numbers 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3117 分析: 1)后四位可以用矩阵快速幂解决.$T= \left ...
随机推荐
- Group Shifted Strings -- LeetCode
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- POJ 2482 Stars in Your Window 离散化+扫描法 线段树应用
遇见poj上最浪漫的题目..题目里图片以上几百词为一篇模板级英文情书.这情感和细腻的文笔深深地打动了我..不会写情书的童鞋速度进来学习.传送门 题意:坐标系内有n个星星,每个星星都有一个亮度c (1& ...
- App Distribution Guide (一)
This guide contains everything you need to know to distribute an app through the App Store or Mac Ap ...
- 【log4j】springboot项目启动 ,使用的druid数据源,log4j报错 log4j:WARN Please initialize the log4j system properly.
springboot项目启动 ,使用的druid数据源,log4j报错 -- :: --- [ restartedMain] o.hibernate.annotations.common.Versio ...
- Android AIDL实例解析
AIDL这项技术在我们的开发中一般来说并不是很常用,虽然自己也使用新浪微博的SSO登录,其原理就是使用AIDL,但是自己一直没有动手完整的写过AIDL的例子,所以就有了这篇简单的文章. AIDL(An ...
- Navicat 破解版的安装
因为电脑系统换掉,重装系统,重新配置了一下环境,安装Navicat,现记录一下过程,以便下次查询使用. 我们首先百度搜索一款navicat for mysql然后进行下载. 2 当我们下载完成之后首先 ...
- 1019(C++)
计算n个数的最小公倍数,可用欧几里得算法计算两个数字的最大公约数,再计算两个数最小公倍数 有了2个数最小公倍数算法就简单了,即为:计算第一和第二个数得到最小公倍数lc,再计算lc和第三个数最小公倍数. ...
- Spark-Join优化之Broadcast
适用场景 进行join中至少有一个RDD的数据量比较少(比如几百M,或者1-2G) 因为,每个Executor的内存中,都会驻留一份广播变量的全量数据 Broadcast与map进行join代码示例 ...
- 2017.4.26 慕课网--Java 高并发秒杀API(一)
Java高并发秒杀API系列(一) -----------------业务分析及Dao层 第一章 课程介绍 1.1 内容介绍及业务分析 (1)课程内容 SSM框架的整合使用 秒杀类系统需求理解和实现 ...
- 转:Spring mvc中@RequestMapping 6个基本用法小结
Spring mvc中@RequestMapping 6个基本用法小结 发表于3年前(2013-02-17 19:58) 阅读(11698) | 评论(1) 13人收藏此文章, 我要收藏 赞3 4 ...