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] ...
随机推荐
- 神经网络卷积层 要回计算output的维度 input 28 卷积是3x3 则output是26 但是channel是卷积核的数量
model = Sequential() model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_s ...
- 逻辑斯蒂(logistic)回归深入理解、阐述与实现
第一节中说了,logistic 回归和线性回归的区别是:线性回归是根据样本X各个维度的Xi的线性叠加(线性叠加的权重系数wi就是模型的参数)来得到预测值的Y,然后最小化所有的样本预测值Y与真实值y'的 ...
- LeetCode OJ:Path Sum(路径之和)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- luogu 3375 KMP模板题
#include<bits/stdc++.h> using namespace std; ,M = ; int next[N]; void getNext(char s[]) //找nex ...
- ecmall中的分页问题
<ecmall>Ecmall系统自带的分页功能 在Ecmall的二次开发中,分页是必不可少的.这个系统已经自带了分页功能,下面来看看如何使用这个分页. 下面是一个自定义的类,用于查看订单的 ...
- HDU4585 Shaolin (STL和treap)
Shaolin HDU - 4585 Shaolin temple is very famous for its Kongfu monks.A lot of young men go to ...
- FastAdmin 中的 formatter flag 分析
FastAdmin 中的 formatter flag 分析 效果 首先看看效果,这里的文字和颜色可以根据数据改变. 这是系统自带的分类管理. 代码 功能在 \public\assets\js\bac ...
- git之reset
有时候,一直用公司电脑开发的时候,顺顺利利.回到家,用自己笔记本,想改改代码,git pull后,发现一堆的unmerged或者其他冲突等问题. 明明只是git pull 一下,怎么会这么多问题. 这 ...
- liferay-ui:search-container reset cur page 当点列排序时,把当前页号重置为1.
问题描述: liferay里面要用liferay-ui:search-container 来展示结果集.并要求点列时,可以排序.然后,如果当前页数不为1时,点列排序,自动设置为1. 解决: // 列排 ...
- linux 标准化
Unix 1969 年诞生于 AT&T 贝尔实验室,并在 1973 年使用 C 语言进行了重写,从此就具有了很好的可移植性.但是当 AT&T 在 1984 年由于分拆而得以进入计算机领 ...