Fibonacci
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13677   Accepted: 9697

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


矩阵乘法的应用
一个有趣的理解:
结果矩阵第m行与第n列交叉位置的那个值,等于第一个矩阵第m行与第二个矩阵第n列,对应位置的每个值的乘积之和
 
白书上的一句:
把一个向量v变成另一个向量v1,并且v1的每一个分量都是v各个分量的线性组合,考虑使用矩阵乘法
对于斐波那契数列,构造矩阵
1 1
1 0
然后
让矩阵
A     1 1
B     1 0
相乘,就是得到
A+B
A
就是斐波那契数列啊
快速幂来优化到logn
 
遇到一个n很大的DP/递推关系,都可以考虑用这种方法
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int MOD=1e4;
typedef long long ll;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n;
struct mat{
int r,c;
int m[][];
mat(){r=;c=;memset(m,,sizeof(m));}
}im,f;
mat mult(mat x,mat y){
mat t;
for(int i=;i<=x.r;i++)
for(int k=;k<=x.c;k++) if(x.m[i][k])
for(int j=;j<=y.c;j++)
t.m[i][j]=(t.m[i][j]+x.m[i][k]*y.m[k][j]%MOD)%MOD;
return t;
}
void init(){
for(int i=;i<=im.c;i++)
for(int j=;j<=im.r;j++)
if(i==j) im.m[i][j]=;
f.m[][]=;f.m[][]=;
f.m[][]=;f.m[][]=;
}
int main(){
init();
while((n=read())!=-){
mat ans=im,t=f;
for(;n;n>>=,t=mult(t,t))
if(n&) ans=mult(ans,t);
printf("%d\n",ans.m[][]);
}
}

POJ3070 Fibonacci[矩阵乘法]【学习笔记】的更多相关文章

  1. POJ3070 Fibonacci[矩阵乘法]

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13677   Accepted: 9697 Descri ...

  2. 【poj3070】矩阵乘法求斐波那契数列

    [题目描述] 我们知道斐波那契数列0 1 1 2 3 5 8 13…… 数列中的第i位为第i-1位和第i-2位的和(规定第0位为0,第一位为1). 求斐波那契数列中的第n位mod 10000的值. [ ...

  3. poj3070 Fibonacci 矩阵快速幂

    学了线代之后 终于明白了矩阵的乘法.. 于是 第一道矩阵快速幂.. 实在是太水了... 这差不多是个模板了 #include <cstdlib> #include <cstring& ...

  4. Eigen 矩阵库学习笔记

    最近为了在C++中使用矩阵运算,简单学习了一下Eigen矩阵库.Eigen比Armadillo相对底层一点,但是只需要添加头文库即可使用,不使用额外的编译和安装过程. 基本定义 Matrix3f是3* ...

  5. POJ3070:Fibonacci(矩阵快速幂模板题)

    http://poj.org/problem?id=3070 #include <iostream> #include <string.h> #include <stdl ...

  6. [学习笔记]矩阵乘法及其优化dp

    1.定义: $c[i][j]=\sum a[i][k]\times b[k][j]$ 所以矩阵乘法有条件,(n*m)*(m*p)=n*p 即第一个矩阵的列数等于第二个矩阵的行数,否则没有意义. 2.结 ...

  7. 学习心得:《十个利用矩阵乘法解决的经典题目》from Matrix67

    本文来自:http://www.matrix67.com/blog/archives/tag/poj大牛的博文学习学习 节选如下部分:矩阵乘法的两个重要性质:一,矩阵乘法不满足交换律:二,矩阵乘法满足 ...

  8. UFLDL深度学习笔记 (二)SoftMax 回归(矩阵化推导)

    UFLDL深度学习笔记 (二)Softmax 回归 本文为学习"UFLDL Softmax回归"的笔记与代码实现,文中略过了对代价函数求偏导的过程,本篇笔记主要补充求偏导步骤的详细 ...

  9. UFLDL深度学习笔记 (七)拓扑稀疏编码与矩阵化

    UFLDL深度学习笔记 (七)拓扑稀疏编码与矩阵化 主要思路 前面几篇所讲的都是围绕神经网络展开的,一个标志就是激活函数非线性:在前人的研究中,也存在线性激活函数的稀疏编码,该方法试图直接学习数据的特 ...

随机推荐

  1. springSecurity入门小demo--配置文件xml的方式

    本例子只是一个最最最简单的入门demo,重点讲解xml的配置参数的意思和遇到的坑,主要的功能有: 自定义登录页面,错误页面 配置角色 csrf-403报错解决方法(加上一行代码配置就ok) 后台ifr ...

  2. jQuery 实现 bootstrap 模态框 删除确认

    思路: 点击删除按钮,通过jquery将删除操作的URL赋值到页面URL元素,并弹出会话框 用户点击确认,通过jquery获取URL,并发送删除请求至后台. 一.删除button <a clas ...

  3. HTML5 拖拽实现

    简介: 最早在网页中引入JavaScript拖放功能是IE4.当时,网页中只有两种对象可以拖放:图像和某些文本.拖放图像时,把鼠标放到图像上,按住鼠标不放就可以拖放它.拖放文本时,要先选中文本,然后可 ...

  4. SQL Server 基础之《学生表-教师表-课程表-选课表》(二)

    表结构 --学生表tblStudent(编号StuId.姓名StuName.年龄StuAge.性别StuSex) --课程表tblCourse(课程编号CourseId.课程名称CourseName. ...

  5. Java SSM框架之MyBatis3(二)MyBatis之Mapper代理的开发方式

    Mapper代理的开发规范 1. mapper接口的全限定名要和mapper映射文件的namespace值一致. 2. mapper接口的方法名称要和mapper映射文件的statement的id一致 ...

  6. Linux中普通用户提权为超级用户

    首先创建一个普通用户,并且给普通用户设置一个密码,保证能用su 命令能用普通用户登录 [root@ahu ~]# useradd test [root@ahu ~]# passwd test New ...

  7. Useful Online Resources for New Hackers

    出处:https://www.hackerone.com/blog/resources-for-new-hackers HackerOne喜欢花时间与活跃的黑客和有兴趣学习如何破解的人交谈. 就在上周 ...

  8. hadoop控制map个数(转)

    原文链接:https://blog.csdn.net/lylcore/article/details/9136555     hadooop提供了一个设置map个数的参数mapred.map.task ...

  9. 【转载】webstorm忽略node_modules目录

    转载自:http://www.cnblogs.com/chengwb/p/6183440.html 我在使用了cnpm后node_modules之前的层级目录变成了同一级目录,所以目录很多,造成web ...

  10. 使用react中遇到的问题

    引入antdesign中Carousel走马灯时遇到问题? Uncaught Error: Element ref was specified as a string (slick) but no o ...