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:

.

解题思路:题目已经帮我们构造出矩阵行列式的关系了,这里就讲讲思路吧。通过给出的递推式我们可以推导得到题目那个n次幂的矩阵的通项公式,记那个矩阵为A,所以问题转化成求An,很自然就想到了矩阵快速幂,没错,无论n有多大,采用矩阵快速幂的做法就是一件很简单的事情。不过如果n值超过int最大值,要改成long long类型,避免数据溢出,其余代码不变。最后取结果矩阵右上角的值即为Fn对应的结果。

AC代码:

 #include<iostream>
#include<string.h>
using namespace std;
const int mod=;
const int maxn=;//2行2列式
struct Matrix{int m[maxn][maxn];}init;int n;
Matrix mul(Matrix a,Matrix b){//矩阵相乘
Matrix c;
for(int i=;i<maxn;i++){//第一个矩阵的行
for(int j=;j<maxn;j++){//第二个矩阵的列
c.m[i][j]=;
for(int k=;k<maxn;k++)
c.m[i][j]=(c.m[i][j]+a.m[i][k]*b.m[k][j])%mod;
}
}
return c;
}
Matrix POW(Matrix a,int x){
Matrix b;memset(b.m,,sizeof(b.m));
for(int i=;i<maxn;++i)b.m[i][i]=;//单位矩阵
while(x){
if(x&)b=mul(b,a);
a=mul(a,a);
x>>=;
}
return b;
}
int main(){
while(cin>>n&&(n!=-)){
init.m[][]=;init.m[][]=;init.m[][]=;init.m[][]=;//初始化矩阵
Matrix res = POW(init,n);//矩阵快速幂
cout<<res.m[][]<<endl;//取右上角的值即可
}
return ;
}

题解报告:poj 3070 Fibonacci的更多相关文章

  1. 矩阵快速幂 POJ 3070 Fibonacci

    题目传送门 /* 矩阵快速幂:求第n项的Fibonacci数,转置矩阵都给出,套个模板就可以了.效率很高啊 */ #include <cstdio> #include <algori ...

  2. POJ 3070 Fibonacci(矩阵高速功率)

    职务地址:POJ 3070 用这个题学会了用矩阵高速幂来高速求斐波那契数. 依据上个公式可知,第1行第2列和第2行第1列的数都是第n个斐波那契数.所以构造矩阵.求高速幂就可以. 代码例如以下: #in ...

  3. 矩阵经典题目六:poj 3070 Fibonacci

    http://poj.org/problem?id=3070 按已构造好的矩阵,那么该矩阵的n次方的右上角的数便是f[n]. #include <stdio.h> #include < ...

  4. poj 3070 Fibonacci 矩阵快速幂

    Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...

  5. POJ 3070 Fibonacci

    Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...

  6. poj 3070 Fibonacci (矩阵快速幂乘/模板)

    题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...

  7. POJ 3070 Fibonacci 【矩阵快速幂】

    <题目链接> Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 ...

  8. poj 3070 Fibonacci 矩阵相乘

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7715   Accepted: 5474 Descrip ...

  9. POJ 3070 Fibonacci【斐波那契数列/矩阵快速幂】

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17171   Accepted: 11999 Descr ...

随机推荐

  1. 在线安全清空慢查询日志slowlog

      mysql> show variables like '%slow_query%';+------------------------------------+--------------- ...

  2. 第十二节:Web爬虫之MongoDB数据库安装与数据存储

    MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功 ...

  3. 使用Mybatis的逆向工程自动生成代码

    1.逆向工程的作用 Mybatis 官方提供了逆向工程,可以针对数据库表自动生成Mybatis执行所需要的代码(包括mapper.xml.Mapper.java.pojo). 2.逆向工程的使用方法 ...

  4. [luoguP1156] 垃圾陷阱(DP)

    传送门 先按照时间排序 f[i][j] 表示 前i个物品高度为j时所剩余的最大能量 显然每个物品有堆和吃两种选择 状态转移看代码 代码 #include <cstdio> #include ...

  5. hdu 2089 记忆化搜索写法(数位dp)

    /* 记忆化搜索,第二维判断是否是6 */ #include<stdio.h> #include<string.h> #define N 9 int dp[N][2],digi ...

  6. kendo grid 点击新增没有反映

    在datasource中缺少 editable: "inline",这一行

  7. [bzoj1597][USACO2008]土地购买(DP斜率优化/四边形优化)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1597 分析: 1.先可以把被包含的土地可以去掉,这些土地的长宽肯定都是不会用的,具体先 ...

  8. - > 动规讲解基础讲解八——正整数分组

    将一堆正整数分为2组,要求2组的和相差最小.例如:1 2 3 4 5,将1 2 4分为1组,3 5分为1组,两组和相差1,是所有方案中相差最少的. 整数个数n<=100,所有整数的和<=1 ...

  9. Ubuntu 16.04安装FTP客户端filezilla

    1.安装: sudo apt-get install filezilla 参考: http://os.51cto.com/art/201103/247564.htm

  10. Hive之内置函数

    函数分类 UDF(User Defined Function):数据一对一 UDAF(User Defined Aggreation Function):数据多对一 UDTF(User Defined ...