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. Linux indent命令

    一.简介 indent可辨识C的原始代码文件,并加以格式化,以方便程序设计师阅读. 二.选项 http://www.cnblogs.com/xuxm2007/archive/2011/11/03/22 ...

  2. 深入浅出 JMS(四) - ActiveMQ 消息存储

    深入浅出 JMS(四) - ActiveMQ 消息存储 一.消息的存储方式 ActiveMQ 支持 JMS 规范中的持久化消息与非持久化消息 持久化消息通常用于不管是否消费者在线,它们都会保证消息会被 ...

  3. 2.自己搭建的一个简易的ioc容器

    1.persondao类namespace MyselfIoC{    public class PersonDao    {        public override string ToStri ...

  4. 简单的socket编程

    1.socket 服务器搭建 实例化socket服务器,循环获取请求 package com.orange.util; import java.io.IOException; import java. ...

  5. 关于adbd进程的ROOT权限问题

    http://blog.csdn.net/a345017062/article/details/6254402 adbd源码位于system/core/adb/目录下,可执行文件位于/sbin/adb ...

  6. 2018.10.22 bzoj1009: [HNOI2008]GT考试(kmp+矩阵快速幂优化dp)

    传送门 f[i][j]f[i][j]f[i][j]表示从状态"匹配了前i位"转移到"匹配了前j位"的方案数. 这个东西单次是可以通过跳kmp的fail数组得到的 ...

  7. 2018.09.27 bzoj2118: 墨墨的等式(最短路+背包)

    传送门 好题啊. 首先找到最小的一个非零系数记做a1a_1a1​,然后如果WWW modmodmod a1=W′a_1=W'a1​=W′ modmodmod a1a_1a1​,且WWW是方程的一个可行 ...

  8. 2018.09.27 codeforces1045A. Last chance(线段树优化建图+最大流)

    传送门 看完题应该都知道是网络流了吧. 但是第二种武器直接建图会gg. 因此我们用线段树优化建图. 具体操作就是,对于这m个人先建一棵线段树,父亲向儿子连容量为inf的边,最后叶子结点向对应的人连容量 ...

  9. 2018.09.26 洛谷P2464 [SDOI2008]郁闷的小J(map+vector)

    传送门 本来出题人出出来想考数据结构的. 但是我们拥有map+vector/set这样优秀的STL,因此直接用map离散化,vector存下标在里面二分找答案就行了. 代码: #include< ...

  10. hdu-1121(差分法--数学问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1121 参考文章:https://blog.csdn.net/fengzhizi76506/articl ...