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的更多相关文章

  1. C#求斐波那契数列第30项的值(递归和非递归)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. 斐波拉契数列加强版——时间复杂度O(1),空间复杂度O(1)

    对于斐波拉契经典问题,我们都非常熟悉,通过递推公式F(n) = F(n - ) + F(n - ),我们可以在线性时间内求出第n项F(n),现在考虑斐波拉契的加强版,我们要求的项数n的范围为int范围 ...

  3. js中的斐波那契数列法

    //斐波那契数列:1,2,3,5,8,13…… //从第3个起的第n个等于前两个之和 //解法1: var n1 = 1,n2 = 2; for(var i=3;i<101;i++){ var ...

  4. 剑指Offer面试题:8.斐波那契数列

    一.题目:斐波那契数列 题目:写一个函数,输入n,求斐波那契(Fibonacci)数列的第n项.斐波那契数列的定义如下: 二.效率很低的解法 很多C/C++/C#/Java语言教科书在讲述递归函数的时 ...

  5. 算法: 斐波那契数列C/C++实现

    斐波那契数列: 1,1,2,3,5,8,13,21,34,....     //求斐波那契数列第n项的值 //1,1,2,3,5,8,13,21,34... //1.递归: //缺点:当n过大时,递归 ...

  6. 洛谷P1962 斐波那契数列 || P1349 广义斐波那契数列[矩阵乘法]

    P1962 斐波那契数列 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数 ...

  7. Python递归及斐波那契数列

    递归函数 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数.举个例子,我们来计算阶乘 n! = 1 * 2 * 3 * ... * n,用函数 fact(n)表示,可 ...

  8. 简单Java算法程序实现!斐波那契数列函数~

    java编程基础--斐波那契数列 问题描述:一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 思路:可能出现的情况:(1) n=1 ,一种方法 ;(2)n=2 ...

  9. js 斐波那契数列(兔子问题)

    对于JS初学者来说,斐波那契数列一直是个头疼的问题,总是理不清思路. 希望看完这篇文章之后会对你有帮助. 什么是斐波那契数列 : 答: 斐波那契数列,又称黄金分割数列.因数学家列昂纳多·斐波那契(Le ...

随机推荐

  1. Visual Studio Code 快捷键大全(Windows)

    Visual Studio Code 是一款优秀的编辑器,非常适合编写 TS 以及 React .最近在学习 AngularJs 2,开始使用 VSCode,特意整理翻译了一下官网的快捷键.因为已经习 ...

  2. Xamarin截取/删除emoji表情bug解决方案

    大家都知道,一个英文=1字节,一个汉字2字节,而一个emoji表情=4个字节,在有这三种混用的时候,比如app聊天界面,那么删除和截取便成了很头痛的事情. 问题描述 截取导致乱码,如下图: 解决方案 ...

  3. TxDragon的训练5

    Solution 代码:由乃: //MADE BY QT666 #include<iostream> #include<cstdio> #include<algorith ...

  4. Linux(CentOS6.5)下编译Popt报错”GNU gettext is required. The latest version”(gettext已经编译安装,但是没有安装在默认目录)的解决方案

    本文地址http://comexchan.cnblogs.com/,作者Comex Chan,尊重知识产权,转载请注明出处,谢谢!   背景: 编译popt的时候出现下述报错. 直接vi查看confi ...

  5. 开发中关于Git那些事(续:Git变基)

    其实上一篇写的内容仅仅是Git的冰山一角,如果你认为Git就是简简单单的几行命令,那只能说明你还没有真正了解Git这个强大的内容寻址文件系统.这篇文章,还是接着介绍一些实用但是很少有人知晓的一些命令, ...

  6. WebRTC 入门到放弃(一)WebRTC

    前言 WebRTC,名称源自网页实时通信(Web Real-Time Communication)的缩写,是一个支持网页浏览器进行实时语音对话或视频对话的技术,是谷歌2010年以6820万美元收购Gl ...

  7. 通过pyenv和virtualenv创建多版本Python虚拟环境

    虚拟环境使用第三方工具virtualenv创建,首先输入以下命令检查系统是否已经安装virtualenv. $ virtualenv --version 如果显示virtualenv版本号,则说明已经 ...

  8. iOS自带API集成二维码、条形码扫描

    源码于 :https://github.com/wangjinfeng/ScanForiOSAPI/tree/main 1.AVFoundation.framework,QuartzCore.fram ...

  9. [Spark SQL] SparkSession、DataFrame 和 DataSet 练习

    本課主題 DataSet 实战 DataSet 实战 SparkSession 是 SparkSQL 的入口,然后可以基于 sparkSession 来获取或者是读取源数据来生存 DataFrameR ...

  10. 网络时钟服务器,NTP授时设备,北斗网络校时服务器,GPS时间同步器

    网络时钟服务器,NTP授时设备,北斗网络校时服务器,GPS时间同步器 网络时钟服务器,NTP授时设备,北斗网络校时服务器,GPS时间同步器 论述当下网络时间同步的重要性   北京华人开创科技发展有限公 ...