poj_3070 Fibonacci(矩阵快速幂)
来源:互联网 发布:淘宝金牌卖家有假货吗 编辑:程序博客网 时间:2024/06/05 12:48
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 <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define FOP2 freopen("data1.txt","w",stdout)#define inf 0x3f3f3f3f#define maxn 5#define PI acos(-1.0)#define LL long longusing namespace std;typedef long long Matrix[maxn][maxn];int sz = 4, mod = 10000;Matrix C;void matrix_mul(Matrix A, Matrix B, Matrix res) { memset(C, 0, sizeof(C)); for(int i = 0; i < sz; i++) for(int j = 0; j < sz; j++) for(int k = 0; k < sz; k++) C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % mod; memcpy(res, C, sizeof(C));}Matrix a, r;void matrix_pow(Matrix A, int n, Matrix res) { memcpy(a, A, sizeof(a)); memset(r, 0, sizeof(r)); for(int i = 0; i < sz; i++) r[i][i] = 1; while(n) { if(n&1) matrix_mul(r, a, r); n >>= 1; matrix_mul(a, a, a); } memcpy(res, r, sizeof(r));}Matrix A, res;int n;int main(){ A[0][0] = A[0][1] = A[1][0] = 1; A[1][1] = 0; while(~scanf("%d", &n) && n != -1) { matrix_pow(A, n, res); printf("%d\n", res[0][1]); } return 0;}
- poj_3070 Fibonacci(矩阵快速幂)
- poj_3070 Fibonacci
- Fibonacci(矩阵快速幂)
- 【矩阵快速幂】Fibonacci
- Fibonacci 矩阵快速幂
- Fibonacci 矩阵快速幂
- Fibonacci (矩阵快速幂)
- Fibonacci 快速幂矩阵
- poj - 3070 - Fibonacci(矩阵快速幂)
- hdu3117 Fibonacci Numbers (矩阵快速幂)
- hdu1588 Gauss Fibonacci (矩阵快速幂)
- poj 3070 Fibonacci(矩阵快速幂)
- poj 3070 Fibonacci(矩阵快速幂)
- POJ 3070 Fibonacci(矩阵快速幂)
- 【POJ】3070Fibonacci(矩阵快速幂)
- POJ3070 - Fibonacci (矩阵快速幂)
- POJ 3070 Fibonacci(矩阵快速幂)
- poj3070 Fibonacci (矩阵快速幂)
- MySQL5.6安装,修改密码,创建一定权限用户
- tensorflow之路-如何处理原始文本数据
- 23种设计模式(2):工厂方法模式
- 【深度学习Deep Learning系列】word2vec和doc2vec
- faster-rcnn+ubuntu14.04+matlab2014a+cpu配置
- poj_3070 Fibonacci(矩阵快速幂)
- 关于NSURL NSURLComponents
- RERAN:安卓系统的定时和点击的录制和回放——(3)
- 查询某字段更新历史
- 解析XML文件
- Windows下搭建React Native环境与Android Studio集成
- Xcode各种iOS版本模拟器的安装使用
- 获取当前iframe的id
- BeautifulSoup解析页面造成信息丢失的解决办法