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:

.

/*
矩阵快速幂裸题
*/
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn = ;
ll sz,mod,n,f[maxn],a[maxn],ans[maxn];
struct mtx{
ll v[maxn][maxn];
void cler(){
memset(v,,sizeof(v));
}
mtx mul(mtx A,mtx B){
mtx C;
C.cler();
for(int i = ;i <= sz;i++){
for(int j = ;j <= sz;j++){
for(int k = ;k <= sz;k++){
C.v[i][j] = (C.v[i][j] + A.v[i][k] * B.v[k][j]) % mod;
}
}
}
return C;
}
mtx mi(mtx A,int n){
mtx R;
R.cler();
for(int i = ;i <= sz;i++) R.v[i][i] = ;
while(n){
if(n&) R = R.mul(R,A);
n >>= ;
A = A.mul(A,A);
}
return R;
}
void get_tr(mtx A){
memset(ans,,sizeof(ans));
for(int i = ;i <= sz;i++){
for(int j = ;j <= sz;j++){
ans[j] = (ans[j] + f[i] * A.v[i][j]) % mod;
}
}
}
};
int main(){
sz = ;
mod = ;
f[] = f[] = ;
a[] = a[] = ;
mtx A;
while(){
cin>>n;
if(n == -){
return ;
}
if(n <= ){
if(n==) cout<<<<endl;
if(n==||n==) cout<<<<endl;
continue;
}
A.cler();
for(int i = ;i <= sz;i++) A.v[i][] = a[i];
for(int i = ;i <= sz;i++) A.v[i-][i] = ;
A = A.mi(A,n-sz);
A.get_tr(A);
cout<<ans[]<<endl;
}
return ;
}

poj3070 Fibonacci的更多相关文章

  1. POJ3070 Fibonacci[矩阵乘法]

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13677   Accepted: 9697 Descri ...

  2. POJ3070 Fibonacci[矩阵乘法]【学习笔记】

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13677   Accepted: 9697 Descri ...

  3. 矩阵十题【六】 poj3070 Fibonacci

    题目链接:http://poj.org/problem? id=3070 题目大意:给定n和10000,求第n个Fibonacci数mod 10000 的值,n不超过2^31. 结果保留四位数字. 非 ...

  4. poj3070 Fibonacci 矩阵快速幂

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

  5. POJ3070 Fibonacci(矩阵快速幂加速递推)【模板题】

    题目链接:传送门 题目大意: 求斐波那契数列第n项F(n). (F(0) = 0, F(1) = 1, 0 ≤ n ≤ 109) 思路: 用矩阵乘法加速递推. 算法竞赛进阶指南的模板: #includ ...

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

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

  7. poj3070 Fibonacci(矩阵快速幂)

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

  8. POJ3070:Fibonacci(矩阵快速幂模板题)

    http://poj.org/problem?id=3070 #include <iostream> #include <string.h> #include <stdl ...

  9. 一些特殊的矩阵快速幂 hdu5950 hdu3369 hdu 3483

    思想启发来自, 罗博士的根据递推公式构造系数矩阵用于快速幂 对于矩阵乘法和矩阵快速幂就不多重复了,网上很多博客都有讲解.主要来学习一下系数矩阵的构造 一开始,最一般的矩阵快速幂,要斐波那契数列Fn=F ...

随机推荐

  1. joomla \libraries\joomla\session\session.php 反序列化截断畸形字符串导致对象注入漏洞

    catalog . 漏洞描述 . PHP SESSION持久化 . PHP 序列化/反序列化内核实现 . 漏洞代码分析 . POC构造技巧 . 防御方案 . Code Pathc方案 1. 漏洞描述 ...

  2. nginx添加proxy_cache模块做缓存服务器

    业务需求nginx对后端tomcat(静态文件)做缓存 减轻后端服务器的压力 # nginx-1.6.2.tar.gz  ngx_cache_purge-2.3.tar.gz #编译安装 ./conf ...

  3. 关于 android 开发中 debug不能顺利进行的各种问题的总结

    最后一条应该写反了,如果combined hover  取消勾选的话   那么 在写代码时候的悬浮提示就没有了

  4. tmux/screen里面如何用鼠标滚轮来卷动窗口内容

    tmux里面用鼠标滚轮来卷动窗口内容 在 tmux里面,因为每个窗口(tmux window)的历史内容已经被tmux接管了,所以原来console/terminal提供的Shift+PgUp/PgD ...

  5. linux 基础 shell脚本命令

    #########shell脚本命令#### 1.diff diff file file1 ####比较两个文件的不同 -c ####显示周围的行 -u ####按照一格式统一输出生成补丁 -r ## ...

  6. POJ2185Milking Grid(最小覆盖子串 + 二维KMP)

    题意: 一个r*c的矩形,求一个子矩形通过平移复制能覆盖整个矩形 关于一个字符串的最小覆盖子串可以看这里http://blog.csdn.net/fjsd155/article/details/686 ...

  7. angularjs中$watch监听model(对象属性、对象)变化

    昨天看了一下教学视频,学到了有关$watch的用法,想到最近做的一个页面中有个select下拉选项(select中的值变化了,则后面input中的值也跟着相应的变化),不知是否可以使用$watch来代 ...

  8. 查询centos查看系统内核版本,系统版本,32位还是64位

    [root@centos01 ~]# lsb_release -a           #查看centos 版本为6.4LSB Version: :base-4.0-amd64:base-4.0-no ...

  9. re正则表达式5_*

    *表示匹配[0,正无穷大]次 * means math zero or more-----occur any number of times in the text. # -*- coding: ut ...

  10. 使用ThreadLocal、Apache的dbutils的QueryRunner和dbcp2数据库连接池的BasicDataSource封装操作数据库工具

    package hjp.smart4j.framework.helper; import hjp.smart4j.framework.util.CollectionUtil; import hjp.s ...