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. EclipseIDE设置

    对于新安装的Eclipse而言要设置: 1.Window-Preferences-General-Workspace,然后分别设置Text file encoding为UTF-8和设置New text ...

  2. arcgis api for js之echarts开源js库实现地图统计图分析

    前面写过一篇关于arcgis api for js实现地图统计图的,具体见:http://www.cnblogs.com/giserhome/p/6727593.html 那是基于dojo组件来实现图 ...

  3. shell按行读取文件

    这工作小半年了发现以前学的那么多流弊技能都不怎么用,倒是shell用的很多,自己已经从shell小菜鸟一步步走过来,已经要变成大菜鸟=.= 经常需要用shell按行读取配置文件,自己在上面踩了很多坑, ...

  4. 如何在MQ中实现支持任意延迟的消息?

    什么是定时消息和延迟消息? 定时消息:Producer 将消息发送到 MQ 服务端,但并不期望这条消息立马投递,而是推迟到在当前时间点之后的某一个时间投递到 Consumer 进行消费,该消息即定时消 ...

  5. CGO 类型(CGO Types) 一

    CGO Types C作为一种混合编程语言已经很久了,无论那些广泛使用的包是用何种语言实现的,都导出了和C兼容的API.Go程序调用C程序,可以借助两种工具实现,一种是cgo,另一种是SWIG工具.C ...

  6. Spark监控官方文档学习笔记

    任务的监控和使用 有几种方式监控spark应用:Web UI,指标和外部方法 Web接口 每个SparkContext都会启动一个web UI,默认是4040端口,用来展示一些信息: 一系列调度的st ...

  7. android 删除相册图片并同步到图库

    private void deleteImage(String imgPath) { ContentResolver resolver = getContentResolver(); Cursor c ...

  8. InnoDB锁

    共享锁和排它锁 InnoDB实现了标准的行级锁,包括两种类型:共享锁(S)和排它锁(X) 一个共享锁(S)允许事务持有这种锁来读取一行 一个排它锁(X)允许事务持有这种锁来修改或删除一行 如果事务T1 ...

  9. Spring入门之IOC

    IOC/DI: Spring最核心的灵魂,IOC/DI(控制反转/依赖注入)!,这里一定要理解他,理解这个思想.我会细说什么是IOC/DI.他的思想是什么.它带来了那些好处. 进入正题,先说说什么是i ...

  10. 【漏洞分析】dedecms有前提前台任意用户密码修改

     0x00 前言 早上浏览sec-news,发现锦行信息安全发布了一篇文章<[漏洞分析] 织梦前台任意用户密码修改>,看完之后就想着自己复现一下. 该漏洞的精髓是php的弱类型比较,'0. ...