POJ 3070 Fibonacci 【矩阵快速幂】
<题目链接>
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
解题分析:
由于n很大,所以直接计算时不可行的,可以用矩阵快速幂来加速,并且,此题直接给出了矩阵的递推式,于是,我们只要按照题意构造矩阵即可。
#include <cstdio>
#include <cstring> const int mod=; struct Matrix{
int m[][];
Matrix(){}
Matrix(int x,int y,int z,int k){
m[][]=x;
m[][]=y;
m[][]=z;
m[][]=k;
}
}; Matrix Mul(Matrix a,Matrix b){
Matrix temp;
for(int i=;i<;i++){
for(int j=;j<;j++){
temp.m[i][j]=;
for(int k=;k<;k++){
temp.m[i][j]=(temp.m[i][j]+a.m[i][k]%mod*b.m[k][j]%mod)%mod;
}
}
}
return temp;
} Matrix pow_Mul(Matrix x,int c){
Matrix tmp(,,,);
while(c>){
if(c&){
tmp=Mul(tmp,x);
}
c>>=;
x=Mul(x,x);
}
return tmp;
} int main(){
int n;
while(scanf("%d",&n)!=EOF){
if(n==-)break;
int f[]={,,,};
if(n<=){
printf("%d\n",f[n]);
continue;
}
Matrix init(f[],f[],f[],f[]);
Matrix tmp(,,,);
Matrix ans=Mul(pow_Mul(tmp,n-),init);
printf("%d\n",ans.m[][]%mod);
}
return ;
}
2018-08-21
POJ 3070 Fibonacci 【矩阵快速幂】的更多相关文章
- 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 ...
- POJ 3070 Fibonacci 矩阵快速幂模板
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18607 Accepted: 12920 Descr ...
- POJ 3070 Fibonacci矩阵快速幂 --斐波那契
题意: 求出斐波那契数列的第n项的后四位数字 思路:f[n]=f[n-1]+f[n-2]递推可得二阶行列式,求第n项则是这个矩阵的n次幂,所以有矩阵快速幂模板,二阶行列式相乘, sum[ i ] [ ...
- HDU 1588 Gauss Fibonacci(矩阵快速幂)
Gauss Fibonacci Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- POJ——3070Fibonacci(矩阵快速幂)
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12329 Accepted: 8748 Descri ...
- UVA - 10229 Modular Fibonacci 矩阵快速幂
Modular Fibonacci The Fibonacci numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 3 ...
- POJ 3744 【矩阵快速幂优化 概率DP】
搞懂了什么是矩阵快速幂优化.... 这道题的重点不是DP. /* 题意: 小明要走某条路,按照个人兴致,向前走一步的概率是p,向前跳两步的概率是1-p,但是地上有地雷,给了地雷的x坐标,(一维),求小 ...
- poj3070 Fibonacci 矩阵快速幂
学了线代之后 终于明白了矩阵的乘法.. 于是 第一道矩阵快速幂.. 实在是太水了... 这差不多是个模板了 #include <cstdlib> #include <cstring& ...
- poj 3735 稀疏矩阵矩阵快速幂
设人数为 $n$,构造 $(n + 1) \times (n + 1)$ 的矩阵 得花生:将改行的最后一列元素 $+ 1$ \begin{gather}\begin{bmatrix}1 & 0 ...
随机推荐
- JavaScript之正方教务系统自动化教评[插件-转载]
[声明]本插件系学院学长原创,非博主所创,发布此处,仅供学习和效仿. /** * @name:正方教务系统自动化教评-插件 * * @author:chenzhongshu * @date:2017- ...
- 寻路优化(一)——二维地图上A*启发函数的设计探索
工作中需要优化A*算法,研究了一天,最后取得了不错的效果.看网上的朋友还没有相关的研究,特此记录一下.有错误欢迎大家批评指正.如需转载请注明出处,http://www.cnblogs.com/Leon ...
- ANR异常及traces信息解析
Application Not Responding:默认情况下,在android中Activity的最长执行时间是5秒,BroadcastReceiver的最长执行时间则是10秒.超出就会提示应用程 ...
- 【apache tika】apache tika获取文件内容(与FileUtils的对比)
Tika支持多种功能: 文档类型检测 内容提取 元数据提取 语言检测 重要特点: 统一解析器接口:Tika封装在一个单一的解析器接口的第三方解析器库.由于这个特征,用户逸出从选择合适的解析器库的负担, ...
- JSON的理解
官方解释: JSON的全称是”JavaScript Object Notation”,单单从字面上的理解就是JavaScript对象表示法,它是一种基于文本,独立于语言的轻量级数据交换格式. 理解: ...
- ROS 多台计算机联网控制机器人
0. 时间同步 sudo apt-get install chrony 1. ubuntu自带的有openssh-client 可以通过如下指令 ssh username@host 来连接同一局域网内 ...
- 每天一个linux命令【转】
转自:http://www.cnblogs.com/peida/archive/2012/12/05/2803591.html 开始详细系统的学习linux常用命令,坚持每天一个命令,所以这个系列为每 ...
- openstack swift节点安装手册3-最后的安装配置及验证
以下步骤都在controller节点上执行 1.远程获取/etc/swift/swift.conf文件: curl -o /etc/swift/swift.conf https://git.opens ...
- linux windows 传输文件
其中两种方式,当然,只是我自己试验的两个,其实还有别的方法,但是我也懒得实践了. 1 pscp c:\abc.sql root@192.168.1.1:/home/person/hww 2 Lrz ...
- zabbix3.0监控centos当主机cpu使用率超过90%的时候报警
在windows系统中监控cpu利用率非常容易,自带模板就有这样的功能,但是在linux里面没有默认的模板 只有cpu的负载,默认当cpu的负载在一定时间内5以上报警 cpu utilization中 ...