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. OrCAD Capture使用记录

    1.安装 ①修改host_ID为主机名称(如果填不对,安装过程会提示你再填一次):其后跟的是机子的MAC地址,用ipconfig /all 可以查看. ②下一行的FLEMlm,后面跟的是文件cdslm ...

  2. Spring BeanUtils的用法

    package test; import java.util.Date; import org.springframework.beans.BeanUtils; import test.basic.B ...

  3. python中set使用

    In [2]: a = set() # 常用操作1 In [3]: a Out[3]: set() In [4]: type(a) Out[4]: set In [5]: b = set([1, 3] ...

  4. MyBatis详解 与配置MyBatis+Spring+MySql

    MyBatis 是一个可以自定义SQL.存储过程和高级映射的持久层框架.MyBatis 摒除了大部分的JDBC代码.手工设置参数和结果集重获.MyBatis 只使用简单的XML 和注解来配置和映射基本 ...

  5. vs 2012 + OPenCV 2.4.8 配置

    一:安装Opencv 下载opencv-2.4.8.exe,然后安装在目录D:\opencv-2.4.8. 二:配置vs 2012 1.打开vs创建项目 2.下一步,选择dll ,空项目,然后完成.后 ...

  6. BZOJ3224 普通平衡树

    传送门: Treap 版本: //OJ 1999 //by Cydiater //2016.8.30 #include <iostream> #include <cstdio> ...

  7. TreeMap按照key排序

    package test.tool.gui.common; import java.util.Comparator; import java.util.TreeMap; public class Te ...

  8. c# 操作datatable

    1.创建 datatable DataTable dt=new Datable();// 可以给表创建一个名字,tb 2.给表加个列名: dt.Columns.Add("id", ...

  9. win7搭建web服务器

    首先找到安装的tomcat软件,打开里面的webapp文件夹,在里面新建一个文件夹用作web应用程序访问端地址,然后再到新建的文件夹test里放入想要被访问的东西,这里我用的是一个测试的页面test. ...

  10. easyUI中tree的简单使用

    一.在JS中的代码 $('#tt').tree({ url: baseCtx + 'lib/easyui-1.4/demo/tree/tree_data1.json',//tree数据的来源,json ...