题解报告:poj 3070 Fibonacci
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的更多相关文章
- 矩阵快速幂 POJ 3070 Fibonacci
题目传送门 /* 矩阵快速幂:求第n项的Fibonacci数,转置矩阵都给出,套个模板就可以了.效率很高啊 */ #include <cstdio> #include <algori ...
- POJ 3070 Fibonacci(矩阵高速功率)
职务地址:POJ 3070 用这个题学会了用矩阵高速幂来高速求斐波那契数. 依据上个公式可知,第1行第2列和第2行第1列的数都是第n个斐波那契数.所以构造矩阵.求高速幂就可以. 代码例如以下: #in ...
- 矩阵经典题目六:poj 3070 Fibonacci
http://poj.org/problem?id=3070 按已构造好的矩阵,那么该矩阵的n次方的右上角的数便是f[n]. #include <stdio.h> #include < ...
- poj 3070 Fibonacci 矩阵快速幂
Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...
- POJ 3070 Fibonacci
Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...
- poj 3070 Fibonacci (矩阵快速幂乘/模板)
题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...
- POJ 3070 Fibonacci 【矩阵快速幂】
<题目链接> Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 ...
- poj 3070 Fibonacci 矩阵相乘
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7715 Accepted: 5474 Descrip ...
- POJ 3070 Fibonacci【斐波那契数列/矩阵快速幂】
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17171 Accepted: 11999 Descr ...
随机推荐
- Python面向对象之私有属性和方法
私有属性与私有方法 应用场景 在实际开发中,对象的某些属性或者方法 可能只希望在对象的内部被使用,而不希望在外部被访问到: 私有属性 就是对象不希望公开的属性: 私有方法 就是对象不希望公开的方法: ...
- codechef 营养题 第一弹
第一弾が始まる! 定期更新しない! 来源:http://wenku.baidu.com/link?url=XOJLwfgMsZp_9nhAK15591XFRgZl7f7_x7wtZ5_3T2peHh5 ...
- sublime3注册码
TwitterInc User License EA7E 1D77F72E 390CDD93 4DCBA022 FAF60790 61AA12C0 A37081C5 D0316412 4584D136 ...
- [luoguP1156] 垃圾陷阱(DP)
传送门 先按照时间排序 f[i][j] 表示 前i个物品高度为j时所剩余的最大能量 显然每个物品有堆和吃两种选择 状态转移看代码 代码 #include <cstdio> #include ...
- 最短网络 Agri-Net
题目背景 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要你的帮助. 题目描述 约翰已经给他的农场安排了一条高速的网络线路,他想把这条线路共享给其 ...
- R - Milking Time DP
Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that sh ...
- [bzoj2097][Usaco2010 Dec]Exercise 奶牛健美操_贪心_树形dp_二分
Exercise bzoj-2097 Usaco-2010 Dec 题目大意:题目链接 注释:略. 想法:题目描述生怕你不知道这题在考二分. 关键是怎么验证?我们想到贪心的删边. 这样的策略是显然正确 ...
- Ubuntu 16.04下没有“用户和组”功能的问题解决
在16.04以前的版本会自带“用户和组”的功能,但是在16.04发现系统只自带了“用户账户”的功能. 问题解决: 1.安装gnome-system-tools sudo apt-get install ...
- C/C++ Threads): Creating worker threads that will be listening to jobs and executing them concurrently when wanted
Suppose we have two workers. Each worker has an id of 0 and 1. Also suppose that we have jobs arrivi ...
- Flume NG源代码分析(二)支持执行时动态改动配置的配置模块
在上一篇中讲了Flume NG配置模块主要的接口的类,PropertiesConfigurationProvider提供了基于properties配置文件的静态配置的能力,这篇细说一下PollingP ...