POJ——3070Fibonacci(矩阵快速幂)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 12329 | Accepted: 8748 |
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:
.
看了很多关于矩阵快速幂的题解,感觉矩阵快速幂得到的是一个含有多个项的矩阵,而答案只是其中一项,而且之还需要特判指数。入门的矩阵快速幂题。感受一下再写其它的题目。拿这题为例。题目中给出的项从0开始,F0=0,F1=1,F2=1,F3=2。。就是一个斐波那契的数列。由于用矩阵,那至少要两项来组成矩阵,根据他的递推公式。可以得到
[Fn,Fn-1]=[1*Fn-1+1*Fn-2,1*Fn-1+0*Fn-2]
代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
struct mat
{
LL m[2][2];
mat(){memset(m,0,sizeof(m));}
};
mat cheng(mat a,mat b)
{
mat c;
for (int i=0 ;i<2; i++)
{
for (int j=0; j<2; j++)
{
for (int k=0; k<2; k++)
{
c.m[i][j]+=(a.m[i][k]*b.m[k][j])%10000;
}
}
}
return c;
}
mat zxc(mat a,LL b)
{
mat c;
c.m[0][0]=c.m[1][1]=1;
while (b!=0)
{
if(b&1)
c=cheng(c,a);
a=cheng(a,a);
b>>=1;
}
return c;
}
int main(void)
{
LL n;
while (cin>>n&&n!=-1)
{
mat one;
if(n==0)
{
cout<<0<<endl;
continue;
}
else if(n==1)
{
cout<<1<<endl;
continue;
}
one.m[0][0]=one.m[1][0]=one.m[0][1]=1;
one=zxc(one,n-1);
cout<<one.m[0][0]%10000<<endl;;
}
return 0;
}
POJ——3070Fibonacci(矩阵快速幂)的更多相关文章
- poj 3233 矩阵快速幂
地址 http://poj.org/problem?id=3233 大意是n维数组 最多k次方 结果模m的相加和是多少 Given a n × n matrix A and a positive i ...
- poj 3734 矩阵快速幂+YY
题目原意:N个方块排成一列,每个方块可涂成红.蓝.绿.黄.问红方块和绿方块都是偶数的方案的个数. sol:找规律列递推式+矩阵快速幂 设已经染完了i个方块将要染第i+1个方块. a[i]=1-i方块中 ...
- POJ 3070 矩阵快速幂解决fib问题
矩阵快速幂:http://www.cnblogs.com/atmacmer/p/5184736.html 题目链接 #include<iostream> #include<cstdi ...
- 解题报告:poj 3070 - 矩阵快速幂简单应用
2017-09-13 19:22:01 writer:pprp 题意很简单,就是通过矩阵快速幂进行运算,得到斐波那契数列靠后的位数 . 这是原理,实现部分就是矩阵的快速幂,也就是二分来做 矩阵快速幂可 ...
- POJ 3070 矩阵快速幂
题意:求菲波那切数列的第n项. 分析:矩阵快速幂. 右边的矩阵为a0 ,a1,,, 然后求乘一次,就进一位,求第n项,就是矩阵的n次方后,再乘以b矩阵后的第一行的第一列. #include <c ...
- POJ 3233 矩阵快速幂&二分
题意: 给你一个n*n的矩阵 让你求S: 思路: 只知道矩阵快速幂 然后nlogn递推是会TLE的. 所以呢 要把那个n换成log 那这个怎么搞呢 二分! 当k为偶数时: 当k为奇数时: 就按照这么搞 ...
- poj 3744 矩阵快速幂+概率dp
题目大意: 输入n,代表一位童子兵要穿过一条路,路上有些地方放着n个地雷(1<=n<=10).再输入p,代表这位童子兵非常好玩,走路一蹦一跳的.每次他在 i 位置有 p 的概率走一步到 i ...
- Blocks(POJ 3734 矩阵快速幂)
Blocks Input The first line of the input contains an integer T(1≤T≤100), the number of test cases. E ...
- Poj 3233 矩阵快速幂,暑假训练专题中的某一道题目,矩阵快速幂的模板
题目链接 请猛戳~ Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 ...
- poj 3233 矩阵快速幂+YY
题意:给你矩阵A,求S=A+A^1+A^2+...+A^n sol:直接把每一项解出来显然是不行的,也没必要. 我们可以YY一个矩阵: 其中1表示单位矩阵 然后容易得到: 可以看出这个分块矩阵的左下角 ...
随机推荐
- 这些O2O比你们更靠谱儿
本文纯属虚构,如有雷同,全是 C2C(Copy to China). 一 「什么社区 O2O,不就是跑腿儿的?那叮*小区不好好跑腿儿,非要搞什么狗屁社交,不是死了?」 三十四岁的老刘咽了口唾沫,接着跟 ...
- Mycat实现读写分离、分库分表
系统开发中,数据库是非常重要的一个点.除了程序的本身的优化,如:SQL语句优化.代码优化,数据库的处理本身优化也是非常重要的.主从.热备.分表分库等都是系统发展迟早会遇到的技术问题问题.Mycat是一 ...
- C#访问数组元素
在C#中,使用索引来访问数组元素.索引必须是一个整型值. 在数组中,每一个维度的索引从0开始. 一.访问一维数组元素 int[] array = {1,2,3,4,5,6,7,8,9,10}; // ...
- vue_music:封装scroll.vue组件
在项目中经常用到滚动,结合Better-scroll封装了sroll.vue组件参考链接:https://ustbhuangyi.github.io...http://www.imooc.com/ar ...
- FSMC原理通俗解释
所以不用GPIO口直接驱动液晶,是因为这种方法速度太慢,而FSMC是用来外接各种存储芯片的,所以其数据通信速度是比普通GPIO口要快得多的.TFT-LCD 驱动芯片的读写时序和SRAM的差不多,所以就 ...
- JAVA-基础(一)
1.一个变量可以声明为final,这样做的目的是阻止它的内容被修改.这意味着在声明final 变量的时候,你必须初始化它(在这种用法上,final类似于C/C++中的const). 例如: final ...
- JS实现——贪吃蛇
把以下代码保存成Snake.html文件,使用Google或360浏览器打开 <!DOCTYPE HTML> <html> <head> <meta char ...
- JavaScript 将当地时间转换成其它时区
毫无疑问,用JavaScript脚本可以通过直接查看用户的时钟,方便地在网页上显示本地时间. 但是,如果你想显示不同地区的时间—--例如,如果你的本部在别的国家,你想查看“本国”时间而非当地时间,又该 ...
- curl download zip file
https://askubuntu.com/questions/285976/download-zip-file-with-curl-command
- python面试题解析(前端、框架和其他)
答: HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和扩展.目前在WWW中使用的是HTTP/1. ...