Fibonacci

题目链接(点击)

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 20989   Accepted: 14381

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

.

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

.

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

.

思路:

由于输入的n数值特别大,所以如果先打表然后随机访问再输出肯定不行 就用到了矩阵快速幂

(补充)矩阵相乘:

矩阵AB的第i行第j列为 A矩阵第i行与B矩阵第j列对应元素分别相乘再求和

不用字母直接上例子:

已知:(n)=f(n-1)+f(n-2)    f(n-1)=f(n-1)+0*f(n-2)

可以构造下面的递推式:

可以化简为:

根据矩阵快速幂就可以 先求常数矩阵的(n-1)次幂 然后输出a[0][0] 即可:

a[0][0]表示的是 f(n) 但要注意考虑当n=0是无法求出 f(-1) 所以要特殊考虑n=0的情况

AC代码:

(通过结构体定义数组方便传参)    感谢@鸡冠花12138

#include<stdio.h>
#include<string.h>
const int mod=1e4;
struct node{
int a[5][5];
};
node mat_mul(node x,node y) //该函数返回值为node型 作用为计算两个矩阵x和y的乘积
{
node res;
memset(res.a,0,sizeof(res.a));
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
res.a[i][j]=0;
for(int k=0;k<2;k++){
res.a[i][j]=(res.a[i][j]+x.a[i][k]*y.a[k][j])%mod;
}
}
}
return res;
}
node mat_qpow(int n) //返回值仍然是node型 作用是求常数矩阵的n次幂
{
node res,c;
c.a[0][0]=1; c.a[0][1]=1; //c.a表示的是常数矩阵
c.a[1][0]=1; c.a[1][1]=0;
memset(res.a,0,sizeof(res.a));
for(int i=0;i<2;i++){
res.a[i][i]=1;
}
while(n){ //快速幂
if(n%2){
res=mat_mul(res,c);
}
c=mat_mul(c,c);
n/=2;
}
printf("%d\n",res.a[0][0]);
}
int main()
{
int n;
while(~scanf("%d",&n)&&n!=-1){
if(n==0){ //特殊考虑n=0的情况
printf("0\n");
}
else{
mat_qpow(n-1);
}
}
return 0;
}

Fibonacci(模板)【矩阵快速幂】的更多相关文章

  1. hdu 3117 Fibonacci Numbers 矩阵快速幂+公式

    斐波那契数列后四位可以用快速幂取模(模10000)算出.前四位要用公式推 HDU 3117 Fibonacci Numbers(矩阵快速幂+公式) f(n)=(((1+√5)/2)^n+((1-√5) ...

  2. hdu3306 Another kind of Fibonacci【矩阵快速幂】

    转载请注明出处:http://www.cnblogs.com/KirisameMarisa/p/4187670.html 题目链接:http://acm.hdu.edu.cn/showproblem. ...

  3. POJ 3070 Fibonacci 【矩阵快速幂】

    <题目链接> Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 ...

  4. ACM-ICPC 2018 焦作赛区网络预赛- L:Poor God Water(BM模板/矩阵快速幂)

    God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...

  5. Project Euler 435 Polynomials of Fibonacci numbers (矩阵快速幂)

    题目链接: https://projecteuler.net/problem=435 题意: The Fibonacci numbers $ {f_n, n ≥ 0}$ are defined rec ...

  6. poj 3070 Fibonacci(矩阵快速幂,简单)

    题目 还是一道基础的矩阵快速幂. 具体的居者的幂公式我就不明示了. #include<stdio.h> #include<string.h> #include<algor ...

  7. POJ 3070 Fibonacci(矩阵快速幂)

    题目链接 题意 : 用矩阵相乘求斐波那契数的后四位. 思路 :基本上纯矩阵快速幂. #include <iostream> #include <cstring> #includ ...

  8. 3990 [模板]矩阵快速幂 洛谷luogu

    题目背景 矩阵快速幂 题目描述 给定n*n的矩阵A,求A^k 输入输出格式 输入格式: 第一行,n,k 第2至n+1行,每行n个数,第i+1行第j个数表示矩阵第i行第j列的元素 输出格式: 输出A^k ...

  9. 2018.09.25 poj3070 Fibonacci(矩阵快速幂)

    传送门 矩阵快速幂板题,写一道来练练手. 这一次在poj做题总算没忘了改万能库. 代码: #include<iostream> #include<cstdio> #define ...

  10. poj3070 Fibonacci(矩阵快速幂)

    矩阵快速幂基本应用. 对于矩阵乘法与递推式之间的关系: 如:在斐波那契数列之中 f[i] = 1*f[i-1]+1*f[i-2]  f[i-1] = 1*f[i-1] + 0*f[i-2].即 所以, ...

随机推荐

  1. 解决删除~/Library/Caches/CocoaPods/search_index.json重新pod search还是不起作用

    今天新苹果机安装cocoapods,安装完以后发现怎么pod search 都没有用 命令行提示: swhcxp@iosdevmac ~ % pod search Almofire Setup com ...

  2. Python实现批量处理文件的缩进和转码问题

    最近把很久前的C代码传到Github上的时候,发现全部上百个源文件的代码缩进都变成了8格,而且里面的中文都出现了乱码,所以决定写个程序来批量处理所有文件的格式.这段关于转码的代码可以适用于很多场合,比 ...

  3. python字典详细介绍

    字典的用途   字典是Python提供的一种常用的数据结构,它用于存放具有映射关系的数据. 字典相当于保存了两组数据,其中一组数据是关键数据,被称为 key:另一组数据可通过 key 来访问,被称为 ...

  4. 面试官:你说你精通 Docker,那你来详细说说 Dockerfile 吧

    接上一篇:30分钟快速上手Docker,看这篇就对了! 一. 带着问题学Dockerfile 1.疑问 我们都知道从远程仓库可以pull一个tomcat等镜像下来,然后docker run启动容器,然 ...

  5. CentOS下搭建Git服务器

    1.首先需要安装Git,可以使用yum源在线安装: [root@localhost Desktop]# yum install -y git 2.创建一个git用户,用来运行git服务 # addus ...

  6. Verilog代码和FPGA硬件的映射关系(三)

    组合逻辑和FPGA之间的映射关系我们知道了,那时序逻辑和FPGA之间又是一种怎样的映射关系呢?我们就以前面寄存器章节的例子来向大家说明,也一同把当时为什么用异步复位更节约资源的原因告诉大家.我们先来看 ...

  7. 基本sql语法

    SQL 语句主要可以划分为以下 3 个类别. DDL(Data Definition Languages)语句:数据定义语言,这些语句定义了不同的数据段.数据库.表.列.索引等数据库对象的定义.常用 ...

  8. 替换Java WEB工程文件的指定字符串

    package com.utils; import java.io.BufferedReader;import java.io.File;import java.io.FileFilter;impor ...

  9. 小智的糖果(Candy) 51nod 提高组试题

    luogu AC通道! (官方数据) 题目描述 小智家里来了很多的朋友,总共有N个人,站成一排,分别编号为0到N-1,小智要给他们分糖果.但 是有的朋友有一些特殊的要求,有的人要求他左右的两个人(左边 ...

  10. @Spring Boot程序员,我们一起给程序开个后门吧:让你在保留现场,服务不重启的情况下,执行我们的调试代码

    前言 这篇其实是对一年前的一篇文章的补坑. @Java Web 程序员,我们一起给程序开个后门吧:让你在保留现场,服务不重启的情况下,执行我们的调试代码 当时,就是在spring mvc应用里定义一个 ...