<题目链接>

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

 解题分析:

由于n很大,所以直接计算时不可行的,可以用矩阵快速幂来加速,并且,此题直接给出了矩阵的递推式,于是,我们只要按照题意构造矩阵即可。

#include <cstdio>
#include <cstring> const int mod=; struct Matrix{
int m[][];
Matrix(){}
Matrix(int x,int y,int z,int k){
m[][]=x;
m[][]=y;
m[][]=z;
m[][]=k;
}
}; Matrix Mul(Matrix a,Matrix b){
Matrix temp;
for(int i=;i<;i++){
for(int j=;j<;j++){
temp.m[i][j]=;
for(int k=;k<;k++){
temp.m[i][j]=(temp.m[i][j]+a.m[i][k]%mod*b.m[k][j]%mod)%mod;
}
}
}
return temp;
} Matrix pow_Mul(Matrix x,int c){
Matrix tmp(,,,);
while(c>){
if(c&){
tmp=Mul(tmp,x);
}
c>>=;
x=Mul(x,x);
}
return tmp;
} int main(){
int n;
while(scanf("%d",&n)!=EOF){
if(n==-)break;
int f[]={,,,};
if(n<=){
printf("%d\n",f[n]);
continue;
}
Matrix init(f[],f[],f[],f[]);
Matrix tmp(,,,);
Matrix ans=Mul(pow_Mul(tmp,n-),init);
printf("%d\n",ans.m[][]%mod);
}
return ;
}

2018-08-21

POJ 3070 Fibonacci 【矩阵快速幂】的更多相关文章

  1. poj 3070 Fibonacci (矩阵快速幂乘/模板)

    题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...

  2. poj 3070 Fibonacci 矩阵快速幂

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

  3. POJ 3070 Fibonacci 矩阵快速幂模板

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18607   Accepted: 12920 Descr ...

  4. POJ 3070 Fibonacci矩阵快速幂 --斐波那契

    题意: 求出斐波那契数列的第n项的后四位数字 思路:f[n]=f[n-1]+f[n-2]递推可得二阶行列式,求第n项则是这个矩阵的n次幂,所以有矩阵快速幂模板,二阶行列式相乘, sum[ i ] [ ...

  5. HDU 1588 Gauss Fibonacci(矩阵快速幂)

    Gauss Fibonacci Time Limit: 3000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) ...

  6. POJ——3070Fibonacci(矩阵快速幂)

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12329   Accepted: 8748 Descri ...

  7. UVA - 10229 Modular Fibonacci 矩阵快速幂

                                 Modular Fibonacci The Fibonacci numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 3 ...

  8. POJ 3744 【矩阵快速幂优化 概率DP】

    搞懂了什么是矩阵快速幂优化.... 这道题的重点不是DP. /* 题意: 小明要走某条路,按照个人兴致,向前走一步的概率是p,向前跳两步的概率是1-p,但是地上有地雷,给了地雷的x坐标,(一维),求小 ...

  9. poj3070 Fibonacci 矩阵快速幂

    学了线代之后 终于明白了矩阵的乘法.. 于是 第一道矩阵快速幂.. 实在是太水了... 这差不多是个模板了 #include <cstdlib> #include <cstring& ...

  10. poj 3735 稀疏矩阵矩阵快速幂

    设人数为 $n$,构造 $(n + 1) \times (n + 1)$ 的矩阵 得花生:将改行的最后一列元素 $+ 1$ \begin{gather}\begin{bmatrix}1 & 0 ...

随机推荐

  1. PHP二叉树

    <?php /******************************************************** * 我写的PHP都是从C语言的数据结构中演化而来********* ...

  2. C++ vector 使用笔记

    map 插入 vector #include <string> #include <iostream> #include <algorithm> #include ...

  3. python3中的比较函数

    在py2中,比较函数是cmp,而在py3,cmp已经不存在了,Py3启用了新的比较方法 原来在py2中,a>b就会调用a对象中的__cmp__函数,而现在a>b会调用a对象中的__lt__ ...

  4. DSO windowed optimization 代码 (1)

    这里不想解释怎么 marginalize,什么是 First-Estimates Jacobian (FEJ).这里只看看代码,看看Hessian矩阵是怎么构造出来的. 1 优化流程 整个优化过程,也 ...

  5. Linux MMC framework2:基本组件之host

    声明:本文很多内容和思路参考了http://www.wowotech.net/comm/mmc_host_driver.html,对原作者表示感谢! 1.前言 本文是Linux MMC framewo ...

  6. dubbo 一个接口多个实现

    <dubbo:service ref="dubboGroupService1" interface="com.yncp.dubbo.service.IDubboGr ...

  7. Spring boot教程mybatis访问MySQL的尝试

    Windows 10家庭中文版,Eclipse,Java 1.8,spring boot 2.1.0,mybatis-spring-boot-starter 1.3.2,com.github.page ...

  8. python之类中的super函数

    作用 实现代码重用 思考:super真的只是调用父类么? super函数是按照mro算法去调用的,不bb上代码: class A: def __init__(self): print('A') cla ...

  9. HTTP协议 (1)

    HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送协议. HTT ...

  10. PHP将数据写入指定文件中

    首先创建一个空的txt文件,这里我们创建了一个1.txt的空文件. 第一种方法:fwrite函数 <?php $file=fopen('1.txt','rb+'); var_dump(fwrit ...