斐波那契数列 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 ...
随机推荐
- 搭建lnmp教程
LNMP指的是一个基于CentOS/Debian 上安装Nginx.PHP.MySQL.php.可以在独立主机上轻松的安装LNMP生产环境. 1 安装nginx 如果是一台新的服务器可直接安装(若以前 ...
- Java 伙伴系统(模拟)
参考:https://labrick.cc/2015/10/12/buddy-system-algorithm/ 代码过烂 不宜参考. output: [operating.entity.Heap@4 ...
- js功能代码大全
1.日期格式化 //化为2017-08-14 function formatDate (date) { var y = date.getFullYear(); var m = date.getMont ...
- 2.Nginx日常维护技巧
Nginx日常维护技巧 Nginx配置正确性检查 nginx提供了配置文件调试功能,可以快速定义配置文件存在的问题.执行如下命令检测配置文件的正确性: [root@localhost 桌面]# whi ...
- Golang丰富的I/O----用N种Hello World展示
h1 { margin-top: 0.6cm; margin-bottom: 0.58cm; direction: ltr; color: #000000; line-height: 200%; te ...
- oracle12c_安装3——部署
数据库安装后需要根据实际情况修改相关参数. 1.生成pfile以防万一. SQL> create pfile from spfile; 2.修改内存参数 只要设置MEMORY_MAX_TARGE ...
- 【自问自答】关于 Swift 的几个疑问
感觉自己给自己释疑,也是一个极为有趣的过程.这次,我还新增了"猜想"一栏,来尝试回答一些暂时没有足够资料支撑的问题. Swift 版本是:4.0.3.不同版本的 Swift,可能无 ...
- 微服务时代TestOps工程师学习总结
TestOps很新鲜,也是近期衍生的新型职位.那TestOps主要目的是推动整个研发体系与发布体系更多在质量方面.可以这样理解DevOps是从研发推动配合运维和测试,而TestOps是从测试角度推动研 ...
- 带以太网的MicroPython开发板:TPYBoardv201温湿度上传实例
转载请以链接形式注明文章来源,MicroPythonQQ交流群:157816561,公众号:MicroPython玩家汇 历来关于温湿度的检测都是没有间断过的,这次我们继续检测温湿度,同样还是使用DH ...
- input 光标在 chrome下不兼容 解决方案
input 光标在 chrome下不兼容 解决方案 height: 52px; line-height: normal; line-height:52px\9 .list li input[type= ...