Fibonacci(模板)【矩阵快速幂】
Fibonacci
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20989 Accepted: 14381 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
-1Sample Output
0
34
626
6875Hint
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数值特别大,所以如果先打表然后随机访问再输出肯定不行 就用到了矩阵快速幂
(补充)矩阵相乘:
矩阵AB的第i行第j列为 A矩阵第i行与B矩阵第j列对应元素分别相乘再求和
不用字母直接上例子:
已知:(n)=f(n-1)+f(n-2) f(n-1)=f(n-1)+0*f(n-2)
可以构造下面的递推式:
可以化简为:
根据矩阵快速幂就可以 先求常数矩阵的(n-1)次幂 然后输出a[0][0] 即可:
a[0][0]表示的是 f(n) 但要注意考虑当n=0是无法求出 f(-1) 所以要特殊考虑n=0的情况
AC代码:
(通过结构体定义数组方便传参) 感谢@鸡冠花12138
#include<stdio.h>
#include<string.h>
const int mod=1e4;
struct node{
int a[5][5];
};
node mat_mul(node x,node y) //该函数返回值为node型 作用为计算两个矩阵x和y的乘积
{
node res;
memset(res.a,0,sizeof(res.a));
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
res.a[i][j]=0;
for(int k=0;k<2;k++){
res.a[i][j]=(res.a[i][j]+x.a[i][k]*y.a[k][j])%mod;
}
}
}
return res;
}
node mat_qpow(int n) //返回值仍然是node型 作用是求常数矩阵的n次幂
{
node res,c;
c.a[0][0]=1; c.a[0][1]=1; //c.a表示的是常数矩阵
c.a[1][0]=1; c.a[1][1]=0;
memset(res.a,0,sizeof(res.a));
for(int i=0;i<2;i++){
res.a[i][i]=1;
}
while(n){ //快速幂
if(n%2){
res=mat_mul(res,c);
}
c=mat_mul(c,c);
n/=2;
}
printf("%d\n",res.a[0][0]);
}
int main()
{
int n;
while(~scanf("%d",&n)&&n!=-1){
if(n==0){ //特殊考虑n=0的情况
printf("0\n");
}
else{
mat_qpow(n-1);
}
}
return 0;
}
Fibonacci(模板)【矩阵快速幂】的更多相关文章
- hdu 3117 Fibonacci Numbers 矩阵快速幂+公式
斐波那契数列后四位可以用快速幂取模(模10000)算出.前四位要用公式推 HDU 3117 Fibonacci Numbers(矩阵快速幂+公式) f(n)=(((1+√5)/2)^n+((1-√5) ...
- hdu3306 Another kind of Fibonacci【矩阵快速幂】
转载请注明出处:http://www.cnblogs.com/KirisameMarisa/p/4187670.html 题目链接:http://acm.hdu.edu.cn/showproblem. ...
- POJ 3070 Fibonacci 【矩阵快速幂】
<题目链接> Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 ...
- ACM-ICPC 2018 焦作赛区网络预赛- L:Poor God Water(BM模板/矩阵快速幂)
God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...
- Project Euler 435 Polynomials of Fibonacci numbers (矩阵快速幂)
题目链接: https://projecteuler.net/problem=435 题意: The Fibonacci numbers $ {f_n, n ≥ 0}$ are defined rec ...
- poj 3070 Fibonacci(矩阵快速幂,简单)
题目 还是一道基础的矩阵快速幂. 具体的居者的幂公式我就不明示了. #include<stdio.h> #include<string.h> #include<algor ...
- POJ 3070 Fibonacci(矩阵快速幂)
题目链接 题意 : 用矩阵相乘求斐波那契数的后四位. 思路 :基本上纯矩阵快速幂. #include <iostream> #include <cstring> #includ ...
- 3990 [模板]矩阵快速幂 洛谷luogu
题目背景 矩阵快速幂 题目描述 给定n*n的矩阵A,求A^k 输入输出格式 输入格式: 第一行,n,k 第2至n+1行,每行n个数,第i+1行第j个数表示矩阵第i行第j列的元素 输出格式: 输出A^k ...
- 2018.09.25 poj3070 Fibonacci(矩阵快速幂)
传送门 矩阵快速幂板题,写一道来练练手. 这一次在poj做题总算没忘了改万能库. 代码: #include<iostream> #include<cstdio> #define ...
- poj3070 Fibonacci(矩阵快速幂)
矩阵快速幂基本应用. 对于矩阵乘法与递推式之间的关系: 如:在斐波那契数列之中 f[i] = 1*f[i-1]+1*f[i-2] f[i-1] = 1*f[i-1] + 0*f[i-2].即 所以, ...
随机推荐
- select 下拉模糊查询
http://ivaynberg.github.io/select2/ https://github.com/ https://github.com/ivaynberg.github.io/selec ...
- Java Web之路一:过滤器(Filter)
一.过滤器(Filter)简介 过滤器是对web资源进行拦截,做一些处理后再交给下一个过滤器或Servlet处理,主要可以拦截request和response 过滤器是以一种组件的形式与web程序绑定 ...
- CukeTest+Puppeteer的Web自动化测试(一)
CukeTest+Puppeteer的Web自动化测试 一.初识BDD.Cucumber(黄瓜).CukeTest 行为驱动开发(Behavior Driven Development,BDD).行为 ...
- 笔记二(JavaWeb)
上一个笔记写的好累,这次换Markdown试试 缺省适配器设计模式:父类不实现该方法,让子类去实现(抽象方法) 模板方法设计模式:定义一个操作中的方法骨架,而将一些步骤延迟到子类中.模板方法使得子类可 ...
- block和delegate的选择
block和delegate均为常用回调方式 (暂不讨论通知) 代理 优点: 设置某个对象的代理,代理对象可以与被代理对象不直接相关,即使两个对象距离较远,传值也比较方便. 代理方法内可以方便调用 ...
- 【JavaScript数据结构系列】01-数组Array
[JavaScript数据结构系列]01-数组Array 码路工人 CoderMonkey 转载请注明作者与出处 # [JavaScript数据结构系列] # 01-数组Array 数组: 是有序的元 ...
- python的map,reduce函数与pandas的apply,filter函数
1. python自带的apply.filter.map函数.reduce函数,很多情况下可以代替for循环: map(func,list),对list的每个元素分别执行func函数操作,显然func ...
- ASP.NET Core Blazor Webassembly 之 数据绑定
上一次我们学习了Blazor组件相关的知识(Asp.net Core Blazor Webassembly - 组件).这次继续学习Blazor的数据绑定相关的知识.当代前端框架都离不开数据绑定技术. ...
- PAT1065 单身狗 (25分) 思路记录——参考大神柳婼
1065 单身狗 (25分) “单身狗”是中文对于单身人士的一种爱称.本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱. 输入格式: 输入第一行给出一个正整数 N(≤ 50 000), ...
- docker重启提示已存在一个容器的问题处理
一.问题:在vmware虚拟机中测试以docker方式安装的prometheus,当重启虚拟机后,再次运行prometheus的执行文件,提示已有名称为prometheus的容器存在. 二.处理过程 ...