870 斐波那契进阶

题目链接:https://buaacoding.cn/problem/870/index

思路

通过读题就可以发现这不是一般的求斐波那契数列,所以用数组存下所有的答案是不现实的。题目也明确点明此题可以利用矩阵的计算解题。

如果你稍微百度一下你会了解到快速矩阵幂的概念。

什么是快速矩阵幂?

分析

快速矩阵幂算法是一种简单的具有典型意义的连续为离散算法,同学们一定要掌握其思想,而不是从网上copy一份板子就用。

时间复杂度:\(O(lgN)\);

考点:简单的快速矩阵幂;

坑点:一边计算一边取模才不会找过范围。

参考代码

//
// Created by AlvinZH on 2017/10/1.
// Copyright (c) AlvinZH. All rights reserved.
// #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <bitset>
#include <utility>
#include <functional>
#include <iomanip>
#include <sstream>
#include <ctime>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MaxSize 100005
#define MOD 10007
typedef long long LL;
using namespace std; const int N = 2; struct Matrix {
int mat[N][N];
Matrix() {}
Matrix operator * (const Matrix& b) const {
Matrix result;
memset(result.mat, 0, sizeof(result.mat));
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
for (int k = 0; k < N; ++k) {
result.mat[i][j] = (result.mat[i][j] + this->mat[i][k] * b.mat[k][j]) % MOD;
}
}
}
return result;
}
}; Matrix MatPow(Matrix base, int n)
{
Matrix result;
memset(result.mat, 0, sizeof(result.mat));
for (int i = 0; i < N; ++i) {
result.mat[i][i] = 1;
} while (n > 0)
{
if(n & 1) result = result * base;
base = base * base;
n >>= 1;
}
return result;
} int main()
{
Matrix base;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
base.mat[i][j] = 1;
}
}
base.mat[1][1] = 0;
int n;
while (~scanf("%d", &n))
{
Matrix ans = MatPow(base, n);
printf("%d\n", ans.mat[0][1]);
}
}

2016级算法第一次练习赛-C.斐波那契进阶的更多相关文章

  1. 2016级算法第一次练习赛-A.群鸦的盛宴

    858 群鸦的盛宴 题目链接:https://buaacoding.cn/problem/858/index 思路 本题乍一眼看过去,你可能会想到使用一个二维数组A[51][51]来记录从i到j的路线 ...

  2. 2016级算法第一次练习赛-F.AlvinZH的儿时梦想——机器人篇

    864 AlvinZH的儿时梦想----机器人篇 题目链接:https://buaacoding.cn/problem/868/index 思路 中等题. 判断无限玩耍: \(p\) 的值能够承担的起 ...

  3. 2016级算法第一次练习赛-E.AlvinZH的儿时回忆——蛙声一片

    864 AlvinZH的儿时回忆----蛙声一片 题目链接:https://buaacoding.cn/problem/865/index 思路 中等题.难点在于理解题意!仔细读题才能弄懂题目规则.整 ...

  4. 2016级算法第一次练习赛-D.AlvinZH的儿时回忆——跳房子

    864 AlvinZH的儿时回忆----跳房子 题目链接:https://buaacoding.cn/problem/864/index 思路 这是一道简单题,但是同样有人想复杂了,DP?大模拟?. ...

  5. 2016级算法第一次练习赛-B.朴素的中位数

    朴素的中位数 题目链接:https://buaacoding.cn/problem/846/index 分析 题意很简单,就是给定了两个从小到大排好序的数组,找出这两个数组合起来的数据中的中位数. 方 ...

  6. 算法 递归 迭代 动态规划 斐波那契数列 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  7. 算法导论-求(Fibonacci)斐波那契数列算法对比

    目录 1.斐波那契数列(Fibonacci)介绍 2.朴素递归算法(Naive recursive algorithm) 3.朴素递归平方算法(Naive recursive squaring) 4 ...

  8. 《BI那点儿事》Microsoft 时序算法——验证神奇的斐波那契数列

    斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10 ...

  9. 基于visual Studio2013解决算法导论之045斐波那契堆

     题目 斐波那契堆 解决代码及点评 // 斐波那契堆.cpp : 定义控制台应用程序的入口点. // #include<iostream> #include<cstdio> ...

随机推荐

  1. C++ split

    /*************************************************Function: splitDescription: 根据空格切分字符串Calls: // 被本函 ...

  2. MySQL 时间函数加减计算

    一.MySQL 获得当前日期时间 函数 1.1 获得当前日期 + 时间(date + time) 函数:now() mysql> select now();+———————+| now() |+ ...

  3. 浅谈Java中set.map.List的区别

    就学习经验,浅谈Java中的Set,List,Map的区别,对JAVA的集合的理解是想对于数组: 数组是大小固定的,并且同一个数组只能存放类型一样的数据(基本类型/引用类型),JAVA集合可以存储和操 ...

  4. Exception in thread "main" org.springframework.beans.factory.BeanCreationException

    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error ...

  5. STL中 set 和 multiset

    1. 所在头文件: <set>, 命名空间: std ; 声明如下: namespace std{ template <class T, class Compare = less&l ...

  6. QT学习之窗口部件

    对话框--QDialog 模态对话框与非模态对话框 模态对话框:就是相当于没关闭它之前,不能再和该应用程序的其他窗口进行交互(比如新建项目时弹出的对话框) 非模态对话框:可以与它交互,也可以与该程序中 ...

  7. word上传博客教程

    目前大部分的博客作者在写博客这件事情上都会遇到以下3个痛点:1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.2.发布到博客或公众号平台 ...

  8. ARM启动代码中_main 与用户主程序main()的区别

    1.1  问题描述     __main函数的作用是什么呀?1.2  问题剖析     __main函数是C/C++运行时库的一个函数,嵌入式系统在进入应用主程序之前必须有一个初始化的过程,使用__m ...

  9. 深入探讨 Java 类加载器(转载)

    类加载器(class loader)是 Java™中的一个很重要的概念.类加载器负责加载 Java 类的字节代码到 Java 虚拟机中.本文首先详细介绍了 Java 类加载器的基本概念,包括代理模式. ...

  10. delphi实现截全屏功能

    procedure TForm1.Button10Click(Sender: TObject);var bmp: TBitmap; can: TCanvas; dc: HDC; Image1: TIm ...