3070 Fibonacci 矩阵乘法
来源:互联网 发布:the essence of sql 编辑:程序博客网 时间:2024/05/26 12:59
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: . Source099999999991000000000-1
0346266875
#include<stdio.h>
int n;
int a[4],b[4];
const int M=10000;
void cal(int n)
{
if(n==1||n==0)
{
a[3]=1;a[2]=1;a[1]=1;a[0]=0;
return ;
}
if(n&1)
{
cal(n/2);
b[3]=(a[3]*a[3]+a[2]*a[1])%M;
b[2]=(a[3]*a[2]+a[2]*a[0])%M;
b[1]=(a[1]*a[3]+a[0]*a[1])%M;
b[0]=(a[1]*a[2]+a[0]*a[0])%M;
for(int i=0;i<4;i++) a[i]=b[i];
b[3]=(a[3]+a[2])%M;
b[2]=a[3];
b[1]=(a[1]+a[0])%M;
b[0]=a[1];
for(int i=0;i<4;i++) a[i]=b[i];
}
else
{
cal(n/2);
b[3]=(a[3]*a[3]+a[2]*a[1])%M;
b[2]=(a[3]*a[2]+a[2]*a[0])%M;
b[1]=(a[1]*a[3]+a[0]*a[1])%M;
b[0]=(a[1]*a[2]+a[0]*a[0])%M;
for(int i=0;i<4;i++) a[i]=b[i];
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==-1) break;
if(n==0) {printf("0/n");continue;}
cal(n-1);
printf("%d/n",a[3]);
}
return 0;
}
- 3070 Fibonacci 矩阵乘法
- POJ 3070 Fibonacci 矩阵乘法
- poj 3070 Fibonacci (快速矩阵乘法)
- POJ 3070 Fibonacci 矩阵乘法 整数分解
- Fibonacci - POJ 3070 矩阵乘法快速幂
- [POJ 3070] Fibonacci · 矩阵乘法
- Fibonacci 矩阵乘法
- 【poj3070】 Fibonacci 【矩阵乘法】
- poj 3070 Fibonacci + 矩阵乘法(矩阵快速幂)
- hdu 3117 Fibonacci Numbers(矩阵乘法+fibonacci)
- 1250 Fibonacci数列(矩阵乘法)
- POJ 1070 Fibonacci 矩阵乘法
- POJ 3070 Fibonacci数列 矩阵乘法及乘幂求法
- poj 3070 Fibonacci(矩阵乘法快速幂)
- hdu 1588 Gauss Fibonacci(矩阵乘法,二分)
- nyoj fibonacci数列(二) 矩阵乘法
- 【矩阵乘法】Fibonacci数列 WikiOI 1732/1250
- hdu 2855 Fibonacci Check-up(矩阵乘法)
- 【其他】【RQNOJ】二叉树计数
- gxout图形类型设置
- CSS核心:框模型与视觉格式化模型[总览]
- 3233 Matrix Power Series 矩阵乘法
- 进程间通信 IPC interprocess communication
- 3070 Fibonacci 矩阵乘法
- CSS核心:再说框模型(Box Model)
- ANDROID Porting系列十、Audio
- 用QT实现Windows定时关机功能
- 网宿实习一周记
- 彻底解密C++宽字符:1、从char到wchar_t
- 彻底解密C++宽字符:2、Unicode和UTF
- grads右侧的色标图注画的命令
- 彻底解密C++宽字符:3、利用C运行时库函数转换