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. Hadoop生态圈-Flume的组件之拦截器与选择器

      Hadoop生态圈-Flume的组件之拦截器与选择器 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客只是配置的是Flume主流的Interceptors,想要了解更详细 ...

  2. Numpy - Pandas - Matplot 功能与函数名 速查

    用Python做数据分析,涉及到的函数实在是太多了,容易忘记,去网上查中文基本上差不到,英文有时候描述不清楚问题. 这里搞个针对个人习惯的函数汇总速查手册,下次需要用一个什么功能,就在这里面查到对应的 ...

  3. Spring Mvc + Maven + yuicompressor 使用 profile 来压缩 javascript ,css 文件; (十)

    profile相关知识点: 在开发项目时,设想有以下场景: 你的Maven项目存放在一个远程代码库中(比如github),该项目需要访问数据库,你有两台电脑,一台是Linux,一台是Mac OS X, ...

  4. 就for循环VS for-in循环

    这种模式的问题在于每次循环迭代的时候都要访问数据的长度.这样会使代码变慢,特别是当myarray不是数据,而是HTML容器对象时. HTML容器是DOM方法返回的对象,如: document.getE ...

  5. Javascript中与Scroll有关的方法

    这块确实太乱了,被兼容搞的简直快要晕死,默默地总结下... 与scroll相关的方法 4个window对象下:scrollX.scrollY.scrollTo.scroll(作用和scrollTo一样 ...

  6. webp实践的javascript检测方案

    function hasWebp () { // 查看Cookie,如果没有则进行以下逻辑 var img = new Image(); img.onload = handleSupport; img ...

  7. hadoop - hdfs 基础操作

    hdfs --help # 所有参数 hdfs dfs -help # 运行文件系统命令在Hadoop文件系统 hdfs dfs -ls /logs # 查看 hdfs dfs -ls /user/ ...

  8. C++单链表反转

    单链表反转笔记: #include<iostream> #include<string.h> using namespace std; struct ListNode { in ...

  9. 存储之磁盘阵列RAID

         存储之磁盘阵列RAID RAID是由美国加州大学伯克利分校的D.A. Patterson教授在1988年提出的.RAID名为独立冗余磁盘阵列(RedundantArray of Indepe ...

  10. 【ARTS】01_05_左耳听风-20181210~1216

    ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...