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] ...
随机推荐
- 【VS2013编译DirectX Tutorials时遇到的错误】FXC : error X3501: 'main': entrypoint not found
修改于2015年9月6日: 去年写这篇解决方案的时候其实对着色器编程还一知半解,摸索了一个治标不治本的方法解决问题,结果被一个CSDN的博主原封不动抄了去,还打上个原创的标签= =,简直无语... 最 ...
- SSM整合(spring、springMVC、mybatis)
需要用的包: 包括:spring的包.springMVC的包.mybatis的包.数据库驱动包.json相关的包 配置如下,首先是mybatis的配置 <?xml version="1 ...
- 清新大气的ListView下拉上拉刷新--第三方开源--PullDownListView
下载地址:https://github.com/guojunyi/PullDownListView 使用: xml: <com.pulldownlistview.PullDownListView ...
- 26 python 并发编程之多进程理论
一 什么是进程 进程:正在进行的一个过程或者说一个任务.而负责执行任务则是cpu. 举例(单核+多道,实现多个进程的并发执行): egon在一个时间段内有很多任务要做:python备课的任务,写书的任 ...
- 【SQL查询】查询的列起别名_AS
方法一: 以as关键字指定字段别名,as在select的字段和别名之间. 方法二: 直接在字段名称后面加上别名,中间以空格隔开.
- uva11489 - Integer Game(考思维,找规律)
从S开始时只能是两种情况: 1.现在总和已经是3的倍数了,那么因为每人每次只能拿走一个数,要保持拿走之后依然是3的倍数,那么就只能拿3,6,9这类数,用num统计一下,看看num奇偶性就知道谁最后拿了 ...
- NOI模拟赛 #4
好像只有一个串串题可以做... 不会 dp 和数据结构啊 QAQ 10 + 20 + 100 = 130 T1 一棵树,每个点有一个能量的最大容量 $l_i$ 和一个增长速度 $v_i$,每次可以选一 ...
- 70. Climbing Stairs Add to List
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- MySQL 利用xtrabackup进行增量备份详细过程汇总 (转)
Xtrabackup下载.安装以及全量备份请参考:http://blog.itpub.net/26230597/viewspace-1465772/ 1,创建mysql备份用户 mysql -uroo ...
- python-snappy的安装小记
在弄个dota2的replay parser玩玩,在github上找到了几个,都是基于V社的demoinfo2(https://developer.valvesoftware.com/wiki/Dot ...