POJ——3070快速幂
来源:互联网 发布:淘宝直播需要什么设备 编辑:程序博客网 时间:2024/06/03 23:47
Description
In the Fibonacci integer sequence, F0 = 0, F1 = 1, andFn = Fn − 1 + Fn − 2 forn ≥ 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 ofFn are all zeros, print ‘0’; otherwise, omit any leading zeros (i.e., printFn mod 10000).
Sample Input
099999999991000000000-1
Sample Output
0346266875
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:
.
基本的快速幂的方法:代码易懂
#include <iostream> #include <stdio.h> using namespace std; struct Mat{ int num[3][3]; }; Mat mult(Mat a,Mat b){ Mat c; for(int i=1;i<=2;i++) for(int j=1;j<=2;j++){ c.num[i][j]=0; for(int k=1;k<=2;k++) c.num[i][j]=(c.num[i][j]+a.num[i][k]*b.num[k][j])%10000; } return c; } Mat pow(Mat x,int y){ Mat ans; for(int i=1;i<=2;i++) for(int j=1;j<=2;j++){ if(i==j) ans.num[i][j]=1; else ans.num[i][j]=0; } while(y){ if(y&1) ans=mult(ans,x); y>>=1; x=mult(x,x); } return ans; } int main(){ int n; while(scanf("%d",&n)){ if(n==-1) break; Mat A; A.num[1][1]=1; A.num[1][2]=1; A.num[2][1]=1; A.num[2][2]=0; A=pow(A,n); printf("%d\n",A.num[1][2]); } }
- POJ——3070快速幂
- POJ—3070—Fibonacci—【矩阵快速幂】
- POJ——3070Fibonacci(矩阵快速幂)
- POJ 3070 —— Fibonacci 【矩阵快速幂】
- 矩阵快速幂——Fibonacci ( POJ 3070 )
- POJ 3233 —— 矩阵快速幂
- POJ 3070 矩阵快速幂
- poj 3070 矩阵快速幂
- poj 3070(矩阵快速幂)
- poj 3070 矩阵快速幂
- 矩阵快速幂 poj 3070
- POJ 3070 矩阵快速幂
- POJ 3070 矩阵快速幂
- POJ 3070 矩阵快速幂
- poj 3070 矩阵快速幂
- POJ 3070 矩阵快速幂
- poj 3070 矩阵快速幂
- 矩阵快速幂POJ-3070
- ios 获取视频的总时长
- 缓存一致性(Cache Coherency)入门
- UVALive - 2093 Moving Pegs bfs
- Android学习 - 截断短信问题
- opencv最大类间方差法(otsu)实现(三种算法比较)
- POJ——3070快速幂
- 文艺
- (int &)a强制类型转换
- Maven项目标准目录结构
- Activity跳转总结
- 学习JAVA第三天
- android drawable ScaleDrawable
- 如何给你的android安装文件瘦身
- mysql一些问题记录