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
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.
.
题解:
按题目要求很容易写出矩阵
F[n] 0 1 1
F[n-1] 0 1 0
直接上矩阵快速幂即可
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long ll;
const int mod=;
int n;
struct matrix
{
ll a[][];
matrix(){for(int i=;i<=;i++)for(int j=;j<=;j++)a[i][j]=;}
matrix(ll b[][]){for(int i=;i<=;i++)for(int j=;j<=;j++)a[i][j]=b[i][j];}
inline matrix operator *(matrix p){
matrix tmp;
for(int i=;i<=;i++)
for(int j=;j<=;j++){
tmp.a[i][j]=;
for(int k=;k<=;k++)
tmp.a[i][j]+=a[i][k]*p.a[k][j],tmp.a[i][j]%=mod;
}
return tmp;
}
};
ll work(){
if(n==)return ;
if(n==||n==)return ;
n-=;
ll t[][]={{,,},{,,},{,,}};ll sum[][]={{,,},{,,},{,,}};
matrix S=matrix(t),T=matrix(sum);
while(n){
if(n&)S=S*T;
T=T*T;n>>=;
}
return S.a[][];
}
int main()
{
while(scanf("%d",&n))
{
if(n==-)break;
printf("%lld\n",work());
}
return ;
}
poj 3070 Fibonacci 矩阵快速幂的更多相关文章
- poj 3070 Fibonacci (矩阵快速幂乘/模板)
题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...
- 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 ...
随机推荐
- org.hibernate.hibernate.connection.release_mode
org.hibernate.connection包的主要封装了通过JDBC来连接数据库的操作,用户可以以数据源的方式,或者通过特定数据库驱动的方式,甚至是自己定义连接类的方式来完成数据库的连接操作,包 ...
- 51Nod P1100 斜率最大
传送门: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1100 由于2 <= N <= 10000, 所以 ...
- JAVA_SE基础——61.字符串入门
public class Demo1 { public static void main(String[] args) { String str1 = "hello"; Strin ...
- volt问题
1./表示当前目录:/college/detail/{{ item.sid }}表示这个路径超链接,url实在不好写就不写,作为开发人员想怎么弄就怎么弄最后发布是项目主管的事 2.不需要服务器给值,直 ...
- js判断语句关于true和false后面跟数字或字符串的问题
我经常在代码中看到很长串判断,看到就头疼,简单的整理一下. 比如:(client.top>=0&&client.left>=0&&client.bottom ...
- python flask框架 tempates 模版的使用
在py文件同级下 建立templates文件夹,再文件夹中编写html文件 1 向模版中传递参数: ''' 1 向模板传送 参数 ''' @app.route('/') def index(): na ...
- Java课后练习
1.利用循环输出:************************* public class Shape { public static void main(String[] args) { for ...
- 改变textField的placeholder的颜色和位置
重写UItextField的这个方法,用其他的textField继承自这个父类 - (void) drawPlaceholderInRect:(CGRect)rect { [[UIColor blue ...
- python/零起点(一、字典)
python/零起点(一.字典) dict( )字典 dict()强型转换成字典类型的数据类型: 字典的键(Key)必须是唯一不可变的 字典是无序,字典可变数据类型,且是可迭代的对象 字典清空操作案例 ...
- spark2.1:在RDD[unit].foreach(s=>{})内部调用sparkSession对象抛出NullPointException
问题代码: val sample_data_combine_result=List( (0,(List(FitModel(4022,1447.92,-8.38983306721434,2.0),Fit ...