POJ3070 Fibonacci【矩阵快速幂】
Fibonacci
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 20098 Accepted: 13850
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
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:
.
Source
Stanford Local 2006
问题链接:POJ3070 Fibonacci
问题简述:(略)
问题分析:
矩阵快速幂的模板题。
程序说明:(略)
参考链接:(略)
题记:(略)
AC的C语言程序如下:
/* POJ3070 Fibonacci */#include <iostream>
#include <stdio.h>
#include <string.h>using namespace std;const int MOD = 1e4;
const int N = 2;struct Matrix
{int m[N][N];Matrix() {}Matrix operator*(Matrix const &a)const{Matrix b;memset(b.m, 0, sizeof(b.m));for (int i = 0 ;i < N; i++)for (int j = 0; j < N; j++)for (int k = 0; k < N; k++)b.m[i][j] = (b.m[i][j] + this->m[i][k] * a.m[k][j]) % MOD;return b;}
};Matrix pow_mod(Matrix a, int n)
{Matrix b;memset(b.m, 0, sizeof(b.m));for (int i = 0; i < N; i++)b.m[i][i] = 1;while (n > 0){if (n & 1) b = b * a;a = a * a;n >>= 1;}return b;
}int main()
{Matrix a;for (int i = 0; i < N; i++)for (int j = 0; j < N; j++)a.m[i][j] = 1;a.m[1][1] = 0;int n;while (~scanf("%d", &n) && n != -1){Matrix b = pow_mod(a, n);printf("%d\n", b.m[0][1]);}return 0;
}
POJ3070 Fibonacci【矩阵快速幂】相关推荐
- poj3070 Fibonacci 矩阵快速幂
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18084 Accepted: 12572 Descr ...
- POJ 3070 Fibonacci(矩阵快速幂入门、模板)
? 题目链接:http://poj.org/problem?id=3070 ? 这题就是让求斐波那契数列的第n项,但是题目中n很大,所以打表和直接求都会TLE,对于这个题我们可以用矩阵快速幂,下面 ...
- HDU 3306 Another kind of Fibonacci 矩阵快速幂
题目链接 因为S(N) , S(N) = A(0)^2 +A(1)^2+--+A(n)^2.所以构造的矩阵一定要维护A(n)^2 s[n-1]=s[n-2]+A[n-1]^2 A[n]=x*A[n-1 ...
- POJ3070 Fibonacci(矩阵快速幂)
用矩阵快速幂求fibonacci序列的第n项. /* *********************************************** Author :devil Created Tim ...
- H - Fibonacci POJ - 3070 (矩阵快速幂)
H - Fibonacci POJ - 3070 (矩阵快速幂) Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and ...
- SDNU 1062.Fibonacci(矩阵快速幂)
Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. I ...
- 矩阵快速幂 POJ 3070 Fibonacci
题目传送门 1 /* 2 矩阵快速幂:求第n项的Fibonacci数,转置矩阵都给出,套个模板就可以了.效率很高啊 3 */ 4 #include <cstdio> 5 #include ...
- (矩阵快速幂)解所有类似Fibonacci 的题目
Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...
- POJ3070矩阵快速幂简单题
题意: 求斐波那契后四位,n <= 1,000,000,000. 思路: 简单矩阵快速幂,好久没刷矩阵题了,先找个最简单的练练手,总结下矩阵推理过程,其实比较简单,关键 ...
最新文章
- JSP第二次作业_6小题
- pads铺铜不能开启drp_PowerPCB (PADS )常见问题全集-泪滴-规则设定
- 多项式乘法c语言,急!!!!c语言:求n次多项式的加法和乘法
- 【渝粤教育】广东开放大学 财务管理 形成性考核 (58)
- oracle dbra,资源供给:IO子系统之二
- Centos 下PHP编译安装fileinfo扩展
- 首届全国信创大赛圆满收官,信创新势力载誉而归!
- 推送MobPush-API说明
- python参考手册下载_Python中文手册【Word版 】
- 你知道这些SOLIDWORKS零件图知识吗?
- RRZCMS安全防护建议
- push notifications step1 打不开
- Micron:DDR2 SDRAM与SODIMM
- 山东法律学校97级计算机班,我校计算机学院97级计算机专业校友重聚母校
- 为什么现在java这么难找,现在java工作难找吗
- access是干什么的软件
- 五种主流音频无损压缩格式简介
- 香蕉派 BPI-R2 作为4G多路聚合融合通信应用开发平台
- Jenkins Git Changelog Plugin
- 开始协议处理句柄[http-nio-8080]_微软win10发布KB4520062更新,解决登录黑屏和开始菜单空白等问题...