地址 http://poj.org/problem?id=3070

大意是输入一个数字 输出位于Fibonacci数列该位置的数字模10000的结果

由于n比较大 0 ≤ n ≤ 1,000,000,000 所以开数组是不可能了 只能实时计算

使用矩阵可以加速Fibonacci数列的推导

经过精心设置的矩阵相乘是可以从Fn-1 Fn-2推导出Fn的

那么设置好的矩阵的多次相乘是不是就可以从F0  F1推到出Fn呢?

是的 如图

1 1

1 0 矩阵为A那么

A^(n-1) 与F1 F0矩阵的乘法就是可以推到出 Fn

代码借用了之前的快速幂代码 不是模板 所以虽然可以AC但是代码复用性不好 先学理论 板子日后再找

 #include <iostream>
#include <vector>
#include <cstring> using namespace std; struct matrix {
int data[][];
}; int n = ;
int m = ;
int k = ; //矩阵乘法
matrix mul(matrix a, matrix b)
{
matrix c;
memset(c.data, , sizeof(c.data));
for (int i = ; i <= n; i++) {
for (int j = ; j <= n; j++) {
for (int k = ; k <= n; k++) {
c.data[i][j] = (c.data[i][j] + 1ll * a.data[i][k] * b.data[k][j]) % m;
}
}
} return c;
} //矩阵加法
matrix add(matrix a, matrix b) {
for (int i = ; i <= n; i++) {
for (int j = ; j <= n; j++) {
a.data[i][j] = (a.data[i][j] + b.data[i][j]) % m;
}
}
return a;
} //矩阵快速幂
matrix quickpow(matrix a, int k) {
matrix c;
memset(c.data, , sizeof(c.data));
for (int i = ; i <= n; i++)
c.data[i][i] = ;
while (k) {
if (k & ) c = mul(c, a);
k >>= ;
a = mul(a, a);
}
return c;
} int main()
{
int j;
while () {
cin >> j;
if (j == -) break;
if (j == ) {
cout << << endl; continue;
}
if (j == || j == ) {
cout << << endl; continue;
}
matrix base;
base.data[][] = ; base.data[][] = ;
base.data[][] = ; base.data[][] = ; matrix fn;
fn.data[][] = ;
fn.data[][] = ; matrix baseN = quickpow(base, j-); matrix c;
memset(c.data, , sizeof(c.data)); for (int i = ; i <= ; i++) {
for (int j = ; j <= ; j++) {
for(int k =;k<=;k++){
c.data[i][j] = (c.data[i][j] + 1ll * baseN.data[i][k] * fn.data[k][j]) % m;
}
}
}
cout << c.data[][] << endl;
}
return ;
}

ac code

poj 3070 矩阵计算Fibonacci的更多相关文章

  1. 【POJ 3070】 Fibonacci

    [题目链接]            点击打开链接 [算法]           矩阵乘法快速幂 [代码] #include <algorithm> #include <bitset& ...

  2. 矩阵快速幂 POJ 3070 Fibonacci

    题目传送门 /* 矩阵快速幂:求第n项的Fibonacci数,转置矩阵都给出,套个模板就可以了.效率很高啊 */ #include <cstdio> #include <algori ...

  3. POJ 3070 Fibonacci(矩阵高速功率)

    职务地址:POJ 3070 用这个题学会了用矩阵高速幂来高速求斐波那契数. 依据上个公式可知,第1行第2列和第2行第1列的数都是第n个斐波那契数.所以构造矩阵.求高速幂就可以. 代码例如以下: #in ...

  4. poj 3070 && nyoj 148 矩阵快速幂

    poj 3070 && nyoj 148 矩阵快速幂 题目链接 poj: http://poj.org/problem?id=3070 nyoj: http://acm.nyist.n ...

  5. POJ 3070 + 51Nod 1242 大斐波那契数取余

    POJ 3070 #include "iostream" #include "cstdio" using namespace std; class matrix ...

  6. (矩阵快速幂) Fibonacci -- poj -- 3070

    链接: http://poj.org/problem?id=3070   Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  7. 矩阵经典题目六:poj 3070 Fibonacci

    http://poj.org/problem?id=3070 按已构造好的矩阵,那么该矩阵的n次方的右上角的数便是f[n]. #include <stdio.h> #include < ...

  8. POJ 3070 Fibonacci

    Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...

  9. POJ ---3070 (矩阵乘法求Fibonacci 数列)

    Fibonacci   Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2  ...

随机推荐

  1. C# 使用System.Media.SoundPlayer播放wav格式的声音文件

    using System.Media; string szPath = Application.StartupPath + “\\SoundFile\\sound.wav”; SoundPlayer ...

  2. WPF无边框可拖动窗体

    下面主要记录下创建无边框窗体,并且可以拖动.这种窗体主要用于弹出小窗体时. <Window x:Class="WpfApplication1.MainWindow" xmln ...

  3. (转)Python中的常见特殊方法—— repr方法

    原文链接:https://www.cnblogs.com/tizer/p/11178473.html 在Python中有些方法名.属性名的前后都添加了双下划线,这种方法.属性通常都属于Python的特 ...

  4. springboot向elk写日志

    springboot里连接elk里的logstash,然后写指定index索引的日志,而之后使用kibana去查询和分析日志,使用elasticsearch去保存日志. 添加引用 implementa ...

  5. Oracle VirtualBox安装CentOS 8

    1.下载CentOS CentOS下载地址: https://wiki.centos.org/Download 这里以CentOS8为例 选择一个比较快的地址,这里以jdcloud mirror为例 ...

  6. [20191127]探究等待事件的本源4.txt

    [20191127]探究等待事件的本源4.txt --//昨天使用ash_wait_chains.sql脚本把各个生产库执行1遍,才发现我对一套系统性能理解错误.--//我一直以为这套系统存储有点问题 ...

  7. ORA-27468: ""."" is locked by another process

    You have a scheduler job that generated an error. When the error occurred, you attempted to disable ...

  8. python捕捉详细异常堆栈的方法

    python中有 try——except 的方法捕获异常,可以获取到异常的种类以及自定义异常, 但是有时候对于debug测试来说,信息不全,比如说 触发异常的具体位置在哪: import traceb ...

  9. SSH的 Write failed: Broken pipe 问题

    问题现象: 表示连接管道已经断开 解决方法: 方法一:客户端配置 在客户端的 ~/.ssh/ config文件(如不存在请自行创建)中添加下面内容: ServerAliveInterval 60 方法 ...

  10. 华为eNSP路由交换实验-生成树之RSTP

    RSTP基础配置 实验拓扑图 实验步骤 1.基本配置 根据实验编址表进行相应的基本IP配置. 2.配置RSTP基本功能. (1)把生成树模式由默认的MSTP(华为交换机默认开启)改为RSTP. [FW ...