2016级算法第一次练习赛-C.斐波那契进阶
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.斐波那契进阶的更多相关文章
- 2016级算法第一次练习赛-A.群鸦的盛宴
858 群鸦的盛宴 题目链接:https://buaacoding.cn/problem/858/index 思路 本题乍一眼看过去,你可能会想到使用一个二维数组A[51][51]来记录从i到j的路线 ...
- 2016级算法第一次练习赛-F.AlvinZH的儿时梦想——机器人篇
864 AlvinZH的儿时梦想----机器人篇 题目链接:https://buaacoding.cn/problem/868/index 思路 中等题. 判断无限玩耍: \(p\) 的值能够承担的起 ...
- 2016级算法第一次练习赛-E.AlvinZH的儿时回忆——蛙声一片
864 AlvinZH的儿时回忆----蛙声一片 题目链接:https://buaacoding.cn/problem/865/index 思路 中等题.难点在于理解题意!仔细读题才能弄懂题目规则.整 ...
- 2016级算法第一次练习赛-D.AlvinZH的儿时回忆——跳房子
864 AlvinZH的儿时回忆----跳房子 题目链接:https://buaacoding.cn/problem/864/index 思路 这是一道简单题,但是同样有人想复杂了,DP?大模拟?. ...
- 2016级算法第一次练习赛-B.朴素的中位数
朴素的中位数 题目链接:https://buaacoding.cn/problem/846/index 分析 题意很简单,就是给定了两个从小到大排好序的数组,找出这两个数组合起来的数据中的中位数. 方 ...
- 算法 递归 迭代 动态规划 斐波那契数列 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 算法导论-求(Fibonacci)斐波那契数列算法对比
目录 1.斐波那契数列(Fibonacci)介绍 2.朴素递归算法(Naive recursive algorithm) 3.朴素递归平方算法(Naive recursive squaring) 4 ...
- 《BI那点儿事》Microsoft 时序算法——验证神奇的斐波那契数列
斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10 ...
- 基于visual Studio2013解决算法导论之045斐波那契堆
题目 斐波那契堆 解决代码及点评 // 斐波那契堆.cpp : 定义控制台应用程序的入口点. // #include<iostream> #include<cstdio> ...
随机推荐
- 494. Target Sum 添加标点符号求和
[抄题]: You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have ...
- 257. Binary Tree Paths返回所有深度优先的遍历
[抄题]: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tr ...
- ansible介绍与安装
一.什么是ansible ansible是python中一套模块,系统中的一套自动化工具,可以用来作系统管理.自动化命令等任务. 二.ansible优势 .ansible是Python中一套完整的自动 ...
- spark reduceByKey
reduce(binary_function) reduce将RDD中元素前两个传给输入函数,产生一个新的return值,新产生的return值与RDD中下一个元素(第三个元素)组成两个元素,再被传给 ...
- 有一个5ml 的瓶子 和3ml 的瓶子 和 很多水 现在 要取出4ml的水 请写出编程 多种解法
//TODO public class demo { public static void main(String[] args) { demo.ss(); demo.sss(); } public ...
- ASP.NET’s compilation system
Compilation in ASP.NET applications First, let’s take a moment to revisit compilation in the context ...
- Git & Github使用总结
Linux下git的安装 在终端下输入 git , 看系统有没有安装git. 如果没有安装则会出现以下提醒: The program 'git' is currently not installed. ...
- Unable to locate JAR/zip in file system as specified by the driver definition: ojdbc14.jar
eclipse的配置错误,把当前包删除,重新导入一个包.然后设置与需要的数据库对应,就可以了
- 深入浅出Java多线程(2)-Swing中的EDT(事件分发线程) [转载]
本系列文章导航 深入浅出Java多线程(1)-方法 join 深入浅出Java多线程(2)-Swing中的EDT(事件分发线程) 深入浅出多线程(3)-Future异步模式以及在JDK1.5Concu ...
- MongoDB整理笔记のjava MongoDB分页优化
最近项目在做网站用户数据新访客统计,数据存储在MongoDB中,统计的数据其实也并不是很大,1000W上下,但是公司只配给我4G内存的电脑,让我程序跑起来气喘吁吁...很是疲惫不堪. 最常见的问题莫过 ...