poj3070 Fibonacci(矩阵快速幂)
2016-08-15 16:07
375 查看
思路:矩阵快速幂的入门题
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct Mat
{
int a[2][2]; //矩阵大小
};
int n;
const int mod = 10000;
Mat mul(Mat a,Mat b)
{
Mat t;
memset(t.a,0,sizeof(t.a));
for(int i = 0;i<n;i++)
{
for(int k = 0;k<n;k++)
{
if(a.a[i][k])
for(int j = 0;j<n;j++)
{
t.a[i][j]+=a.a[i][k]*b.a[k][j];
if(t.a[i][j]>=mod)
t.a[i][j]%=mod;
}
}
}
return t;
}
Mat expo(Mat p,int k)
{
if(k==1)return p;
Mat e;
memset(e.a,0,sizeof(e.a));
for(int i = 0;i<n;i++) //初始化单位矩阵
e.a[i][i]=1;
if(k==0)return e;
while(k)
{
if(k&1)
e = mul(p,e);
p = mul(p,p);
k>>=1;
}
return e;
}
int main()
{
int k;
n=2;
while(scanf("%d",&k)!=EOF && k!=-1)
{
if(k==0)
printf("0\n");
else
{
Mat p;
p.a[0][0]=p.a[0][1]=p.a[1][0]=1;
p.a[1][1]=0;
Mat ans = expo(p,k);
/*for(int i = 0;i<n;i++,printf("\n"))
for(int j = 0;j<n;j++)
printf("%d ",ans.a[i][j]);*/
printf("%d\n",ans.a[0][1]);
}
}
}
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
![](http://7xjob4.com1.z0.glb.clouddn.com/23da167d8116d97497f11a1e8ecd74fa)
.
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., printFn mod 10000).
Sample Input
Sample Output
Hint
As a reminder, matrix multiplication is associative, and the product of two 2 × 2 matrices is given by
![](http://7xjob4.com1.z0.glb.clouddn.com/984c6270154c8fdd9f92e6bcdccc108b)
.
Also, note that raising any 2 × 2 matrix to the 0th power gives the identity matrix:
![](http://7xjob4.com1.z0.glb.clouddn.com/9edfa7d7905bf6c423a7108ebfb9adf6)
.
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct Mat
{
int a[2][2]; //矩阵大小
};
int n;
const int mod = 10000;
Mat mul(Mat a,Mat b)
{
Mat t;
memset(t.a,0,sizeof(t.a));
for(int i = 0;i<n;i++)
{
for(int k = 0;k<n;k++)
{
if(a.a[i][k])
for(int j = 0;j<n;j++)
{
t.a[i][j]+=a.a[i][k]*b.a[k][j];
if(t.a[i][j]>=mod)
t.a[i][j]%=mod;
}
}
}
return t;
}
Mat expo(Mat p,int k)
{
if(k==1)return p;
Mat e;
memset(e.a,0,sizeof(e.a));
for(int i = 0;i<n;i++) //初始化单位矩阵
e.a[i][i]=1;
if(k==0)return e;
while(k)
{
if(k&1)
e = mul(p,e);
p = mul(p,p);
k>>=1;
}
return e;
}
int main()
{
int k;
n=2;
while(scanf("%d",&k)!=EOF && k!=-1)
{
if(k==0)
printf("0\n");
else
{
Mat p;
p.a[0][0]=p.a[0][1]=p.a[1][0]=1;
p.a[1][1]=0;
Mat ans = expo(p,k);
/*for(int i = 0;i<n;i++,printf("\n"))
for(int j = 0;j<n;j++)
printf("%d ",ans.a[i][j]);*/
printf("%d\n",ans.a[0][1]);
}
}
}
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., printFn 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:
.
相关文章推荐
- scala 学习笔记--对象
- 剑指Offer_18_二叉树的镜像
- TDA线程分析
- virtualbox 使用物理硬盘
- JavaScript——表单验证
- CentOS 配置网络yum源
- 全栈工程师之路
- python学习笔记之基础操作
- svn学习
- 数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历
- POJ3267——The Cow Lexicon
- hdu5832 A water problem
- HDU1698 Just a Hook [lazy+成段更新]
- ip和long的转换
- XAMPP 更换其它路径
- 推荐系统资料
- Scrapy爬虫Demo
- Context全解析,让你的上下文明明白白
- 测试图片
- 省市区三级联动