Fibonacci(矩阵)
Fibonacci
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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:
.
题意:求Fibonacci数列的第n项。
注意n很大,用矩阵,就可以了。
题目链接:http://poj.org/problem?id=3070 转载请注明出处:
寻找&星空の孩子
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define LL __int64
#define mod 10000
struct matrix
{
LL mat[][];
}; matrix multiply(matrix a,matrix b)
{
matrix c;
memset(c.mat,,sizeof(c.mat));
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
if(a.mat[i][j]==)continue;
for(int k=;k<;k++)
{
if(b.mat[j][k]==)continue;
c.mat[i][k]+=a.mat[i][j]*b.mat[j][k]%mod;
c.mat[i][k]%=mod;
}
}
}
return c;
} matrix quicklymod(matrix a,LL n)
{
matrix res;
memset(res.mat,,sizeof(res.mat));
for(int i=;i<;i++) res.mat[i][i]=;
while(n)
{
if(n&)
res=multiply(a,res);
a=multiply(a,a);
n>>=;
}
return res;
} int main()
{
LL n;
while(scanf("%I64d",&n)!=EOF)
{
if(n==-)break;
matrix ans;
ans.mat[][]=;
ans.mat[][]=;
ans.mat[][]=;
ans.mat[][]=; if(n==)
{
printf("0\n");
continue;
}
/* else if(n==1)
{
printf("1\n");
continue;
}*/ else
ans=quicklymod(ans,n); /* for(int i=0; i<2; i++)
{
for(int j=0; j<2; j++)
printf("%I64d\t",ans.mat[i][j]);
printf("\n");
}
printf("\n");
*/
printf("%I64d\n",ans.mat[][]);
}
return ;
}
矩阵入门题!
Fibonacci(矩阵)的更多相关文章
- hdu 1588(Fibonacci矩阵求和)
题目的大意就是求等差数列对应的Fibonacci数值的和,容易知道Fibonacci对应的矩阵为[1,1,1,0],因为题目中f[0]=0,f[1]=1,所以推出最后结果f[n]=(A^n-1).a, ...
- BZOJ3286 Fibonacci矩阵 矩阵 快速幂 卡常
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3286 题意概括 n,m,a,b,c,d,e,f<=10^1000000 题解 神奇的卡常题目 ...
- POJ3070 Fibonacci[矩阵乘法]
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13677 Accepted: 9697 Descri ...
- 【全国互虐】Fibonacci矩阵
orz啊又被屠了 人生如此艰难 题意: 给定一个k维的n^k的超立方体 超立方体的元素Ai1,i2,...,ik 的值为f(i1+i2+...+ik-k+1) 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 for n ≥ 2. F ...
- HDU 3306 Another kind of Fibonacci(矩阵+ll超时必须用int&输入必须取模&M必须是int类型)
Another kind of Fibonacci [题目链接]Another kind of Fibonacci [题目类型]矩阵+ll超时必须用int&输入必须取模&M必须是int ...
- POJ3070 Fibonacci[矩阵乘法]【学习笔记】
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13677 Accepted: 9697 Descri ...
- HDU 1588 Gauss Fibonacci(矩阵快速幂)
Gauss Fibonacci Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU1588-Gauss Fibonacci(矩阵高速幂+等比数列二分求和)
题目链接 题意:g(x) = k * x + b.f(x) 为Fibonacci数列.求f(g(x)),从x = 1到n的数字之和sum.并对m取模. 思路: 设A = |(1, 1),(1, 0) ...
随机推荐
- AJPFX讲解外汇保证金交易的货币符号和外汇的报价方式
AJPFX:外汇保证金交易的货币符号 认识货币名字是必须的入门基础,通过货币符号,首先要知道买卖哪个货币,下面是一些货币的符号.买卖外汇就是这些任意其中某两种货币的比值,也就是汇率.根据汇率比的升高或 ...
- Java几种单例模式的实现与利弊
饿汉式 提前new出来实例了,并不是在第一次调用get方法时才实例化,没有进行延迟加载 public class Singleton1 { private static Singleton1 inst ...
- 【文档】使用Sphinx + reST编写文档
0 前言 写文档是开发人员日常工作中的一项重要内容,除了word之外,我更偏爱使用标记语言(Markup Language).使用标记语言,可以利用简单.免费的文本编辑器(记事本,vim, emacs ...
- Eclipse 导入本地 Git 项目
File --> Open Projects From File System 选择项目路径 Finish
- 【并发】3、LockSupport阻塞与唤醒,相较与wait和notify
我们可以使用wait和notify分别对象线程进行阻塞或者唤醒,但是我们也可以使用LockSupport实现一样的功能,并且在实际使用的时候,个人感觉LockSupport会更加顺手 范例1,wait ...
- 02-04:springboot 访问静态资源
1.SpringBoot从classpath/static的目录下:(目录名称必须叫static,可以理解为根目录为static) 2.servletContext根目录下,进行查找: 在src/ma ...
- 浅谈Retrofit2+Rxjava2
近几年,Retrofit犹如燎原之火搬席卷了整个Android界.要是不懂Retrofit,简直不好意思出门...由于近几个项目都没用到Retrofit,无奈只能业余时间自己撸一下,写的不好的地方,还 ...
- Mac终端配置,DIY你的Terminal (iTerm 2 + Oh My Zsh)
使用mac osx一年以来,自带的Terminal终端一直都是白底黑字,食之无味,越来越缺乏新鲜感,怎么也得想个法子来刺激下眼球. 不然花那么多大洋买你回来是要哪般,难道真是为来学习工作??? 怎么可 ...
- JavaScript初探三
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- Shell脚本 | 性能测试之启动时间
安卓应用的性能测试,通常包括六个指标:启动时间.内存.CPU.耗电量.流量.流畅度. 除了耗电量,其他五个指标的数据在我们团队中已经可以通过运行脚本的方式获取到. 今天给大家分享下启动时间的脚本吧- ...