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. 689. Maximum Sum of 3 Non-Overlapping Subarrays三个不重合数组的求和最大值

    [抄题]: In a given array nums of positive integers, find three non-overlapping subarrays with maximum ...

  2. Webdings和Wingdings字符码对应表

    刚才研究动网论坛代码,发现一个页面提示标记 i 感觉很神奇,看了半天才明白原来是一种叫“Webdings”的字体,其实很简单,只需要<font face='webdings' size=&quo ...

  3. redis高可用 - 主从复制

    工作需要,调研了一下redis的复制实现.在2.8版本之前和之后,复制方式有所不同.2.8之前的复制方式对于初次复制数据没有问题,对于断连接重新复制比较耗性能,因为都是全量复制.2.8之后对断线重连做 ...

  4. Fix: The account is not authorized to log in from this station

    If you have more the one computers running Windows, then its possible to connect them using HomeGrou ...

  5. Linux hostname主机名配置文件与文件 /etc/hosts解析(copy来的,原作者看到了别打我)

    1.关于/etc/host,主机名和IP配置文件 Hosts - The static table lookup for host name(主机名查询静态表) hosts文件是Linux系统中一个负 ...

  6. json操作工具-LitJson

    LitJSON是json生成与读取的操作工具,使用很方便并且网上还能找到源码.下面是使用LitJSON的例子: 一.生成json:实例化一个JsonData,然后按照List数组的方式向里面填. Js ...

  7. C语言访问mysql数据库

    mysql中新建的数据库为hyx,hyx中的表为my_schema,表中的数据为下图: 编写代码,访问表中的数据,测试代码如下: #include "stdafx.h" #incl ...

  8. redis 缓存用户账单策略

    最近项目要求分页展示用户账单列表,为提高响应使用redis做缓存,用到的缓存策略和大家分享一下. 需求描述:展示用户账单基本信息以时间倒序排序,筛选条件账单类型(所有,订单收入.提现.充值...). ...

  9. Arch Linux 使用markdown

    Arch Linux 使用markdown pandoc 文档格式转换 pygments 代码高亮 markdown-mode.el 配置emacs pandoc 号称文件格式转换的瑞士军刀,这里主要 ...

  10. delphi Ini读写

    try ini := TIniFile.Create(GetCurrentDir+'\BackServiceSetting.ini'); {ini 对象建立需要文件路径参数, 如果缺少路径会默认Win ...