25-Fibonacci(矩阵快速幂)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 17694 | Accepted: 12315 |
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
#include <iostream>
#include <cstring>
using namespace std;
const int MAX = 2;
const int mod = 1e4; struct mat{
int f[MAX][MAX];
mat operator * (const mat x){ //重载矩阵的乘法
mat rt;
for(int i = 0; i < MAX; i++){
for(int j = 0; j < MAX; j++){
int ans = 0;
for(int m = 0; m < MAX; m++){
ans += (this->f[i][m] * x.f[m][j]) % mod;
ans %= mod;
}
rt.f[i][j] = ans;
}
}
return rt;
}
}; mat quike(mat base, int n){ //与普通快速幂相似,只是用于存结果的其实值不同,这里用的是rt单位矩阵,类似乘法中设的1
mat rt;
memset(rt.f, 0, sizeof(rt.f));
for(int i = 0; i < MAX; i++)
rt.f[i][i] = 1;
while(n){
if(n & 1)
rt = rt * base;
base = base * base;
n >>= 1;
}
return rt;
} int main(){
int n;
mat base;
for(int i = 0; i < MAX; i++){
for(int j = 0; j < MAX; j++)
base.f[i][j] = 1;
}
base.f[1][1] = 0;
while(cin >> n && n != -1){
mat ans = quike(base, n);
cout << ans.f[0][1] << endl;
}
return 0;
}
25-Fibonacci(矩阵快速幂)的更多相关文章
- UVA - 10229 Modular Fibonacci 矩阵快速幂
Modular Fibonacci The Fibonacci numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 3 ...
- poj 3070 Fibonacci (矩阵快速幂乘/模板)
题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...
- poj 3070 Fibonacci 矩阵快速幂
Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...
- HDU 1588 Gauss Fibonacci(矩阵快速幂)
Gauss Fibonacci Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- POJ 3070 Fibonacci 矩阵快速幂模板
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18607 Accepted: 12920 Descr ...
- poj3070 Fibonacci 矩阵快速幂
学了线代之后 终于明白了矩阵的乘法.. 于是 第一道矩阵快速幂.. 实在是太水了... 这差不多是个模板了 #include <cstdlib> #include <cstring& ...
- $loj$10222 佳佳的$Fibonacci$ 矩阵快速幂
正解:矩阵快速幂 解题报告: 我永远喜欢loj! 一看到这个就应该能想到矩阵快速幂? 然后就考虑转移式,发现好像直接想不好想,,,主要的问题在于这个*$i$,就很不好搞$QAQ$ 其实不难想到,$\s ...
- POJ 3070 Fibonacci矩阵快速幂 --斐波那契
题意: 求出斐波那契数列的第n项的后四位数字 思路:f[n]=f[n-1]+f[n-2]递推可得二阶行列式,求第n项则是这个矩阵的n次幂,所以有矩阵快速幂模板,二阶行列式相乘, sum[ i ] [ ...
- POJ3070:Fibonacci(矩阵快速幂模板题)
http://poj.org/problem?id=3070 #include <iostream> #include <string.h> #include <stdl ...
- hdu 3306 Another kind of Fibonacci 矩阵快速幂
参考了某大佬的 我们可以根据(s[n-2], a[n-1]^2, a[n-1]*a[n-2], a[n-2]^2) * A = (s[n-1], a[n]^2, a[n]*a[n-1], a[n-1] ...
随机推荐
- 浪漫爱心--第三方开源--PeriscopeLayout
点此下载 使用很简单,首先在xml里面添加 <Button android:id="@+id/btn_start" android:layout_width="wr ...
- Mac 下 Mosquitto 安装和配置 (Mosquitto为开源的mqtt服务器)
官网:http://mosquitto.org/download/ 官网的介绍简单明了 Mac 下一个命令“brew install mosquitto” 安装成功了,还学会了brew 安装目录:/u ...
- HTTP协议 与 Requests库
HTTP协议 与 Requests库: 1 HTTP协议: 2 URL作为网络定位的标识: >>>> 用户通过url来定位资源 >>>> 然后通过 g ...
- NET Core 2.0利用MassTransit集成RabbitMQ
NET Core 2.0利用MassTransit集成RabbitMQ https://www.cnblogs.com/Andre/p/9579764.html 在ASP.NET Core上利用Mas ...
- 《Javascript高级程序设计》阅读记录(二):第四章
这个系列之前文字地址:http://www.cnblogs.com/qixinbo/p/6984374.html 这个系列,我会把阅读<Javascript高级程序设计>之后,感觉讲的比较 ...
- The Suspects (并查集)
个人心得:最基础的并查集经典题.借此去了解了一下加深版的即加权并查集,比如食物链的题目,这种题目实行起来还是有 一定的难度,不仅要找出与父节点的关系,还要在路径压缩的时候进行更新,这一点现在还是没那么 ...
- MLCC 电容的的 NP0 C0G 材质
MLCC 电容的的 NP0 C0G 材质 随手记一下. MLCC 中最稳定的材质 NP0 C0G,NP0 和 C0G 是相同的,只是不同的产商不同的名字而已. 注意中间的是 0 不是 英文字母 O,虽 ...
- 洛谷 4245 【模板】任意模数NTT——三模数NTT / 拆系数FFT
题目:https://www.luogu.org/problemnew/show/P4245 三模数NTT: 大概是用3个模数分别做一遍,用中国剩余定理合并. 前两个合并起来变成一个 long lon ...
- BZOJ3170:[TJOI2013]松鼠聚会
题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=3170 通过分析可以发现,题目所说的两点之间的距离就是切比雪夫距离. 两点之间欧几里得距离:\( ...
- Zabbix通过SNMPv2监控DELL服务器的硬件信息
(一)zabbix监控DELL服务器 (1)简述:监控DELL服务器硬件一般有两种途径:1.操作系统上安装OMSA,编写脚本调用omreport命令进行监控(需要在操作系统上安装比较麻烦):2.使用i ...