斐波那契数列 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 ...
随机推荐
- React的JSX语法及组件
最近一个同事很急没有做任何交接就请了陪产假,然后我来维护.说实在的我一开始是一脸懵逼的.因为MV*项目里用的最多的还是Vue:React听说也了解过,但毕竟不熟... 不过不管如何这也是工作:同事也恭 ...
- Find all factorial numbers less than or equal to N
A number N is called a factorial number if it is the factorial of a positive integer. For example, t ...
- python Is 与== 的坑
以前看过一篇python技术贴,说用is替代==,这样更加pythonic?然后我就能把用'=='的地方用'Is'替代,结果程序运行结果的偏差很大,甚至完全不同.后来发现,Is与==使用上是有区别的. ...
- ntopng 推送solr
1.修改代码在且不说 2.修改完之后先卸载原先的ntopng 使用 whereis ntopng 找到安装目录,然后删除 /usr/local/bin/ntopng /usr/local/share/ ...
- 删除SVN版本信息的两种方式
一.在linux下删除SVN版本信息 删除这些目录是很简单的,命令如下 find . -type d -name ".svn"|xargs rm -rf 或者 find . -ty ...
- Layout 不可思议(二)—— 两侧定宽的三列布局
三列布局作为网页设计中最常见的布局,其实现方法早已被诸位前端大神摸透 网上相关的文章很多,原本已无必要再做赘述 不过既然开了 Layout 系列,三列布局就是必修课 本文整理了一些常用的实现方法,然后 ...
- 微信小程序开发之图片预览
实现图片的展示和大图预览 使用wx.previewImage(OBJECT)来实现 OBJECT参数说明: 参数 类型 必填 说明 current String 否 当前显示图片的链接,不填则默认为 ...
- canvas 简易的加载进度条
做一个web app,想在第一次或者更新的时候,有一个更新进度条,我个人比较喜欢圆的那种. canvas + svg高低配,应该还不错的.顺便一提,canvas用来压缩图片也是么么哒的. 先看下效果图 ...
- Java学习笔记14(面向对象七:final、static)
final:意为最终,不可变,是一个修饰词 有时候一个类地功能被开发好了,不想让子类重写,修改,这里就会用到final关键字 final修饰类: 不可以被继承,但是可以继承其他类 示例: public ...
- 什么是BIG?如何买BIG?
谈到BIG,就要谈到BIG ONE.BigONE号称"全民交易所",也称"云币国际站".是 INBlockchian(硬币资本)旗下全球区块链资产现货交易所,是 ...