Fibonacci
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12732   Accepted: 9060

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:

.

Source

 
 //快速幂矩阵
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct Mat{
int mat[][];
};
const int Mod = ;
Mat operator *(Mat a, Mat b)
{
Mat c;
memset(c.mat,,sizeof(c.mat));
for(int i = ; i < ; i++)
{
for(int j = ; j < ; j++)
{
for(int k = ; k < ; k++)
{
c.mat[i][j] = (c.mat[i][j]+(a.mat[i][k]*b.mat[k][j])%Mod)%Mod;
}
}
}
return c;
}
Mat multi(int n)
{
Mat c;
memset(c.mat,,sizeof(c.mat));
c.mat[][] = c.mat[][] = ;
Mat a;
memset(a.mat,,sizeof(a.mat));
a.mat[][] = a.mat[][] = a.mat[][] = ;
while(n)
{
if(n&) c = c*a;
a = a*a;
n>>=;
}
return c;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
if(n==-) return ;
Mat ans = multi(n);
printf("%d\n",ans.mat[][]);
}
return ;
}

poj_3070Fibonacci(矩阵快速幂)的更多相关文章

  1. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

  2. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

  3. 51nod 1113 矩阵快速幂

    题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...

  4. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

  5. HDU5950(矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:f(n) = f(n-1) + 2*f(n-2) + n^4,f(1) = a , f(2 ...

  6. 51nod 1126 矩阵快速幂 水

    有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的值. Input 输 ...

  7. hdu2604(递推,矩阵快速幂)

    题目链接:hdu2604 这题重要的递推公式,找到公式就很easy了(这道题和hdu1757(题解)类似,只是这道题需要自己推公式) 可以直接找规律,推出递推公式,也有另一种找递推公式的方法:(PS: ...

  8. 矩阵乘法&矩阵快速幂&矩阵快速幂解决线性递推式

    矩阵乘法,顾名思义矩阵与矩阵相乘, 两矩阵可相乘的前提:第一个矩阵的行与第二个矩阵的列相等 相乘原则: a b     *     A B   =   a*A+b*C  a*c+b*D c d     ...

  9. hdu4965 Fast Matrix Calculation (矩阵快速幂 结合律

    http://acm.hdu.edu.cn/showproblem.php?pid=4965 2014 Multi-University Training Contest 9 1006 Fast Ma ...

随机推荐

  1. 掌握numpy(二)

    目录 掌握numpy(一) 掌握numpy(二) 掌握numpy(三) 掌握numpy(四) 数组的reshape 顾名思义,就是对数组的形状进行改变,比如行变成列,一行变多行等. in place ...

  2. JFinal极速开发框架使用笔记

    记录第一次使用JFinal,从简单的框架搭建到增删改查,从自带的方法到正常框架习惯的使用方式. JFinal官网:http://www.jfinal.com/ JFinal 是基于 Java 语言的极 ...

  3. DNS 域名系统的简介

    一.DNS域名系统简介 1.网络中为了区别各个主机,必须为每台主机分配一个唯一的地址, 这个地址即称为“IP 地址.但这些数字难以记忆, 所以采用“域名” 的方式来取代这些数字. 2.当某台主机要与其 ...

  4. CentOS 7.x上gitlab搭建教程(https可用,邮件可用)

    目录 知识要求 搭建感想 搭建过程 参考 知识要求: nginx基础知识 搭建感想 注:以下是我搭建gitlab时的思考,需要nginx的基础知识,Docker的基础知识才容易理解,与下面的搭建过程是 ...

  5. javamelody 使用

    javamelody 扩展API如何获取监控参数 https://github.com/javamelody/javamelody/wiki/ExternalAPI#json

  6. C语言学生管理系统(原版本)(自编)

    /*系统特色:(大牛勿笑) *颜色提示 *文字提示 *功能 */ #include <stdio.h> #include <stdlib.h> #include <mat ...

  7. vue-router源码学习(一)

    因为v3.01版本中的   /src代码使用TypeScript进行书写,我这里仅仅用作模块学习, 具体学习的还是 /dist/vue-router.js 代码. (一)基本使用方式 JS代码 // ...

  8. vue 自定义组件 v-model双向绑定、 父子组件同步通信

    父子组件通信,都是单项的,很多时候需要双向通信.方法如下: 1.父组件使用:msg.sync="aa"  子组件使用$emit('update:msg', 'msg改变后的值xxx ...

  9. vue2.0 资源文件assets和static的区别

    资源文件处理 在我们的项目结构里,有两个资源文件的路径,分别是:src/assets 和 static/.那这两个到底有什么区别呢? Webpacked 资源 为了回答这个问题,我们首先需要理解web ...

  10. H2Engine游戏服务器设计之属性管理器

    游戏服务器设计之属性管理器 游戏中角色拥有的属性值很多,运营多年的游戏,往往会有很多个成长线,每个属性都有可能被N个成长线模块增减数值.举例当角色戴上武器时候hp+100点,卸下武器时HP-100点, ...