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] ...
随机推荐
- html5 video 监听播放结束. 最好获取html标签而不是id。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- UVALive 3708 Graveyard(思维题)
将原有的每个雕塑的坐标位置,映射在一个总长为n+m的数轴上,设第一个点的坐标为0,(新的等分点必然有至少有一个和原来n等分的等分点重合,因为等分点可以等距的绕圆周旋转,总可以转到有至少一个重合的,不妨 ...
- 线程存储(Thread Specific Data)
线程中特有的线程存储, Thread Specific Data .线程存储有什么用了?他是什么意思了? 大家都知道,在多线程程序中,所有线程共享程序中的变量.现在有一全局变量,所有线程都可以使用它, ...
- HihoCoder1181欧拉路(Fleury算法求欧拉路径)
描述 在上一回中小Hi和小Ho控制着主角收集了分散在各个木桥上的道具,这些道具其实是一块一块骨牌. 主角继续往前走,面前出现了一座石桥,石桥的尽头有一道火焰墙,似乎无法通过. 小Hi注意到在桥头有一张 ...
- leetcode_sql_1,176,177
1.176题目,Second Highest Salary,https://leetcode.com/problems/second-highest-salary/#/description Writ ...
- Storm实时计算:流操作入门编程实践
转自:http://shiyanjun.cn/archives/977.html Storm实时计算:流操作入门编程实践 Storm是一个分布式是实时计算系统,它设计了一种对流和计算的抽象,概念比 ...
- Maven错误之 Check $M2_HOME environment variable
Eclipse中使用maven插件的时候,运行run as maven build的时候报错 -Dmaven.multiModuleProjectDirectory system propery is ...
- Js中获取键盘的事件
使用方法: <script type="text/javascript" language=JavaScript charset="UTF-8"> ...
- BZOJ1202:[HNOI2005]狡猾的商人
浅谈并查集:https://www.cnblogs.com/AKMer/p/10360090.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php? ...
- ORA-12514: TNS: no listener 解决方案
服务端:oracle 11g 客户端: pl/sql 问题描述: 用客户端 pl/sql 连接登录的时候,提示 "ORA-12514: TNS: no listener". 在服务 ...