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. nginx4win10 文件下载服务器

    默认root是Nginx下目录html. 我们在其目录下新建download目录,然后在该目录下copy几个供下载的文件. 在浏览器输入http://localhost:9001/download/y ...

  2. JavaEE最新技术整理--新技术

    JavaEE最新技术整理-----https://blog.csdn.net/qq_21683643/article/details/79747922

  3. Leetcode 90.子集

    子集 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1], [1,2,2], ...

  4. noip模拟赛 毁灭

    题目描述 YJC决定对入侵C国的W国军队发动毁灭性打击.将C国看成一个平面直角坐标系,W国一共有n^2个人进入了C国境内,在每一个(x,y)(1≤x,y≤n)上都有恰好一个W国人.YJC决定使用m颗核 ...

  5. 使用ajax传值,后台乱码

    spring mvc,使用ajax传值,后台发现是乱码 解决方法:  后台的方法里加入  request.setCharacterEncoding("UTF-8"); 就行了 我前 ...

  6. 在Myeclipse中拷贝一个web项目,但是tomcat文件夹中没有更新,需要进行修改才能更新。

    1.在Myeclipse中拷贝一个web项目,但是tocat文件夹中没有更新,需要进行修改才能更新. 2.方法:右键这个工程,然后Properties->MyEclipse->Projec ...

  7. 什么是Spring Boot简介

    1.什么是spring boot 简单的说,spring boot就是整合了很多优秀的框架,不用我们自己手动的去写一堆xml配置然后进行配置. 从本质上来说,Spring Boot就是Spring,它 ...

  8. [bzoj1563][NOI2009]诗人小G(决策单调性优化)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1563 分析: 首先可得朴素的方程:f[i]=min{f[j]+|s[j]-j-s[i] ...

  9. java实现WebService 以及客户端不同的调用方式

    java 实现WebService 以及不同的调用方式 webservice:    就是应用程序之间跨语言的调用    wwww.webxml.com.cn    1.xml    2.    ws ...

  10. Maticsoft Code Generator

    源码:https://github.com/easonjim/MaticsoftCodeGenerator bug提交:https://github.com/easonjim/MaticsoftCod ...