斐波那契数列 Library
http://acm.tju.edu.cn/toj/showp3267.html3267. Library
Time Limit: 1.0 Seconds Memory Limit: 65536K
Total Runs: 214 Accepted Runs: 96
Description
As we all know, there are very long stairs before our library in campus III of our school. It is so tired for us to go up stairs. Alpc60 has to go up and down stairs every day, what a boring walk!
One day when alpc60 went up stairs, he thought that every time he can step over one or two stairs, if there are n stairs, then how many ways he can reach the top?
The clever ACMers, can you help alpc60 to calculate how many ways he can go up n (1 ≤ n ≤ 1,000,000,000) stairs.
Because the number of the answer will be so large, you must output the number module by 9901.
Input
Each line of the input contains a number n indicating the stairs number.
Input is ended with -1 which is not the stairs number.
Output
For each case of the input output the possible ways module by 9901.
Sample Input
1
2
5
-1
Sample Output
1
2
8
Hint: The Bruce force method will simply lead to the Time Limit Exceeded error, try some efficient method to solve this problem.
Source: NUDT Programming Contest 2009
题意 : 上楼梯, 每次都可以上1节或2节,求有几种上楼方式,是一个典型的斐波那契数列,某状态可以是从两种情况来的,1,上两节到这儿,2,上1节到这儿,就是f[n] = f[n-1]+f[n-2]
但是因为数会很大所以要用到矩阵,和快速矩阵幂
/*
快速幂矩阵法
dp 动态规划
a[n] = a[n-1]+ a[n-2]; 这是一个典型的斐波那契数列 一般斐波那契数列算到20的时候已经很大了所以一般来说要用快速法
构造矩阵有
a[n-1] = q[n-1]; 为了构造一个矩阵
上下两式分析有 [ a[n] ] = [1,1]*[a[n-1]]
[ a[n-1] ] = [1,0] [a[n-2]]
其中要自定义矩阵的乘法
然后递推得 转化成求矩阵幂的问题
*/ #include<cstdio>
#include<cmath>
#define N 9901
using namespace std;
struct mtx{
// int n;// 矩阵的阶数
int a[][];
mtx operator * (const mtx o) const {
mtx c;
// return c.n = n ;
c.a[][]= c.a[][] = c.a[][] = c.a[][] = ;//做乘法前初始化
for(int i = ; i < ; i++ )
{
for(int j = ; j < ; j++)
{
for(int k = ; k < ; k++)
{
c.a[i][j] += ((a[i][k]%N)*(o.a[k][j]%N))%N;
c.a[i][j] %= N;
}
}
}
return c;
}
};//定义矩阵乘法
/* 如果 递归的写数幂是
int f(int a ,int b)
{
int ret = 1;
if (b == 1 ) return a;
int t = f(a,b/2)
t = t*t;
if(b&1) return t*a;
return t;
} 但是矩阵的乘法一般不写递归形式,因为递归用栈来存储,会爆内存 非递归形式写数幂 int f (int a , int b)
{
int ret = 1;
while(b > 0)
{
if(b&1) ret *= a;
a *= a;
b >>= 1;
}
return ret;
}
int f(int a ,int b )
{
int ret;
for( ret = 1 ; b ; b>>=1)
{
if(b&1) ret*=a;
a = a * a;
}
return ret;
} int f (int a , int b)
{
int ret ;
for(ret = 1 ; b ; b>>=1 , a = a * a %Mod)
if(b&1) ret = ret*a%N;////+=要比加快。。。。。也可以写成ret*=a; ret %=a;
return ret;
}
*/ mtx f(int b)
{
mtx t;
mtx haha ;
haha.a[][] = haha.a[][] = haha.a[][] = ;
haha.a[][] = ; t.a[][] = t.a[][] = ;
t.a[][] = t.a[][] = ;
mtx ret ;
//if(b==1) return t;
for(ret = t ; b ; b>>=)
{
// printf("%d\n", b);
if(b&) ret = ret * haha ;
haha = haha * haha;
}
return ret;
} //快速幂矩阵 int main()
{
int cnt ;
while(~scanf("%d",&cnt)&&cnt!=-)
{
mtx ans;
ans = f(cnt-);
// printf("%d %d\n%d %d\n", ans.a[0][0], ans.a[0][1], ans.a[1][0], ans.a[1][1]);
int sum = (*ans.a[][]+*ans.a[][])%N;
printf("%d\n",sum);
}
return ;
}
斐波那契数列 Library的更多相关文章
- C#求斐波那契数列第30项的值(递归和非递归)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 斐波拉契数列加强版——时间复杂度O(1),空间复杂度O(1)
对于斐波拉契经典问题,我们都非常熟悉,通过递推公式F(n) = F(n - ) + F(n - ),我们可以在线性时间内求出第n项F(n),现在考虑斐波拉契的加强版,我们要求的项数n的范围为int范围 ...
- js中的斐波那契数列法
//斐波那契数列:1,2,3,5,8,13…… //从第3个起的第n个等于前两个之和 //解法1: var n1 = 1,n2 = 2; for(var i=3;i<101;i++){ var ...
- 剑指Offer面试题:8.斐波那契数列
一.题目:斐波那契数列 题目:写一个函数,输入n,求斐波那契(Fibonacci)数列的第n项.斐波那契数列的定义如下: 二.效率很低的解法 很多C/C++/C#/Java语言教科书在讲述递归函数的时 ...
- 算法: 斐波那契数列C/C++实现
斐波那契数列: 1,1,2,3,5,8,13,21,34,.... //求斐波那契数列第n项的值 //1,1,2,3,5,8,13,21,34... //1.递归: //缺点:当n过大时,递归 ...
- 洛谷P1962 斐波那契数列 || P1349 广义斐波那契数列[矩阵乘法]
P1962 斐波那契数列 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数 ...
- Python递归及斐波那契数列
递归函数 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数.举个例子,我们来计算阶乘 n! = 1 * 2 * 3 * ... * n,用函数 fact(n)表示,可 ...
- 简单Java算法程序实现!斐波那契数列函数~
java编程基础--斐波那契数列 问题描述:一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 思路:可能出现的情况:(1) n=1 ,一种方法 ;(2)n=2 ...
- js 斐波那契数列(兔子问题)
对于JS初学者来说,斐波那契数列一直是个头疼的问题,总是理不清思路. 希望看完这篇文章之后会对你有帮助. 什么是斐波那契数列 : 答: 斐波那契数列,又称黄金分割数列.因数学家列昂纳多·斐波那契(Le ...
随机推荐
- qt调用simsimi api实现小黄鸡
项目地址:https://github.com/racaljk/xiaojianji 好吧我把它命名为小贱鸡.,下面说一说他的实现. 由于官方的simsimi api需要收费,免费试用版有各种限制,所 ...
- http性能测试工具wrk源码学习之开篇
1.前言 最近工作需要测试nginx反向代理的性能,于是找了一些http测试工具,例如经典的Apache的ab.siege.wrk.wrk使用多线程事件驱动方式,支持lua脚本扩展.关于wrk介绍可以 ...
- 浅谈对ST表的一些理解
今天打了人生第一道ST表题(其实只是ST表跑得最快); ST表是一种用来解决RMQ问题的利器... 大体操作有两步: 第一部分nlogn预处理 第二部分O(1)询问 预处理就是运用倍增+区间动规 ST ...
- AdaBoost对实际数据分类的Julia实现
写在前面 AdaBoost是机器学习领域一个很重要很流行的算法,而Julia是一门新兴的发展迅速的科学计算语言.本文将从一个实际例子出发,展示如何用Julia语言实现AdaBoost算法. 什么是Ad ...
- ThreadLocal 线程本地变量 及 源码分析
■ ThreadLocal 定义 ThreadLocal通过为每个线程提供一个独立的变量副本解决了变量并发访问的冲突问题 当使用ThreadLocal维护变量时,ThreadLocal为每个使用该变量 ...
- Django学习日记03_模型_Fields
创建模型 模型对应工程中的应用,一个工程可能包含很多的应用,通过命令 python manage.py startapp myapp 创建一个叫myapp的应用,django会帮助生成以下目录: po ...
- vue的挖坑和爬坑之vuex的简单入门
首先vuex的中文文档https://vuex.vuejs.org/zh-cn/ 首先vuex是什么,官方解释是 Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应 ...
- VS代码生成工具ReSharper使用手册:配置快捷键(转)
原文:http://blog.csdn.net/fhzh520/article/details/46364603 VS代码生成工具ReSharper提供了丰富的快捷键,可以极大地提高你的开发效率. 配 ...
- File System 之本地文件系统
上一篇文章提到了,最近做一个基于 File System/IndexedDB的应用,上一篇是定额和使用的查询. 因为LocalFileSystem只有chrome支持,有点尴尬,如果按需加载又何来尴尬 ...
- Go执行远程ssh命令
使用包:golang.org/x/crypto/ssh 以下封装一个发送命令的Cli结构体 type Cli struct { IP string //IP地址 Username string //用 ...