POJ 3070 Fibonacci (矩阵快速幂)
2017-08-09 09:28
288 查看
传送门:点击打开链接
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
Sample Output
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最大为10^9,线性超时,所以要用到矩阵快速幂的知识。
矩阵快速幂的模板题。
关于矩阵快速幂,不了解的同学请看下这几篇博客。
传送门:点击打开链接 点击打开链接 点击打开链接
代码实现:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
struct node{
int matrix[3][3]; //储存矩阵
int col,row; //储存行和列
};
node get_mult(node a,node b) //矩阵相乘
{
int i,j,k;
node temp;
temp.row=a.row;
temp.col=b.col;
for(i=1;i<=temp.row;i++)
{
for(j=1;j<=temp.col;j++)
{
temp.matrix[i][j]=0; //初始化为0
for(k=1;k<=a.col;k++)
temp.matrix[i][j]=(temp.matrix[i][j]+a.matrix[i][k]*b.matrix[k][j])%10000;
}
}
return temp;
}
int main()
{
int n,i;
node a,b,c;
while(cin>>n)
{
if(n==-1)
break;
if(n==0)
{
cout<<"0"<<endl;
continue;
}
a.col=a.row=2;b.col=b.row=2;c.col=2;c.row=1;
a.matrix[1][1]=1;a.matrix[1][2]=1;a.matrix[2][1]=1;a.matrix[2][2]=0;
b.matrix[1][1]=1;b.matrix[1][2]=0;b.matrix[2][1]=0;b.matrix[2][2]=1;
c.matrix[1][1]=1;c.matrix[2][1]=0;
n--;
while(n) //快速幂具体实现
{
if(n%2)
b=get_mult(a,b);
a=get_mult(a,a);
n>>=1;
}
b=get_mult(b,c);
cout<<b.matrix[1][1]<<endl;
}
return 0;
}
Fibonacci
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15807 | Accepted: 11106 |
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最大为10^9,线性超时,所以要用到矩阵快速幂的知识。
矩阵快速幂的模板题。
关于矩阵快速幂,不了解的同学请看下这几篇博客。
传送门:点击打开链接 点击打开链接 点击打开链接
代码实现:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
struct node{
int matrix[3][3]; //储存矩阵
int col,row; //储存行和列
};
node get_mult(node a,node b) //矩阵相乘
{
int i,j,k;
node temp;
temp.row=a.row;
temp.col=b.col;
for(i=1;i<=temp.row;i++)
{
for(j=1;j<=temp.col;j++)
{
temp.matrix[i][j]=0; //初始化为0
for(k=1;k<=a.col;k++)
temp.matrix[i][j]=(temp.matrix[i][j]+a.matrix[i][k]*b.matrix[k][j])%10000;
}
}
return temp;
}
int main()
{
int n,i;
node a,b,c;
while(cin>>n)
{
if(n==-1)
break;
if(n==0)
{
cout<<"0"<<endl;
continue;
}
a.col=a.row=2;b.col=b.row=2;c.col=2;c.row=1;
a.matrix[1][1]=1;a.matrix[1][2]=1;a.matrix[2][1]=1;a.matrix[2][2]=0;
b.matrix[1][1]=1;b.matrix[1][2]=0;b.matrix[2][1]=0;b.matrix[2][2]=1;
c.matrix[1][1]=1;c.matrix[2][1]=0;
n--;
while(n) //快速幂具体实现
{
if(n%2)
b=get_mult(a,b);
a=get_mult(a,a);
n>>=1;
}
b=get_mult(b,c);
cout<<b.matrix[1][1]<<endl;
}
return 0;
}
相关文章推荐
- 矩阵快速幂——Fibonacci ( POJ 3070 )
- poj 3070 Fibonacci (矩阵快速幂)
- poj 3070 Fibonacci(矩阵快速幂)
- 【POJ 3070 Fibonacci】+ 矩阵快速幂
- POJ 3070 Fibonacci 矩阵快速求法
- POJ 3070 Fibonacci(矩阵快速幂)
- poj 3070 Fibonacci 矩阵快速幂
- POJ 3070 Fibonacci 矩阵快速幂
- [POJ](3070)Fibonacci ---矩阵快速幂与斐波那契
- POJ 3070 Fibonacci (矩阵快速幂)
- poj 3070 Fibonacci 矩阵快速幂
- Fibonacci - POJ 3070 矩阵乘法快速幂
- poj 3070 Fibonacci(矩阵快速幂)
- POJ 3070 Fibonacci【矩阵快速幂】【感受数论的神奇】
- POJ 3070 Fibonacci(矩阵快速幂)
- 数论 快速矩阵幂 POJ 3070 Fibonacci
- poj 3070 Fibonacci(矩阵快速幂)
- POJ 3070 Fibonacci(矩阵快速幂)
- POJ 3070 Fibonacci(矩阵快速幂)
- POJ 3070 Fibonacci(矩阵快速幂)