fibonacci数列(二)

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
 
描述

In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

An alternative formula for the Fibonacci sequence is

.

Given an integer n, your goal is to compute the last 4 digits of Fn.

Hint

As a reminder, matrix multiplication is associative, and the product of two 2 × 2 matrices is given by

.

Also, note that raising any 2 × 2 matrix to the 0th power gives the identity matrix:

.

 
输入
The input test file will contain multiple test cases. Each test case consists of a single line containing n (where 0 ≤ n ≤ 1,000,000,000). The end-of-file is denoted by a single line containing the number −1.
输出
For each test case, print the last four digits of Fn. If the last four digits of Fn are all zeros, print ‘0’; otherwise, omit any leading zeros (i.e., print Fn mod 10000).
样例输入
0
9
1000000000
-1
样例输出
0
34
6875
来源
POJ
上传者
hzyqazasdf
解题思路:
一下午就学了这一个算法,有很多细节总是花很长时间才理解。感觉自己学习算法效率好低啊。
之前刚接触斐波那契数列,想找一个更高效的方法来求,当时看到了,却根本不懂。原来这就是矩阵快速幂。。。从此有了求斐波那契数列更好的方法
既然整数求幂可以用快速幂来求,那么矩阵的幂同样也可以啊。
#include <iostream>
#include <cstdio>
#include <cstring> #define mod 10000 using namespace std; struct matrix{
int m[][];
}; matrix base,ans; void init(int n){//只初始化base和ans(单位矩阵)
memset(base.m,,sizeof(base.m));
memset(ans.m,,sizeof(ans.m));
for(int i=;i<;i++){
ans.m[i][i]=;
} base.m[][]=base.m[][]=base.m[][]=;
} matrix multi(matrix a,matrix b){
matrix t;
for(int i=;i<;i++){
for(int j=;j<;j++){
t.m[i][j]=;
for(int k=;k<;k++){
t.m[i][j]=(t.m[i][j]+a.m[i][k]*b.m[k][j])%mod;
}
}
}
return t;
} int fast_matrix(int n){
while(n){
if(n&){
ans=multi(ans,base);
}
base=multi(base,base);
n>>=;
}
return ans.m[][];
} int main()
{
int n;
while(~scanf("%d",&n) && n!=-){
init(n);
printf("%d\n",fast_matrix(n));
}
return ;
}

nyoj_148_fibonacci数列(二)_矩阵快速幂的更多相关文章

  1. fibonacci数列(二)_矩阵快速幂

    描述 In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For exampl ...

  2. HDU——1005Number Sequence(模版题 二维矩阵快速幂+操作符重载)

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  3. leetcode_935. Knight Dialer_动态规划_矩阵快速幂

    https://leetcode.com/problems/knight-dialer/ 在如下图的拨号键盘上,初始在键盘中任意位置,按照国际象棋中骑士(中国象棋中马)的走法走N-1步,能拨出多少种不 ...

  4. POJ3070 斐波那契数列递推 矩阵快速幂模板题

    题目分析: 对于给出的n,求出斐波那契数列第n项的最后4为数,当n很大的时候,普通的递推会超时,这里介绍用矩阵快速幂解决当递推次数很大时的结果,这里矩阵已经给出,直接计算即可 #include< ...

  5. bzoj5118 Fib数列2 二次剩余+矩阵快速幂

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=5118 题解 这个题一看就是不可做的样子. 求斐波那契数列的第 \(n\) 项,\(n \leq ...

  6. Tribonacci UVA - 12470 (简单的斐波拉契数列)(矩阵快速幂)

    题意:a1=0;a2=1;a3=2; a(n)=a(n-1)+a(n-2)+a(n-3);  求a(n) 思路:矩阵快速幂 #include<cstdio> #include<cst ...

  7. hihoCoder #1151 : 骨牌覆盖问题·二 (矩阵快速幂,DP)

    题意:给一个3*n的矩阵,要求用1*2的骨牌来填满,有多少种方案? 思路: 官网题解用的仍然是矩阵快速幂的方式.复杂度O(logn*83). 这样做需要构造一个23*23的矩阵,这个矩阵自乘n-1次, ...

  8. BZOJ5118 Fib数列2(矩阵快速幂)

    特殊矩阵的幂同样满足费马小定理. #include<iostream> #include<cstdio> #include<cmath> #include<c ...

  9. BZOJ 3231: [Sdoi2008]递归数列 (JZYZOJ 1353) 矩阵快速幂

    http://www.lydsy.com/JudgeOnline/problem.php?id=3231   和斐波那契一个道理在最后加一个求和即可 #include<cstdio> #i ...

随机推荐

  1. editplus的配置和使用

    editplus以及其他所有软件的 "页" 是一个什么概念? 所谓 页 : 是指 当前 你看到的 "客户区" client 的区域大小. 如果窗口越小, 那么你 ...

  2. thinkphp-许愿墙-3

    用jquery写异步传递的时候, 首先要判断表单中的输入是否为空: 如果有多个输入项, 应该, 分别的, 一步一步的来判断是否为空, 而不是用 and / or来复合判断! 同时如果为空, 应该将它设 ...

  3. xinetd

    最简安装centos6.4时,xinetd服务是没有安装的,只是在/etc下有xinetd.d目录, 没有xinetd.conf这个配置文件 xinetd is a secure replacemen ...

  4. 论Linux运维的一些基础安全知识和简单办法

    不知不觉本人来北京也已经第三个年头了,从一个Linux小小鸟,开始,2012年我参加了第一份工作,其实现在想想其实我是幸运的,本来求学的时候,就没好好的学Linux,我认为有Cisco知识从上wind ...

  5. JMS的可靠性

    ---------------------------------------------------------------------------------------------------- ...

  6. nyoj 4 779 兰州烧饼

    兰州烧饼 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 烧饼有两面,要做好一个兰州烧饼,要两面都弄热.当然,一次只能弄一个的话,效率就太低了.有这么一个大平底锅,一 ...

  7. Swift语法入门

    正文参考: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Progra ...

  8. BZOJ1146——[CTSC2008]网络管理Network

    1.题目大意:就是在动态的树上路径权值第k大. 2.分析:这个就是树链剖分+树套树 #include <cstdio> #include <cstdlib> #include ...

  9. Android学习笔记(十八)——再谈升级数据库

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 之前我们为了保证数据库中的表是最新的,只是简单地在 onUpgrade()方法中删除掉了当前所有的表,然后强制 ...

  10. [POJ1068]Parencodings

    [POJ1068]Parencodings 试题描述 Let S = s1 s2...s2n be a well-formed string of parentheses. S can be enco ...