POJ3070 Fibonacci[矩阵乘法]【学习笔记】

时间:2022-05-28 01:06:19
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

POJ3070 Fibonacci[矩阵乘法]【学习笔记】.

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

POJ3070 Fibonacci[矩阵乘法]【学习笔记】.

Also, note that raising any 2 × 2 matrix to the 0th power gives the identity matrix:

POJ3070 Fibonacci[矩阵乘法]【学习笔记】.

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&lbrack;矩阵乘法&rsqb;

    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&colon;Fibonacci&lpar;矩阵快速幂模板题)

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

  6. &lbrack;学习笔记&rsqb;矩阵乘法及其优化dp

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

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

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

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

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

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

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

随机推荐

  1. css实现背景颜色线性渐变

    http://www.qttc.net/201304316.html http://www.ruanyifeng.com/blog/2008/05/css_background_image_posit ...

  2. 红黑树&lpar;二&rpar;之 C语言的实现

    概要 红黑树在日常的使用中比较常用,例如Java的TreeMap和TreeSet,C++的STL,以及Linux内核中都有用到.之前写过一篇文章专门介绍红黑树的理论知识,本文将给出红黑数的C语言的实现 ...

  3. mysql 64 zip download

    open the url  ::  http://dev.mysql.com/downloads/file/?id=461109 and click the location "no tha ...

  4. 多备份CEO胡茂华:创业路上的五道坎

    本文由多备份CEO胡茂华记述,授权南七道发表,未做删改.胡茂华:腾讯第116号员工,历任腾讯总监.盛大CTO (旅游).1号店技术副总裁.现担任云服务提供商多备份联合创始人&CEO. 2014 ...

  5. 路径R

    定义文件路径时前面加个r 例如 firstfolder = r"C:\Users\1261\Desktop\" 不对其中的符号进行转义

  6. 动态iframe 分享

    function createIframe() { var i = document.createElement("iframe"); i.src = "/RSK/Rsk ...

  7. 跨域、跨服务器调用时候session丢失的问题

    最近新进一个公司,做的项目是手机支付系统.由于涉及到金钱相关的,所以安全性要求特别的高.项目分了很多个子系统,在部署(测试)的时候是每个Tomcat上面只放一个子系统.比如现在有5个子系统,那么就会对 ...

  8. ETL第一篇&lpar;Kettle Spoon&rpar; 初遇

    ETL第一篇(Kettle Spoon) 初遇 ETL第二篇 调用webservice 简介 Kettle 是一款国外开源的 ETL 工具,纯 Java 编写,绿色无需安装,数据抽取高效稳定(数据迁移 ...

  9. spring3&colon; 基于Schema的AOP

    6.3  基于Schema的AOP 基于Schema的AOP从Spring2.0之后通过“aop”命名空间来定义切面.切入点及声明通知. 在Spring配置文件中,所以AOP相关定义必须放在<a ...

  10. Symfony 没有找到数据库驱动An exception occured in driver&colon; could not find driver

    如果一直报这个错误, 第一,你本地没有相关的数据库驱动(mysql:-->pdo_myql,postgresql-->pdo_pgsql等); 需要执行 php -m|grep -i pd ...