POJ ---3070 (矩阵乘法求Fibonacci 数列)
Description
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.
Input
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.
Output
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).
Sample Input
0
9
999999999
1000000000
-1
Sample Output
0
34
626
6875 思路:矩阵快速幂,没什么可说的。
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef struct Matrix{
int m[][];
Matrix(){
memset(m, , sizeof(m));
}
}Matrix;
Matrix mtMul(Matrix A, Matrix B){
Matrix tmp;
for(int i = ;i < ;i ++)
for(int j = ;j < ;j ++)
for(int k = ;k < ;k ++){
int t = (A.m[i][k] * B.m[k][j])%;
tmp.m[i][j] = (tmp.m[i][j] + t)%;
}
return tmp;
}
Matrix mtPow(Matrix A, int k){
if(k == ) return A;
Matrix tmp = mtPow(A, k >> );
Matrix res = mtMul(tmp, tmp);
if(k & ) res = mtMul(res, A);
return res;
}
int main(){
int n;
while(~scanf("%d", &n) && (n+)){
if(n == ) printf("0\n");
else{
Matrix M;
M.m[][] = M.m[][] = M.m[][] = ;
M.m[][] = ;
Matrix tmp = mtPow(M, n);
printf("%d\n", tmp.m[][]);
}
}
return ;
}
POJ ---3070 (矩阵乘法求Fibonacci 数列)的更多相关文章
- 【poj3070】矩阵乘法求斐波那契数列
[题目描述] 我们知道斐波那契数列0 1 1 2 3 5 8 13…… 数列中的第i位为第i-1位和第i-2位的和(规定第0位为0,第一位为1). 求斐波那契数列中的第n位mod 10000的值. [ ...
- poj 3070 矩阵快速幂模板
题意:求fibonacci数列第n项 #include "iostream" #include "vector" #include "cstring& ...
- 用PL0语言求Fibonacci数列前m个中偶数位的数
程序说明:求Fibonacci数列前m个中偶数位的数: 这是编译原理作业,本打算写 求Fibonacci数列前m个数:写了半天,不会写,就放弃了: 程序代码如下: var n1,n2,m,i; pro ...
- C++项目參考解答:求Fibonacci数列
[项目:求Fibonacci数列] Fibonacci数列在计算科学.经济学等领域中广泛使用,其特点是:第一.二个数是1,从第3个数開始,每一个数是其前两个数之和.据此,这个数列为:1 1 2 3 5 ...
- 程序员面试题精选100题(16)-O(logn)求Fibonacci数列[算法]
作者:何海涛 出处:http://zhedahht.blog.163.com/ 题目:定义Fibonacci数列如下: / 0 n=0 f(n)= ...
- 《面试题精选》15.O(logn)求Fibonacci数列
题目:定义Fibonacci数列例如以下: / 0 n=0 f(n)= 1 n=1 ...
- 求Fibonacci数列通项公式
0. Intro \[f_n=\begin{cases} 0 & (n=0) \\ 1 & (n=1) \\ f_{n-1}+f_{n-2} & (n>1) \end{c ...
- ☆ [HDU2157] How many ways?? 「矩阵乘法求路径方案数」
传送门:>Here< 题意:给出一张有向图,问从点A到点B恰好经过k个点(包括终点)的路径方案数 解题思路 一道矩阵乘法的好题!妙哉~ 话说把矩阵乘法放在图上好神奇,那么跟矩阵唯一有关的就 ...
- 用Python实现求Fibonacci数列的第n项
1. 背景——Fabonacci数列的介绍(摘自百度百科): 斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacc ...
随机推荐
- pngfix 实现Png图片透明效果
1.http://www.jb51.net/codes/67324.html 2.DD_belatedPNG工具
- jquery-easyui使用
1,闲来无事用了一下jquery-easyui,首先下载下来easyui,文件夹如图 2,将themes里面的东西整体拷贝,我是整体的,省的麻烦了 3,在在自己的vs里面新建一个mvc的项目(项目自己 ...
- DataNavigator之分页
前言 做客户端也有两个月了,先前做列表都没有分页,可能考虑数据也不是很多,昨天做了一个页面,考虑到了数据的问题,所以改为分页查询.因为也是第一次用dev,用哪个控件分页呢,还是要去搜一下,得出的事Da ...
- 工作踩坑记录:JavaScript跳转被缓存
起因:业务想要一个固定二维码来每周扫码跳转到不同的页面上去,我用JS写了个跳转,却发现被缓存了,虽然被具体被缓存多久不清楚,但是被缓存了很不爽,不符合业务实时更改这个二维码跳转页面的需求. 经过:既然 ...
- 学习PYTHON第一天
需要掌握的内容 1.编程语言 2.python . C# JAVA 3.python: pypy cpython jpython 4.执行方式 解释器 文件执行 5.指定解释 ...
- Python3.4 多线程
线程安全和全局解释器锁 Thread State and the Global Interpreter Lock 总结: 通过使用GIL后, Python多线程安全, 并且数据保持同步. Python ...
- Ubuntu启动停止在checking battery state...
Ubuntu在“checking battery state...”处停止了,出现这种现象的原因可能有很多种.笔者遇到的是其中的一种. 情景描述: 笔者此前将Ubuntu的图形启动方式修改为了字符界面 ...
- JAVA 内存管理总结
1. java是如何管理内存的 Java的内存管理就是对象的分配和释放问题.(两部分) 分配 :内存的分配是由程序完成的,程序员需要通过关键字new 为每个对象申请内存空间 (基本类型除外),所有的对 ...
- poj 3415 Common Substrings(后缀数组+单调栈)
http://poj.org/problem?id=3415 Common Substrings Time Limit: 5000MS Memory Limit: 65536K Total Sub ...
- csuoj 1350: To Add Which?
这个题目其实很简单,可惜当时比赛的时候看到出的人少,以为有trick,就和队友扯淡去了: 因为每个数总是被相邻的数影响,所以往前往后扫两遍就行了: #include<cstdio> #in ...