Fibonacci
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13172   Accepted: 9368

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

思路:没得说,矩阵快速幂

代码如下:

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int mod = ;
const int N = ;//矩阵的维数,角标从0开始
struct Matrix
{
__int64 v[N][N];
Matrix()
{
memset(v,,sizeof(v));
}
};
//矩阵的乘法p1*p2
Matrix multi(Matrix p1,Matrix p2)
{
Matrix res;
for(int i=;i<N;i++)
for(int j=;j<N;j++)
if(p1.v[i][j])//代码优化,是0的话就不用计算
for(int k=;k<N;k++)
res.v[i][k]=(res.v[i][k]+(p1.v[i][j]*p2.v[j][k]))%mod;
return res;
}
//矩阵的快速幂p^k
Matrix pow(Matrix p,__int64 k)
{
Matrix t;
for(int i=;i<N;i++)//初始化为单位矩阵
t.v[i][i]=;
while(k)
{
if(k&)
t=multi(t,p);
p=multi(p,p);
k=k>>;
}
return t;
} int main()
{
__int64 n;
Matrix e,ans;
e.v[][]=e.v[][]=e.v[][]=;
e.v[][]=;
while(scanf("%I64dd",&n)!=EOF&&n!=-)
{
ans = pow(e,n);
printf("%I64d\n",ans.v[][]);
}
return ;
}

poj3070矩阵快速幂求斐波那契数列的更多相关文章

  1. 51 Nod 1242 矩阵快速幂求斐波那契数列

    #include<bits/stdc++.h> #define mod 1000000009 using namespace std; typedef long long ll; type ...

  2. codeforce 227E 矩阵快速幂求斐波那契+N个连续数求最大公约数+斐波那契数列的性质

    E. Anniversary time limit per test2 seconds memory limit per test256 megabytes inputstandard input o ...

  3. 矩阵快速幂--51nod-1242斐波那契数列的第N项

    斐波那契额数列的第N项 斐波那契数列的定义如下: F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) (n >= 2) (1, 1, 2, 3, 5, 8, ...

  4. UVA - 10689 Yet another Number Sequence (矩阵快速幂求斐波那契)

    题意:已知f(0) = a,f(1) = b,f(n) = f(n − 1) + f(n − 2), n > 1,求f(n)的后m位数. 分析:n最大为109,矩阵快速幂求解,复杂度log2(1 ...

  5. 矩阵快速幂 求斐波那契第N项

    #include<cstdio> #include<algorithm> #include<cstring> #include<iostream> us ...

  6. python 快速幂求斐波那契数列

    先占坑 后面再写详细的 import numpy as np def pow(n): a = np.array([[1,0],[0,1]]) b = np.array([[1,1],[1,0]]) n ...

  7. codeforces gym #101161G - Binary Strings(矩阵快速幂,前缀斐波那契)

    题目链接: http://codeforces.com/gym/101161/attachments 题意: $T$组数据 每组数据包含$L,R,K$ 计算$\sum_{k|n}^{}F(n)$ 定义 ...

  8. POJ 3070 - 快速矩阵幂求斐波纳契数列

    这题并不复杂. 设$A=\begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}$ 由题中公式: $\begin{pmatrix}f(n+1) & ...

  9. 【poj3070】矩阵乘法求斐波那契数列

    [题目描述] 我们知道斐波那契数列0 1 1 2 3 5 8 13…… 数列中的第i位为第i-1位和第i-2位的和(规定第0位为0,第一位为1). 求斐波那契数列中的第n位mod 10000的值. [ ...

随机推荐

  1. OpenGLES.APPLE_texture_format_BGRA8888

    OpenGL ES的扩展: APPLE_texture_format_BGRA8888 http://www.khronos.org/registry/gles/extensions/APPLE/AP ...

  2. powerdesigner 生成表备注

    第一步: 操作:Database-->Edit Current DBMS,进入下图页面, 然后分别将 Script-->Objects-->Table-->TableComme ...

  3. static 成员函数

    和静态数据成员一样,静态成员函数是所有对象共享的,不是单独属于某一个对象,由于静态成员函数没有传递this指针,故static member function 只能访问static成员,不能访问非st ...

  4. bootstrap中让图片自适应不同的分辨率的方法

    boostrap中加上这个样式class="img-responsive"图片就可以自适应,手机端同样适用 详细介绍请查看全文:https://cnblogs.com/qianzf ...

  5. 201.09.22 除虫药水(线性dp)

    描述 在十年前,除虫是十分艰苦的工作.那时,使用普通药水进行除虫的效果极差,在一片苹果 林中使用后除掉的虫仅为极小一部分. 比如说,Bugs 镇共有 N 片苹果林,对第i 片使用普通药水可以除掉 ai ...

  6. 第七章 介词(La préposition )

    法语中的介词是将两个同类或非同类的词连接在一起,把一个句中成分同另一个句子成分联系起来,并表明两者之间关系的词类.介词是一种无词性变化的词类. ★介词的形式 ()简单形式的介词,如: ()复合形式的介 ...

  7. 解决sea_born和matplotlib画图中文显示的问题

    #以下解决mtpl中文显示问题 from pylab import * mpl.rcParams['font.sans-serif'] = ['SimHei'] #以下解决seaborn中文编码报错问 ...

  8. 201709011工作日记--Volley源码详解(三)

    1. RequestQueue类 我们使用 Volley 的时候创建一个 request 然后把它丢到 RequestQueue 中就可以了.那么来看 RequestQueue 的构造方法,含有四个参 ...

  9. hdu1002 A + B Problem II(高精度加法) 2016-05-19 12:00 106人阅读 评论(0) 收藏

    A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  10. Hdu1969 Pie 2017-01-17 13:12 33人阅读 评论(0) 收藏

    Pie Time Limit : 5000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submissio ...