H - Fibonacci POJ - 3070 (矩阵快速幂)
H - Fibonacci POJ - 3070 (矩阵快速幂)
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
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:
利用矩阵快速幂求 fibonacci 数列第n项
题解
通过设未知数计算求解常数矩阵
利用矩阵快速幂简化计算
复杂度
O(logn) == 30
数据范围
n = 1e9
每次%10000,数据不大
算法
矩阵快速幂(二分)
代码
#include<bits\stdc++.h>
using namespace std;
#define mod 10000typedef struct node {int e[3][3];
}Matrix;Matrix mp, temp, ans;
int n;void Mut(Matrix &a, Matrix &b, Matrix &c) { // 传递指针 memset(temp.e, 0, sizeof(temp.e));for(int i = 0; i <= 1; i++) {for(int j = 0; j <= 1; j++) {for(int k = 0; k <= 1; k++) {temp.e[i][j] += a.e[i][k] * b.e[k][j];temp.e[i][j] %= mod;}}}for(int i = 0; i <= 1; i++) {for(int j = 0; j <= 1; j++) {c.e[i][j] = temp.e[i][j];}}
}void quick_mod(int power) {while(power) {if(power&1) {Mut(ans, mp, ans); // 将ans*mp存到ans里 }power >>= 1;Mut(mp, mp, mp); // a=mp b=mp c=a*b }
}int main() {freopen("test.in", "r", stdin);while(cin >> n && n != -1) {if(n == 0) {printf("0\n");continue;}mp.e[0][0] = 0;mp.e[0][1] = mp.e[1][0] = mp.e[1][1] = 1;ans.e[0][0] = ans.e[0][1] = 1;quick_mod(n-1); // 求第n项,即求 (1,1)*A^(n-1) printf("%d\n", ans.e[0][0]);}return 0;
}
H - Fibonacci POJ - 3070 (矩阵快速幂)相关推荐
- I-Matrix Power Series POJ - 3233 矩阵快速幂+分治
I-Matrix Power Series POJ - 3233 矩阵快速幂+分治 Problem Description Given a n × n matrix A and a positive ...
- H - Log Concave Sequences —— 矩阵快速幂
This way 题意: 你现在有无穷个0,1,2,现在让你构造一个长度为n的串,使得满足以下条件 对于第i位(2<=i<n),a[i]*a[i]>=a[i-1]*a[i+1] 问你 ...
- POJ 3233 矩阵快速幂 等比数列
//http://www.cnblogs.com/jiangjing/archive/2013/05/28/3103336.html/* 分析:求a^1+..a^n这是矩阵乘法中关于等比矩阵的求法:| ...
- POJ3070 矩阵快速幂模板
题目:http://poj.org/problem?id=3070 矩阵快速幂模板.mod写到乘法的定义部分就行了. 别忘了 I ( ) 和 i n i t ( ) 要传引用! #include< ...
- 矩阵快速幂 POJ 3070 Fibonacci
题目传送门 1 /* 2 矩阵快速幂:求第n项的Fibonacci数,转置矩阵都给出,套个模板就可以了.效率很高啊 3 */ 4 #include <cstdio> 5 #include ...
- POJ 3070 Fibonacci(矩阵快速幂入门、模板)
? 题目链接:http://poj.org/problem?id=3070 ? 这题就是让求斐波那契数列的第n项,但是题目中n很大,所以打表和直接求都会TLE,对于这个题我们可以用矩阵快速幂,下面 ...
- SDNU 1062.Fibonacci(矩阵快速幂)
Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. I ...
- POJ3070 Fibonacci【矩阵快速幂】
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20098 Accepted: 13850 Descripti ...
- POJ 2778 DNA Sequence [AC自动机 + 矩阵快速幂]
http://poj.org/problem?id=2778 题意:给一些只由ACGT组成的模式串,问有多少种长度为n且不含有给出的模式串的DNA序列. 自动机的状态转换可以看成一个有向图(有重边的) ...
最新文章
- Linux C SQLite3 编程
- alpine安装启动mysql_在php5-fpm-alpine docker容器中安装php-gd依赖于alpine linux
- 信息论2——单维连续信源
- WORD如何取消编辑保护?
- am335x backlight
- 隆冬寒骨,风吹十一豪迈
- Volume Shadow Copy Service(VSS)如何工作
- 从优秀到卓越 pdf_演讲口才培训:演讲能力是卓越领导者需要具备的能力
- vue-cli开发Vue项目时定义环境变量需加VUE_APP前缀
- 视频教程-【CVPR2018】A Causal And-Or Graph Model for Visibil-计算机视觉
- 周鸿祎:35告老的大富豪
- python prettytable输出对齐_漂亮的输出-----prettytable和colorama的使用
- itools3.0服务器维护,APP Store失联?iTools3.0告诉你如何解决
- 大数据实时处理第三周课
- 整型转换为32位二进制字符串
- 阿里云搭建Tomcat+Jdk+Mysql(阿里云系统CentOs)特完整
- Swift编程语言简介
- 股市投资必修课二十六--商业竞争决定生死
- 吴军:顶级工程师能让中国走向浪潮之巅
- MessageBox的几点用法
热门文章
- 自动驾驶中的3D物体状态检测
- 链表问题10——两个单链表生成相加链表(方法二)
- linux里那些依赖包
- phpmyadmin底部出现提示“The configuration file now needs a secret passphrase (blowfish_secret). ”...
- ORM版学员管理系统
- 进行博客博文管理的设计
- 记一次数据中心云平台系统项目实施
- java 线程关闭小结(转)
- 《3D数学基础》1.8 混合积
- 一起谈.NET技术,微软PDC10:大牛谈ASP.NET和C#技术走向