nyoj_148_fibonacci数列(二)_矩阵快速幂
fibonacci数列(二)
- 描述
-
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
- 样例输出
-
- 0
- 34
- 6875
- 0
- 来源
- 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数列(二)_矩阵快速幂的更多相关文章
- fibonacci数列(二)_矩阵快速幂
描述 In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For exampl ...
- HDU——1005Number Sequence(模版题 二维矩阵快速幂+操作符重载)
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- leetcode_935. Knight Dialer_动态规划_矩阵快速幂
https://leetcode.com/problems/knight-dialer/ 在如下图的拨号键盘上,初始在键盘中任意位置,按照国际象棋中骑士(中国象棋中马)的走法走N-1步,能拨出多少种不 ...
- POJ3070 斐波那契数列递推 矩阵快速幂模板题
题目分析: 对于给出的n,求出斐波那契数列第n项的最后4为数,当n很大的时候,普通的递推会超时,这里介绍用矩阵快速幂解决当递推次数很大时的结果,这里矩阵已经给出,直接计算即可 #include< ...
- bzoj5118 Fib数列2 二次剩余+矩阵快速幂
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=5118 题解 这个题一看就是不可做的样子. 求斐波那契数列的第 \(n\) 项,\(n \leq ...
- 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 ...
- hihoCoder #1151 : 骨牌覆盖问题·二 (矩阵快速幂,DP)
题意:给一个3*n的矩阵,要求用1*2的骨牌来填满,有多少种方案? 思路: 官网题解用的仍然是矩阵快速幂的方式.复杂度O(logn*83). 这样做需要构造一个23*23的矩阵,这个矩阵自乘n-1次, ...
- BZOJ5118 Fib数列2(矩阵快速幂)
特殊矩阵的幂同样满足费马小定理. #include<iostream> #include<cstdio> #include<cmath> #include<c ...
- BZOJ 3231: [Sdoi2008]递归数列 (JZYZOJ 1353) 矩阵快速幂
http://www.lydsy.com/JudgeOnline/problem.php?id=3231 和斐波那契一个道理在最后加一个求和即可 #include<cstdio> #i ...
随机推荐
- MySql的安装及配置详细指引!
一.安装My Sql数据库 1.1,首先下载MySQL与HeidiSQL工具,双击打开后可以看到名为”mysql-5.0.22-win32 Setup.exe”的安装程序,双击执行该程序. 1.2,打 ...
- WebForm中搭配母版页和用户控件页时候的事件加载顺序
在生产环境中,一个内容页(aspx)可能会包含数个用户控件(ascx),而每个控件可能都会涉及到数据库访问. 如果在内容页.母版页.控件页中各自使用自己的数据库访问方法,会造成很大的运行成本. 这样的 ...
- 新年新技术:HTTP/2
新的一年,项目也要带着发展的眼光往前走,得跟上潮流,当然前提是自己真的用的上. 用的上用不上都得先简单了解下. 2月下旬Google发布了首个基于HTTP/2的RPC框架GRPC,它是基于HTTP/2 ...
- Codeforces #270 D. Design Tutorial: Inverse the Problem
http://codeforces.com/contest/472/problem/D D. Design Tutorial: Inverse the Problem time limit per t ...
- linux之awk、sed命令
总结: 两个命令的正则表达式都用''单引号进行区分. 输出变量名用单引号,输出变量值用双引号,这个在bash脚本里一般是通用的. 在bash脚本里要使用变量值,都要加上双引号 awk用法: 参考网址: ...
- 使用liunx部署的心得
安装SSH. 输入IP,输入用户名,点击连接,再输入密码. 如果采用的tomcat容器,则: 第一步: 查看正在运行的tomcat ps aux|grep tomcat 如果输出信息很长代表启动了的, ...
- 推荐ubuntu下的画图工具
今天发现ubuntu下面也有类似于windows画图的画图工具,功能也比较强大,支持各种格式的图片,也有各种工具,非常方便,安装的方法是: sudo apt-get install kolourpai ...
- python 输入和输出
到目前为止我们遇到过两种输出值的方法: 表达式语句和print语句. (第三个方式是使用文件对象的write()方法: 标准输出文件可以引用 sys.stdout.详细内容参见库参考手册. Pytho ...
- 字串符相关 split() 字串符分隔 substring() 提取字符串 substr()提取指定数目的字符 parseInt() 函数可解析一个字符串,并返回一个整数。
split() 方法将字符串分割为字符串数组,并返回此数组. stringObject.split(separator,limit) 我们将按照不同的方式来分割字符串: 使用指定符号分割字符串,代码如 ...
- qt-5.6.0 移植之纯净的linux文件系统的建立
为什么要建立一个最纯净的文件系统,一开始是想在qt-4.8.5的文件系统基础之上加东西,慎重想了一下,这方法行不通,以为有很多东西不熟悉.干脆就自己建立一个. 步骤很简单: 一:下载一个bulidro ...